Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to Install Google Cloud CLI (gcloud) and Set It Up on Ubuntu?

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.

Prerequisites

  • 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.

  1. First, open a terminal session on your Ubuntu system.
  2. 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.

  3. 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

  4. 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.

  5. 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.

  6. Finally, confirm the successful gcloud CLI installation by checking the bundled versions:
    gcloud version

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.

  1. 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 
    

  2. 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.

  3. When asked to import the Google GPG key, press y and Enter, wait for the installation to finish.
  4. Lastly, check the gcloud bundled versions:
    gcloud version

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.

  1. Open a terminal session and run this command to download the Google Cloud SDK install script:
    curl https://sdk.cloud.google.com | bash
  2. 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.
  3. Wait as the files extract to the installation folder.
  4. 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.
  5. When asked to let the installer update the $PATH and enable shell command completion, press y and Enter.
  6. 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.
  7. Reload your shell profile for the changes to take effect:
    source ~/.bashrc
  8. Finally, check the gcloud version:
    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.

  1. On the terminal, run the below command. This command authenticates and creates default configuration settings on your computer for the Google Cloud CLI:
    gcloud init

    The command performs network diagnostics to ensure that your computer has no connectivity issues.

  2. When prompted to log in, press Y and Enter, this action launches the default web browser.
  3. Sign in using your Google account with access to GCP and click Next.
  4. Enter your account password and click Next.
  5. Review the permissions requested by the Google SDK and click Allow.
  6. Go back to the terminal; you’ll see that you’ve successfully authenticated your Google Cloud CLI installation.
  7. 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.
  8. 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.
  9. 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

  1. Go to the IAM & Admin console using your web browser.
  2. Ensure to select the project you want to contain the service account.
  3. Click CREATE SERVICE ACCOUNT.
  4. Enter the service account name, service account ID, and service account description, and click CREATE AND CONTINUE.
  5. 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.
  6. 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.

  1. Click the newly created service account.
  2. Go to the KEYS tab, click ADD KEYCreate new key.
  3. Choose JSON as the essential type and click CREATE.
  4. Once the key is downloaded, click Close.
  5. 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

  1. Go back to the terminal and confirm the key file location:
    ls ~/xservicex.json

  2. Now, authenticate the service account by running this command. Replace with your key file location:
    gcloud auth login --cred-file=

    If the service account authentication is successful, you’ll get a similar result as below.

  3. 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.

  4. Should you wish to switch accounts, run this command. Replace ‘ACCOUNT’ with the account name.
    gcloud condif set account 'ACCOUNT'

Conclusion

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!



This post first appeared on TheITBros.com, please read the originial post: here

Share the post

How to Install Google Cloud CLI (gcloud) and Set It Up on Ubuntu?

×

Subscribe to Theitbros.com

Get updates delivered right to your inbox!

Thank you for your subscription

×