How to Configure gcloud CLI in GCP: Step-by-Step Guide
To configure the
gcloud CLI in Google Cloud Platform, run gcloud init to start the setup wizard. This command helps you log in, select a project, and set default settings for your cloud environment.Syntax
The main command to configure the gcloud CLI is gcloud init. It guides you through authentication, project selection, and default configuration.
gcloud init: Starts the interactive setup.gcloud auth login: Logs you into your Google account.gcloud config set project [PROJECT_ID]: Sets the default project.gcloud config list: Shows current configuration.
bash
gcloud init gcloud auth login gcloud config set project [PROJECT_ID] gcloud config list
Example
This example shows how to run gcloud init to configure your CLI. It will open a browser to log in, then ask you to pick a project or create a new one.
bash
gcloud init
Output
Welcome! This command will take you through the configuration of gcloud.
Pick configuration to use:
[1] Re-initialize this configuration
[2] Create a new configuration
Please enter your numeric choice: 1
You are logged in as: user@example.com
Pick cloud project to use:
[1] my-project-123
[2] Create a new project
Please enter numeric choice or text value: 1
Your current project has been set to: my-project-123
Do you want to configure a default Compute Region and Zone? (Y/n)? y
Enter your desired Compute Region (e.g. us-central1): us-central1
Enter your desired Compute Zone (e.g. us-central1-a): us-central1-a
Configuration complete!
Common Pitfalls
Common mistakes when configuring gcloud CLI include:
- Not logging in before setting a project, causing errors.
- Choosing the wrong project or forgetting to set one.
- Skipping region and zone setup, which can cause issues with some services.
- Running commands without internet connection, which blocks authentication.
Always run gcloud init first to avoid these issues.
bash
Wrong way: # Trying to set project without login gcloud config set project my-project-123 # This will fail if not logged in Right way: gcloud auth login gcloud config set project my-project-123
Quick Reference
Here is a quick cheat sheet for configuring gcloud CLI:
| Command | Purpose |
|---|---|
| gcloud init | Start interactive setup and login |
| gcloud auth login | Log in to your Google account |
| gcloud config set project [PROJECT_ID] | Set default project |
| gcloud config set compute/region [REGION] | Set default compute region |
| gcloud config set compute/zone [ZONE] | Set default compute zone |
| gcloud config list | Show current configuration |
Key Takeaways
Run
gcloud init first to configure your CLI with login and project setup.Always log in using
gcloud auth login before running other commands.Set your default project with
gcloud config set project [PROJECT_ID].Configure default region and zone to avoid issues with compute services.
Check your current settings anytime with
gcloud config list.