How to Perform a gcloud CLI Install and Manage Google Cloud

Published:16 November 2022 - 8 min. read

Nicholas Xuan Nguyen Image

Nicholas Xuan Nguyen

Read more tutorials by Nicholas Xuan Nguyen!

Azure Cloud Labs: these FREE, on‑demand Azure Cloud Labs will get you into a real‑world environment and account, walking you through step‑by‑step how to best protect, secure, and recover Azure data.

As a Google Cloud Platform (GCP) user, you can manage your resources and applications via the web console. But what if you plan to automate tasks or manage Google Cloud from the command line? Worry not! The gcloud command-line interface (gcloud CLI) tool is what you need.

In this tutorial, you will learn how to install the gcloud CLI on your machine and start with some basic commands for managing GCP resources.

Stay tuned and up your GCP resource manage management!

Prerequisites

This tutorial will be a hands-on demonstration. To follow along, be sure you have the following:

A Google Cloud account with active billing – A free trial will suffice if you do not have one yet.

A Windows 10 machine with an internet connection. This tutorial uses the 21H2 version.

Docker installed on your machine.

Downloading and Installing the gcloud CLI

If you are a more CLI-inclined user, you can install the gcloud CLI using the Google Cloud SDK Installer. This cross-platform installer installs the gcloud CLI along with other Google Cloud SDK tools.

1. Open your favorite web browser, and download the installer from Google Cloud.

Alternatively, open PowerShell as administrator and run the below command to download the latest stable release of the gcloud CLI installer. This command does not provide output but automatically starts the Google Cloud CLI Setup wizard.

(New-Object Net.WebClient).DownloadFile("https://dl.google.com/dl/cloudsdk/channels/rapid/GoogleCloudSDKInstaller.exe", "$env:Temp\GoogleCloudSDKInstaller.exe")

& $env:Temp\GoogleCloudSDKInstaller.exe

2. Once downloaded, open the installer, which brings up the Google Cloud CLI Setup wizard (step three). If you fail to open the installer, run it as an administrator.

3. On the Google Cloud CLI Setup wizard, tick the first checkbox, as shown below, and click Next. This option lets you Turn on screen reader mode.

Enabling this option lets you use a screen reader to streamline your screen reader experience during the installation, like displaying a process bar.

Turning on the screen reader mode
Turning on the screen reader mode

4. Now, read the license term and click IAgree to agree to the license agreement.

Accepting the license agreement
Accepting the license agreement

5. Once agreed, select the install type (Single User), and click Next.

Selecting the install type
Selecting the install type

6. Next, select the location for the installation and click Next.

Selecting the installation location
Selecting the installation location

7. Tick all the checkboxes and click Install to install all the required dependencies, including Python v3 and the Cloud Tools for PowerShell.

Selecting components to install
Selecting components to install

As you can see below, the installer downloads and installs all the required dependencies and the CLI core. This process might take a few minutes to complete.

Viewing installation progress
Viewing installation progress

8. Once installed, click Next to continue.

Confirming gcloud CLI has been installed
Confirming gcloud CLI has been installed

9. Finally, tick all the checkboxes below, and click Finish to complete the installation.

Completing the gcloud CLI installation
Completing the gcloud CLI installation

A command prompt window opens automatically after the installation, as shown below. This prompt is where you will later configure the gcloud CLI.

Viewing the command prompt window for diagnostics
Viewing the command prompt window for diagnostics

Configuring the gcloud CLI

After installing gcloud CLI, you still need to configure gcloud CLI to ensure it works correctly by initializing the SDK and setting up your credentials.

1. Type Y and press Enter on the command prompt window.

Accepting this prompt opens a new tab in your default web browser, asking you to log in to your Google account (step two).

Confirming logging into Google Cloud
Confirming logging into Google Cloud

2. Log in to your Google account to continue.

Logging in to Google
Logging in to Google

3. Click Allow to grant the Google Cloud SDK the required permissions.

Granting the Google Cloud SDK the required permissions
Granting the Google Cloud SDK the required permissions

You will see a page similar to the one below in your web browser. This page confirms you have successfully logged in to your GCP account and authenticated to use the gcloud CLI.

Verifying authentication is successful
Verifying authentication is successful

4. Now, type 3 in the command prompt window and press Enter to create a new project. This project will be used to host your GCP resources.

Creating a new project
Creating a new project

5. Enter a Project ID (unique across your GCP account) for your project and press Enter. This tutorial uses the Project ID called gcp-gcloud-cli-demo.

Note the Project ID must be six to 30 characters long and can contain lowercase letters, digits, and hyphens.

Providing a Project ID
Providing a Project ID

At this point, gcloud CLI is successfully installed on your machine and is ready to be used, as shown below. You can now use this tool to manage your GCP resources from the command line.

Confirming gcloud CLI is ready
Confirming gcloud CLI is ready

6. Next, run the below command to get –help using the gcloud CLI.

gcloud --help

If all goes well, you will get the same output as below.

Getting help in using the gcloud CLI tool
Getting help in using the gcloud CLI tool

Installing the gcloud CLI via the Versioned Archives

If you prefer to install a specific version of the gcloud CLI, you can use versioned archives. This method helps install an older version of the CLI or if you wish to use gcloud CLI with a script in your CI/CD pipeline.

Versioned archives are like a portable version of the gcloud CLI. You can download these archives and extract them to any location on your machine. As these archives are self-contained, they do not require any additional dependencies.

A list of versioned archives of the gcloud CLI is available for Linux, macOS, and Windows.

To install gcloud CLI using a versioned archive, follow the steps below:

1. Navigate to the official page list of gcloud CLI versioned archives.

2. Download a compatible version archive of the gcloud CLI. You can choose any other archive based on your operating system and architecture. But this tutorial’s choice is the Windows 64-bit (x86_64) with Python bundled archive.

Downloading a compatible version archive of the gcloud CLI installer
Downloading a compatible version archive of the gcloud CLI installer

3. Once the download completes, extract the zip archive to any location on your machine.

Open a command prompt window, navigate to where you extracted the archive, and run the below command to install gcloud CLI.

.\install.bat

Type Y and press Enter to help Google improve this tool, or type N and press Enter to skip this prompt.

Opting in/out of Google data collecting.
Opting in/out of Google data collecting.

5. Now, input Y and press Enter to make the gcloud CLI available in all your command prompt windows. Or input N and press Enter to use this tool only in the current command prompt window.

Regardless of your choice, restarting your shell may be required.

Updating the %PATH% environment variable.
Updating the %PATH% environment variable.

Finally, run the below command to initialize (gcloud init) the SDK and configure your credentials.

Repeat steps three to five of the “Configuring the gcloud CLI” section to complete configuring your credentials.

.\bin\gcloud init

At this point, you have now successfully installed the gcloud CLI on your machine.

Initializing the SDK and configuring your credentials
Initializing the SDK and configuring your credentials

Installing gcloud CLI Using Docker

Docker is a containerization platform that can package and deploy applications. Docker is an excellent tool for developers who want to try out new technologies without impacting their local development environment.

Google provides a Docker image that contains the gcloud CLI, updated weekly with the latest version of the gcloud CLI.

To install gcloud CLI using Docker:

1. Run the below docker pull command in PowerShell to pull and install the latest version of the gcloud CLI Docker image.

docker pull gcr.io/google.com/cloudsdktool/google-cloud-cli:latest
Pulling the latest version of the gcloud CLI Docker image
Pulling the latest version of the gcloud CLI Docker image

Alternatively, check the list of available gcloud CLI Docker images, and run the below commands to pull the corresponding image from GCR. In this example, the below command installs the gcloud CLI version 370.0.0.

docker pull gcr.io/google.com/cloudsdktool/google-cloud-cli:370.0.0
Installing a specific gcloud CLI version
Installing a specific gcloud CLI version

2. Next, run the following docker run command to verify the gcloud CLI installation. This command runs a container using the gcloud CLI Docker image and prints the version of the gcloud CLI inside the container.

Perhaps you installed a specific version of gcloud CLI. In that case, run the same command below but ensure you change the latest tag to the actual version of the gcloud CLI you installed (i.e., 370.0.0).

docker run --rm gcr.io/google.com/cloudsdktool/google-cloud-cli:latest gcloud version

As you can see below, the latest version of the gcloud SDK and CLI is printed. But note that your version may be different as the image updates.

Verifying the installation
Verifying the installation

Below, you can confirm the installed specific version of gcloud CLI.

Verifying the specific gcloud CLI version installation
Verifying the specific gcloud CLI version installation

3. Run the below command to initialize the SDK and configure your credentials.

Your credentials will be saved in the gcloud-config container, which you can use in future sessions.

docker run -ti --name gcloud-config gcr.io/google.com/cloudsdktool/google-cloud-cli gcloud auth login

4. Lastly, follow the steps of the “Configuring the gcloud CLI” section to configure your credentials.

Managing gcloud Components

Now you have the gcloud CLI installed on your machine, explore some examples of using this tool and managing your Google Cloud.

One of the essential features of the gcloud CLI is the ability to manage Cloud SDK components. These components are individual tools and libraries that make up the Cloud SDK.

1. Run the below gcloud command in the command prompt window to list all available components and their version numbers.

gcloud components list

Pick an ID of the component you want to install.

Listing all available components
Listing all available components

2. Next, run the below command to download and install a new component. This tutorial’s choice is the Cloud Bigtable Emulator with the component ID bigtable.

gcloud components install bigtable

Input Y and hit Enter to continue.

Installing the Cloud Bigtable Emulator component
Installing the Cloud Bigtable Emulator component

3. Once installed, run the following command to update all installed components to their latest version.

gcloud components update
Updating all installed components to their latest version
Updating all installed components to their latest version

4. Now, run the below command to remove an unneeded component. In this example, the below command removes the Google Cloud CRC32C Hash Tool component (gcloud-crc32c).

gcloud components remove gcloud-crc32c
Removing an unneeded component
Removing an unneeded component

Creating a Virtual Machine Instance with gcloud CLI

Whether you wish to make a simple test environment to avoid causing harm to your computer or set up your own server, a virtual machine (VM) will do the trick. Luckily, one of the most important tasks you can do with the gcloud CLI is to create a new VM instance.

To create a VM with gcloud CLI:

1. Run the below command to list all public compute images available.

gcloud compute images list

Note the image family and project for the image you want to use.

Listing all public images available
Listing all public images available

2. Next, run the below command to list all machine types available in your region and the number of CPUs and memory for each type.

gcloud compute machine-types list

Take note of the machine type’s name you want to use.

Listing all machine types
Listing all machine types

3. Now, run the following command below to create a new VM instance (compute instance) called gcloud-cli-demo-vm against the details you noted in steps one and two.

Image Family–image-family=centos-7
Image Project–image-project=centos-cloud
Machine Type–machine-type=t2d-standard-4
gcloud compute instances create gcloud-cli-demo-vm --image-family=centos-7 --image-project=centos-cloud --machine-type=t2d-standard-4

The VM instance creation takes a few minutes to complete. Once created, you can see the instance’s status (running), as shown below.

Creating a VM instance
Creating a VM instance

4. Now, run the below gcloud compute command to describe the VM instance (gcloud-cli-demo-vm) in more detail.

gcloud compute instances describe gcloud-cli-demo-vm

Finally, run the following command to delete the VM instance (gcloud-cli-demo-vm).

gcloud compute instances delete gcloud-cli-demo-vm

Input Y and hit Enter to confirm deleting the specified VM instance.

Deleting a VM instance
Deleting a VM instance

Conclusion

Managing Google Cloud resources can be challenging, especially when you have too many. The good thing is that, in this tutorial, you learned how powerful gcloud CLI is in managing your resources. You learned how to install and configure the gcloud CLI and use it to manage components and create VM instances in Google Cloud.

This tutorial only covered the tip of the iceberg. The gcloud CLI comes with many more features. With this newfound knowledge, why not up the ante and explore some of these features on your own? Perhaps use the gcloud CLI to manage storage buckets, set IAM roles for users, and even deploy applications? The sky is the limit!

Hate ads? Want to support the writer? Get many of our tutorials packaged as an ATA Guidebook.

Explore ATA Guidebooks

Looks like you're offline!