PastaInc logo
Pasta Inc
web development

How to Set Up the Google Cloud SDK Locally and Authenticate via IAM

How to Set Up the Google Cloud SDK Locally and Authenticate via IAM
0 views
2 min read
#web development

Overview

Setting up the Google Cloud SDK (Software Development Kit) locally is an essential step for developers who want to interact with Google Cloud Platform (GCP) resources. In this guide, we will provide instructions for Windows, macOS, and Linux. You will learn how to install the SDK, connect using IAM credentials, and initialize the SDK on your local machine.

Prerequisites

Before proceeding, ensure you have the following:

  • A Google Cloud account.
  • IAM credentials with sufficient permissions to access the resources you need.
  • Administrative privileges on your local machine.

1. Install the Google Cloud SDK

Windows

Download the Installer:

  • Go to the Google Cloud SDK download page and download the Windows installer

Run the Installer:

  • Double-click the downloaded .exe file
  • Follow the on-screen instructions to complete the installation
  • During installation, ensure you select the option to "Add gcloud to your PATH."

Verify Installation:

Open the Command Prompt and run the following command:

gcloud \--version

Ensure the SDK version is displayed.

macOS

Download and Install:

  • Open a terminal and run the following command to download and install the SDK:

curl -O https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-sdk-\<latest_version\>.tar.gz

tar -xf google-cloud-sdk-\<latest_version\>.tar.gz

./google-cloud-sdk/install.sh

Add to PATH:

  • Append the SDK path to your .zshrc or .bashrc file:

export PATH="\$PATH:/path/to/google-cloud-sdk/bin"

  • Reload the terminal or run source ~/.zshrc or source ~/.bashrc.

Verify Installation:

Run the following command:

gcloud --version

Linux

Download and Install:

  • Open a terminal and run the following commands:

curl -O https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-sdk-\<latest_version\>.tar.gz

tar -xf google-cloud-sdk-\<latest_version\>.tar.gz

./google-cloud-sdk/install.sh

Add to PATH:

  • Append the SDK path to your .bashrc or .zshrc file:

export PATH="$PATH:/path/to/google-cloud-sdk/bin"

Reload the terminal or run source \~/.bashrc or source \~/.zshrc.

Verify Installation:

Run the following command:

gcloud --version

2. Authenticate Using IAM

Sign In with gcloud:

  • Run the following command to authenticate:

gcloud auth login

  • A browser window will open. Log in with your Google Cloud account.

Set a Project:

  • Identify the project you want to use:

gcloud projects list

  • Set the desired project:

gcloud config set project [PROJECT_ID]

Use a Service Account - Optional:

  • If you need to authenticate using a service account, download the JSON key file from the GCP Console
  • Authenticate using the key file:

gcloud auth activate-service-account [ACCOUNT_NAM] --key-file=[PATH_TO_KEY_FILE]

3. Initialize the SDK

Run the following command to initialize the SDK:

gcloud init

This command guides you through:

  • Choosing the account to use
  • Selecting the project
  • Setting a default compute region and zone

Conclusion

You now have the Google Cloud SDK installed and configured on your local machine. With the SDK set up, you can start interacting with GCP resources using gcloud commands.

If you encounter any issues, refer to the official documentation for troubleshooting tips.

Resources