A terminal, shell, or CLI rendered on the web is useful when managing resources in the cloud. One example is the Cloud Shell in the Google Cloud console. However, we found the experience and utility lacking, and the interface typically becomes clunky.
You can install Google Cloud CLI on your local computer if you want a full-featured and customizable Google Cloud CLI experience. Google Cloud CLI is available on most platforms, including Windows, Linux, and macOS.
If you’re into automation and scripting, gcloud is an essential weapon in your arsenal. It allows you to log in with a user account interactively or with a service account that can be assigned specific permissions and access levels to your Google Cloud Platform.
Don’t go away; we’ll learn how to install and set up the Google Cloud (gcloud) CLI on Linux.
- A computer running recent versions of Ubuntu/Debian or Red Hat/Fedora/CentOS. This post will be using Fedora 37 Workstation and Ubuntu Desktop 22.04.
- A Google Cloud Platform account.
Install Google Cloud CLI
Let me start by saying there are many ways to install Google Cloud CLI on any supported platform, and we will not cover all of them in this post. Instead, we’ll explore a few of the ways that are the easiest methods to install Google Cloud CLI on Linux.
Installing the Ubuntu/Debian Package
The Google Cloud CLI is available as a package to install in Ubuntu and Debian-based distribution. Google provides a package source that you can add to your operating systems package manager.
Note. Learn how to install AWS CLI and use it.
- First, open a terminal session on your Ubuntu system.
- Ensure that your machine can support the succeeding command by installing the following packages: apt-transport-https, ca-certificates, gnupg.
sudo apt install -y apt-transport-https ca-certificates gnupg
Don’t worry if the packages are already installed; APT will skip the installation.
- Next, run this command to add the Google Cloud SDK package source URI to your package sources list.
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
- Download and import Google’s GPG signing keys.
curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key --keyring /usr/share/keyrings/cloud.google.gpg add -
You can safely ignore the warning about apt-key being deprecated. It is not an error.
- Update the local package index and install Google Cloud CLI:
sudo apt update && sudo apt install -y google-cloud-cli
Wait for the google-cloud-cli installation to finish. This process should take at most five minutes on relatively decent hardware and internet speed.
- Finally, confirm the successful gcloud CLI installation by checking the bundled versions:
Installing the Fedora/RHEL/CentOS Package
You can also install Google Cloud CLI in Fedora through the package manager. But first, you must add the repository information so that DNF can find the package.
Note. Learn how to install OneDrive on Ubuntu and sync your files to the Cloud.
- Open a terminal session and run the below command. This command creates the /etc/yum.repos.d/google-cloud-sdk.repo file and inserts the repository source information:
sudo tee -a /etc/yum.repos.d/google-cloud-sdk.repo << EOM [google-cloud-cli] name=Google Cloud CLI baseurl=https://packages.cloud.google.com/yum/repos/cloud-sdk-el8-x86_64 enabled=1 gpgcheck=1 repo_gpgcheck=0 gpgkey=https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg EOM
- Now, install Google Cloud CLI by running the below command:
sudo dnf install google-cloud-cli
When asked to confirm the installation, press Y and Enter.
- When asked to import the Google GPG key, press y and Enter, wait for the installation to finish.
- Lastly, check the gcloud bundled versions:
Install Using the Google Cloud CLI Installer
If you don’t want to worry about installing the right package to the correct operating system, you can use the Google Cloud CLI installer instead. This method provides a wizard-type interactive installation experience.
Moreover, this method performs additional tasks, like adding the gcloud CLI components to the system’s PATH environment variable, and enables the shell command auto-completion.
- Open a terminal session and run this command to download the Google Cloud SDK install script:
curl https://sdk.cloud.google.com | bash
- Once downloaded, the installation script asks you to specify the installation directory. Leave it blank to accept the default path, which is typically your home directory. The script will then create a subdirectory called google-cloud-sdk.
- Wait as the files extract to the installation folder.
- Next, the installer asks if you want to opt-in to the usage reporting. The default option is N, which means to opt out. But if you want to enable the usage reporting, press y and Enter.
The installation continues. Wait for it to complete.
- When asked to let the installer update the $PATH and enable shell command completion, press y and Enter.
- Lastly, the installer will want to update your profile to add Google Cloud CLIs, leave the default value, and press Enter. Doing so will update your ~/.bashrc file.
- Reload your shell profile for the changes to take effect:
- Finally, check the gcloud version:
Authorize Google Cloud CLI as a User
Now that you’ve finished the Google Cloud CLI install task, it’s time to put it into action. But first, we must initialize and authenticate Google Cloud CLI against your GCP account.
The first type of authorization is through a user account. This type allows you to log in with your user account to authorize Google Cloud CLI with GCP.
- On the terminal, run the below command. This command authenticates and creates default configuration settings on your computer for the Google Cloud CLI:
The command performs network diagnostics to ensure that your computer has no connectivity issues.
- When prompted to log in, press Y and Enter, this action launches the default web browser.
- Sign in using your Google account with access to GCP and click Next.
- Enter your account password and click Next.
- Review the permissions requested by the Google SDK and click Allow.
- Go back to the terminal; you’ll see that you’ve successfully authenticated your Google Cloud CLI installation.
- Assuming you don’t have any existing projects in your GCP account, you will be asked if you want to create one. Why not? Press y and Enter.
- Next, type the name of your new GCP project. The project ID must be 16-30 characters with lowercase ASCII, digits, or hyphens. Special characters are not allowed.
- Finally, run this command to list all your projects in GCP. The result of this command will indicate if you’ve successfully initialized and authorized your Google Cloud CLI instance:
gcloud projects list
Authorize Google Cloud CLI as a Service Account
Authorizing with a service account is typically for non-interactive, scripting, no-browser login to GCP. A service account is a Google Cloud identity not specifically assigned to an actual user.
Create a Service Account in GCP
- Go to the IAM & Admin console using your web browser.
- Ensure to select the project you want to contain the service account.
- Click CREATE SERVICE ACCOUNT.
- Enter the service account name, service account ID, and service account description, and click CREATE AND CONTINUE.
- Next, choose the access level to give this service account to the project. This step is optional. Let’s grant the Viewer access to this service account in this example. Click CONTINUE.
- Now, choose who else (user or service account) would have access to this service account and what level of access they should have. In this example, let’s leave it blank and click DONE.
Download the Service Account Key ID
Once the Service Account is created, you must download the key ID file we will use with gcloud.
- Click the newly created service account.
- Go to the KEYS tab, click ADD KEY → Create new key.
- Choose JSON as the essential type and click CREATE.
- Once the key is downloaded, click Close.
- Locate the downloaded key file. You can rename and move this file to another location as you wish. In this example, we’ll rename the key file to xservicex.json and move it to the home folder.
Log in to the Service Account
- Go back to the terminal and confirm the key file location:
- Now, authenticate the service account by running this command. Replace <key_file> with your key file location:
gcloud auth login --cred-file=<key_file>
If the service account authentication is successful, you’ll get a similar result as below.
- Now, list all authorized accounts:
gcloud auth list
As you can see, the current active account is the service account. Any command you run at this point will be under the service account’s security context.
- Should you wish to switch accounts, run this command. Replace ‘ACCOUNT’ with the account name.
gcloud config set account 'ACCOUNT'
That’s it! You’ve completed the installation and basic configuration of the Google Cloud CLI on Linux. You’ve learned the different ways to install gcloud CLI and set up the authentication of both a user account and a service account.
You can now start managing GCP through the command line and automate tedious or routine tasks. Good luck!
Just a heads up, you have a typo here:
gcloud condif set account ‘ACCOUNT’
Thanks for the tutorial!
Thanks for tip!