Command Palette

Search for a command to run...

Google Cloud CLI Installation Guide

Complete guide for installing and configuring Google Cloud Command Line Interface (gcloud CLI) across different operating systems.

The Google Cloud CLI (gcloud) is a command-line tool for managing Google Cloud resources and services.

Prerequisites

  • Active Google Cloud account
  • Internet connection
  • Python 3.7 or later (for some operating systems)
  • Administrative privileges

Installation Instructions

Ubuntu/Debian Installation

# Add the Cloud SDK distribution URI as a package source
echo "deb [signed-by=/usr/share/keyrings/cloud.google.gpg] https://packages.cloud.google.com/apt cloud-sdk main" | \
  sudo tee -a /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 apt-key --keyring /usr/share/keyrings/cloud.google.gpg add -
 
# Update and install the Cloud SDK
sudo apt update
sudo apt install -y google-cloud-cli
 
# Initialize gcloud
gcloud init
 
# Verify installation
gcloud --version

Configuration

Initial Setup

# Initialize gcloud and authenticate
gcloud init
 
# Authenticate with application-default credentials
gcloud auth application-default login
 
# Set project
gcloud config set project PROJECT_ID
 
# Set compute zone
gcloud config set compute/zone us-central1-a

Multiple Configurations

# Create new configuration
gcloud config configurations create my-config
 
# List configurations
gcloud config configurations list
 
# Switch configurations
gcloud config configurations activate my-config
 
# View current configuration
gcloud config list

Service Account Setup

# Create service account
gcloud iam service-accounts create my-service-account \
  --display-name "My Service Account"
 
# Download service account key
gcloud iam service-accounts keys create key.json \
  --iam-account my-service-account@PROJECT_ID.iam.gserviceaccount.com
 
# Activate service account
gcloud auth activate-service-account \
  --key-file=key.json

Basic Usage

Compute Engine

# List instances
gcloud compute instances list
 
# Create instance
gcloud compute instances create my-instance \
  --machine-type=e2-medium \
  --image-family=debian-10 \
  --image-project=debian-cloud
 
# SSH into instance
gcloud compute ssh my-instance
 
# Stop instance
gcloud compute instances stop my-instance

Cloud Storage

# Create bucket
gsutil mb gs://my-bucket
 
# Copy files to bucket
gsutil cp local-file.txt gs://my-bucket/
 
# List buckets
gsutil ls
 
# Set bucket permissions
gsutil iam ch user:john@example.com:objectViewer gs://my-bucket

Troubleshooting

Best Practices

  1. Project Organization

    # Create and manage projects
    gcloud projects create my-project-id
    gcloud config set project my-project-id
     
    # Use labels for resources
    gcloud compute instances create my-instance \
      --labels=environment=production,team=devops
  2. Security

    # Use service accounts for automation
    gcloud iam service-accounts create my-sa \
      --display-name "My Service Account"
     
    # Grant minimal permissions
    gcloud projects add-iam-policy-binding PROJECT_ID \
      --member="serviceAccount:my-sa@PROJECT_ID.iam.gserviceaccount.com" \
      --role="roles/viewer"
  3. Resource Management

    # Use resource policies
    gcloud compute resource-policies create snapshot-schedule \
      policy-name --schedule="0 */12 * * *" \
      --snapshot-labels=type=automated

Advanced Configuration

Environment Variables

# Set project ID
export GOOGLE_CLOUD_PROJECT="your-project-id"
 
# Set application credentials
export GOOGLE_APPLICATION_CREDENTIALS="path/to/service-account-key.json"
 
# Set default region
export CLOUDSDK_COMPUTE_REGION="us-central1"

Configuration File

# ~/.config/gcloud/configurations/config_default
[core]
account = user@example.com
project = my-project-id
disable_usage_reporting = True
 
[compute]
zone = us-central1-a
region = us-central1
 
[container]
cluster = my-cluster

Components and Extensions

# List available components
gcloud components list
 
# Install specific components
gcloud components install beta
gcloud components install kubectl
 
# Update all components
gcloud components update
 
# Remove component
gcloud components remove COMPONENT_ID

Next Steps