Google Cloud
beginner
15-20 minutes

Installing the gcloud CLI

Install and configure the Google Cloud SDK on your local machine for command-line deployments and management.

What you'll accomplish

gcloud CLI installed, authenticated, and configured for your project

Prerequisites

  • GCP account created
  • Project created with billing enabled

What is the gcloud CLI?

The gcloud command-line interface lets you manage GCP resources from your terminal:

bash
# Deploy a container
gcloud run deploy my-app --image gcr.io/my-project/my-app

# Create a VM
gcloud compute instances create my-vm

# View logs
gcloud logging read "resource.type=cloud_run_revision"

The Google Cloud SDK includes:

  • gcloud - Main CLI for GCP services
  • gsutil - Cloud Storage operations
  • bq - BigQuery operations

Installation

Initial Configuration

Run gcloud init

bash
gcloud init

This interactive wizard will:

  1. Welcome message - Press Enter to continue

  2. Choose account - Select "Log in with a new account" or pick existing

  3. Authorize in browser - A browser window opens

    • Sign in to Google
    • Allow gcloud CLI access
    • Return to terminal
  4. Select project - Choose your project from the list or enter project ID

  5. Configure default region - Select a default compute region

Example Session

Welcome! This command will take you through the configuration of gcloud.

Pick configuration to use:
 [1] Re-initialize this configuration with new settings
 [2] Create a new configuration
Please enter your numeric choice: 1

Your current configuration has been set to: [default]

Choose the account you would like to use:
 [1] [email protected]
 [2] Log in with a new account
Please enter your numeric choice: 1

Pick cloud project to use:
 [1] my-project-123456
 [2] another-project-789
 [3] Enter a project ID
Please enter numeric choice: 1

Your current project has been set to: [my-project-123456].

Verify Configuration

Check Current Settings

bash
# Show all configuration
gcloud config list

# Output:
# [core]
# account = [email protected]
# project = my-project-123456
#
# [compute]
# region = us-central1

Check Authentication

bash
# List authenticated accounts
gcloud auth list

# Output:
# ACTIVE  ACCOUNT
# *       [email protected]

Check Project

bash
# Get current project
gcloud config get-value project

# Output: my-project-123456

Configuration Commands

Set Project

bash
# Set default project
gcloud config set project my-project-123456

# Use a different project for one command
gcloud compute instances list --project other-project

Set Region/Zone

bash
# Set default region
gcloud config set compute/region us-central1

# Set default zone
gcloud config set compute/zone us-central1-a

Common Regions

RegionLocationUse Case
us-central1Iowa, USALow cost, general purpose
us-east1South CarolinaUS East Coast
us-west1OregonUS West Coast
europe-west1BelgiumEuropean users
europe-west2LondonUK users
asia-east1TaiwanAsian users
asia-northeast1TokyoJapanese users
australia-southeast1SydneyAustralian users

Multiple Configurations

Manage different projects/accounts with configurations:

Create Configuration

bash
# Create new configuration
gcloud config configurations create my-work-config

# Set properties
gcloud config set account [email protected]
gcloud config set project work-project-id

Switch Configurations

bash
# List configurations
gcloud config configurations list

# Switch to different configuration
gcloud config configurations activate my-work-config

# Switch back
gcloud config configurations activate default

Use Configuration for Single Command

bash
gcloud compute instances list --configuration=my-work-config

Authentication Methods

User Authentication (Interactive)

bash
# Standard login (opens browser)
gcloud auth login

# Login with specific scopes
gcloud auth login --scopes=https://www.googleapis.com/auth/cloud-platform

# Check logged-in accounts
gcloud auth list

Service Account Authentication

For scripts, CI/CD, and automated processes:

bash
# Activate service account with key file
gcloud auth activate-service-account \
  --key-file=/path/to/service-account-key.json

# Set as application default credentials
export GOOGLE_APPLICATION_CREDENTIALS="/path/to/service-account-key.json"

Application Default Credentials (ADC)

For applications using Google client libraries:

bash
# Set up ADC for local development
gcloud auth application-default login

# This creates credentials at:
# Linux/macOS: ~/.config/gcloud/application_default_credentials.json
# Windows: %APPDATA%\gcloud\application_default_credentials.json

Enable APIs via CLI

bash
# Enable a single API
gcloud services enable run.googleapis.com

# Enable multiple APIs
gcloud services enable \
  run.googleapis.com \
  cloudbuild.googleapis.com \
  artifactregistry.googleapis.com

# List enabled APIs
gcloud services list --enabled

# List available APIs
gcloud services list --available

Common Commands Reference

Project Management

bash
gcloud projects list                    # List all projects
gcloud projects describe PROJECT_ID     # Project details
gcloud config set project PROJECT_ID    # Switch project

Account Management

bash
gcloud auth list                        # List accounts
gcloud auth login                       # Add account
gcloud auth revoke                      # Remove account
gcloud auth revoke --all               # Remove all accounts

Configuration

bash
gcloud config list                      # Show config
gcloud config set KEY VALUE             # Set value
gcloud config unset KEY                 # Remove value

Information

bash
gcloud info                             # Installation info
gcloud version                          # Version info
gcloud help                             # General help
gcloud help COMMAND                     # Command help

Update the CLI

bash
# Check for updates
gcloud components update

# Install additional components
gcloud components install kubectl
gcloud components install docker-credential-gcr

# List installed components
gcloud components list

Docker Authentication

To push images to Google Container Registry or Artifact Registry:

bash
# Configure Docker to use gcloud credentials
gcloud auth configure-docker

# For Artifact Registry (specific region)
gcloud auth configure-docker us-central1-docker.pkg.dev

Troubleshooting

"Command not found: gcloud"

bash
# Ensure gcloud is in PATH
# Add to ~/.bashrc or ~/.zshrc:
export PATH="$HOME/google-cloud-sdk/bin:$PATH"

# Then reload
source ~/.bashrc

"You do not have permission"

bash
# Check current account
gcloud auth list

# Re-authenticate
gcloud auth login

# Check project permissions
gcloud projects get-iam-policy PROJECT_ID

"The project property is not set"

bash
# Set the project
gcloud config set project YOUR_PROJECT_ID

Reset Everything

bash
# Remove all configurations and start fresh
gcloud auth revoke --all
rm -rf ~/.config/gcloud
gcloud init
Note

Checkpoint: Run gcloud config list to verify your account and project are correctly configured.