GCP for DevOps — gcloud CLI Setup
gcloud CLI Setup
The gcloud CLI is your primary tool for interacting with GCP from the command line. Every DevOps engineer working with GCP uses it daily for managing resources, deploying applications, and automating workflows.
What is the Google Cloud SDK?
The Google Cloud SDK is a set of command-line tools that includes:
| Tool | Purpose |
|---|---|
gcloud |
Main CLI for managing GCP resources |
gsutil |
Cloud Storage operations |
bq |
BigQuery operations |
kubectl |
Kubernetes cluster management (installable component) |
Installation
macOS
# Using Homebrew
$ brew install --cask google-cloud-sdk
# Or download directly
$ curl https://sdk.cloud.google.com | bash
# Restart your shell
$ exec -l $SHELL
# Verify installation
$ gcloud version
Google Cloud SDK 470.0.0
bq 2.1.3
core 2026.03.14
gcloud-crc32c 1.0.0
gsutil 5.29
Linux (Debian/Ubuntu)
# Add the Cloud SDK repo
$ echo "deb [signed-by=/usr/share/keyrings/cloud.google.asc] https://packages.cloud.google.com/apt cloud-sdk main" | \
sudo tee /etc/apt/sources.list.d/google-cloud-sdk.list
# Import the Google Cloud public key
$ curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | \
sudo tee /usr/share/keyrings/cloud.google.asc
# Install the SDK
$ sudo apt-get update && sudo apt-get install -y google-cloud-cli
$ gcloud version
Google Cloud SDK 470.0.0
...
Windows
Download the installer from cloud.google.com/sdk/docs/install and run it. Or use PowerShell:
(New-Object Net.WebClient).DownloadFile("https://dl.google.com/dl/cloudsdk/channels/rapid/GoogleCloudSDKInstaller.exe", "$env:Temp\GoogleCloudSDKInstaller.exe")
& $env:Temp\GoogleCloudSDKInstaller.exe
Initial Configuration
Authenticate
# Interactive login — opens a browser
$ gcloud auth login
Your browser has been opened to visit:
https://accounts.google.com/o/oauth2/auth?...
You are now logged in as [you@example.com].
Your current project is [None].
# Verify who you are
$ gcloud auth list
ACCOUNT ACTIVE
* you@example.com True
Initialize gcloud
$ gcloud init
Welcome! This command will take you through the configuration of gcloud.
Pick configuration to use:
[1] Re-initialize this configuration [default] with new settings
[2] Create a new configuration
Please enter your numeric choice: 1
Choose the account you would like to use to perform operations for this configuration:
[1] you@example.com
Please enter your numeric choice: 1
Pick cloud project to use:
[1] my-devops-lab
[2] my-first-project
[3] Enter a project ID
Please enter numeric choice: 1
Your current project has been set to: [my-devops-lab].
Do you want to configure a default Compute Region and Zone? (Y/n)? Y
[1] us-central1-a
...
Please enter numeric choice: 1
Your project default Compute Engine zone has been set to [us-central1-a].
Set Configuration Properties
# Set default project
$ gcloud config set project my-devops-lab
# Set default region and zone
$ gcloud config set compute/region us-central1
$ gcloud config set compute/zone us-central1-a
# View all configuration
$ gcloud config list
[compute]
region = us-central1
zone = us-central1-a
[core]
account = you@example.com
project = my-devops-lab
Application Default Credentials (ADC)
For applications and tools like Terraform to authenticate with GCP, use Application Default Credentials:
$ gcloud auth application-default login
Your browser has been opened to visit:
https://accounts.google.com/o/oauth2/auth?...
Credentials saved to file:
[/Users/you/.config/gcloud/application_default_credentials.json]
This creates a credentials file that Terraform, client libraries, and other tools use automatically.
Cloud Shell
Cloud Shell is a browser-based terminal with gcloud pre-installed. It’s perfect for quick commands without local setup:
- 5 GB persistent home directory
- Pre-installed: gcloud, kubectl, terraform, docker, git
- Free to use
- Access from any browser at shell.cloud.google.com
# In Cloud Shell, you're already authenticated
$ gcloud config list
[core]
account = you@example.com
project = my-devops-lab
Managing Configurations
You can have multiple gcloud configurations for different projects:
# Create a new configuration
$ gcloud config configurations create prod-config
Created [prod-config].
Activated [prod-config].
# Set properties for this configuration
$ gcloud config set project my-app-prod
$ gcloud config set compute/region us-east1
# Switch between configurations
$ gcloud config configurations activate default
Activated [default].
# List all configurations
$ gcloud config configurations list
NAME IS_ACTIVE ACCOUNT PROJECT DEFAULT_ZONE DEFAULT_REGION
default True you@example.com my-devops-lab us-central1-a us-central1
prod-config False you@example.com my-app-prod us-east1
Essential gcloud Commands
# Get help for any command
$ gcloud help compute instances create
# Format output as JSON
$ gcloud compute instances list --format=json
# Format output as table with specific columns
$ gcloud compute instances list \
--format="table(name,zone,machineType,status)"
# Filter results
$ gcloud compute instances list --filter="status=RUNNING"
# Get the project number
$ gcloud projects describe my-devops-lab --format="value(projectNumber)"
123456789012
Common Mistakes
| Mistake | Fix |
|---|---|
Not running gcloud init after install |
Always initialize gcloud after installation |
Forgetting gcloud auth application-default login |
Required for Terraform and other tools |
| Working in wrong project | Check with gcloud config get project |
| Not updating gcloud | Run gcloud components update regularly |
| Using personal account for production | Use service accounts for automation |
Quick Reference
| Command | Description |
|---|---|
gcloud init |
Initialize gcloud |
gcloud auth login |
Authenticate with Google |
gcloud auth application-default login |
Set up ADC for tools |
gcloud config set project <id> |
Set active project |
gcloud config list |
View current config |
gcloud config configurations create <name> |
Create named config |
gcloud config configurations activate <name> |
Switch config |
gcloud components update |
Update gcloud SDK |
gcloud components install kubectl |
Install kubectl |
Next → Practice Exercises