{"id":810,"date":"2024-03-28T05:55:00","date_gmt":"2024-03-28T05:55:00","guid":{"rendered":"https:\/\/blog.ngocha.biz\/?p=810"},"modified":"2024-03-28T05:55:00","modified_gmt":"2024-03-28T05:55:00","slug":"setup-google-cloud-clisdk","status":"publish","type":"post","link":"https:\/\/blog.ngocha.biz\/?p=810","title":{"rendered":"How to Setup Google Cloud SDK &#8211; gcloud CLI Beginners Guide"},"content":{"rendered":"<p>If you want to interact with Google Cloud through gcloud CLI or through code, you need to setup the gcloud CLI &amp;  SDK on your workstation.<\/p>\n<p>For example, if you want to write a python script that interacts with google cloud, you should have the gcloud sdk installed so that all the required packages for python will be available for your script.<\/p>\n<p>This tutorial will guide you through the steps for setting up Google Cloud gcloud CLI SDK on your workstation.<\/p>\n<p>Also, I have added some of the tips and tricks that I have learned while using <code>gcloud<\/code> CLI utility in my workstation.<\/p>\n<h2 id=\"install-gloud-cli-sdk\">Install gloud CLI SDK<\/h2>\n<div class=\"kg-card kg-callout-card kg-callout-card-grey\">\n<div class=\"kg-callout-text\"><b><strong style=\"white-space: pre-wrap;\">Note:<\/strong><\/b> If you&#8217;re using a google cloud instance, SDK is installed by default.<\/div>\n<\/div>\n<p><strong>Step 1:<\/strong> Head over to <a href=\"https:\/\/cloud.google.com\/sdk\/docs\/install?ref=devopscube.com\" rel=\"noreferrer noopener\">https:\/\/cloud.google.com\/sdk\/<\/a><a href=\"https:\/\/cloud.google.com\/sdk\/?ref=devopscube.com\" rel=\"noreferrer noopener\"> <\/a>and copy the link to the appropriate installation package. This package is common for all Linux distributions.<\/p>\n<div class=\"kg-card kg-callout-card kg-callout-card-grey\">\n<div class=\"kg-callout-text\">For Windows users: Download the installation package from <a href=\"https:\/\/cloud.google.com\/sdk\/docs\/install?ref=devopscube.com#windows\">https:\/\/cloud.google.com\/sdk\/windows<\/a> and install it directly on your systems. You will have the gcloud command available in the terminal.<\/div>\n<\/div>\n<p><strong>Step 2: <\/strong>Download the package.<\/p>\n<pre><code>wget https:\/\/dl.google.com\/dl\/cloudsdk\/channels\/rapid\/downloads\/google-cloud-cli-470.0.0-linux-x86_64.tar.gz<\/code><\/pre>\n<p><strong>Step 3: <\/strong>Untar the sdk package.<\/p>\n<pre><code>tar -xvf google-cloud-sdk*<\/code><\/pre>\n<p><strong>Step 4: <\/strong>Install the SDK using the <code>install.sh<\/code> script. It will detect the appropriate package for your Linux distribution and installs it.<\/p>\n<pre><code>.\/google-cloud-sdk\/install.sh<\/code><\/pre>\n<p><strong>Step 5:<\/strong> Follow through the installation prompts and select the required options. It is pretty self-explanatory. It will take a couple of minutes for the setup.<\/p>\n<figure class=\"kg-card kg-image-card kg-card-hascaption\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-47-11.png\" class=\"kg-image\" alt=\"gcloud CLI installation\" loading=\"lazy\" width=\"825\" height=\"541\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/image-47-11.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-47-11.png 825w\" sizes=\"auto, (min-width: 720px) 720px\"><figcaption><span style=\"white-space: pre-wrap;\">Click to view in HD<\/span><\/figcaption><\/figure>\n<p><strong>Step 6:<\/strong> Restart the terminal or source the <strong>.bashrc or .zshrc<\/strong> file based on the shell profile preference you chose during the installation.<\/p>\n<pre><code>source ~\/.bashrc\nsource ~\/.zshrc<\/code><\/pre>\n<p>If you dont open a new terminal session or source the relevant shell profile, you might see the following error.<\/p>\n<pre><code>-bash: gcloud: command not found<\/code><\/pre>\n<p><strong>Step 7: <\/strong>Now, to validate the installation, open a new terminal session and execute the <code>gcloud<\/code> command.<\/p>\n<pre><code>$ gcloud --version\n\nGoogle Cloud SDK 470.0.0\nbq 2.1.2\ncore 2024.03.22\ngcloud-crc32c 1.0.0\ngsutil 5.27<\/code><\/pre>\n<h3 id=\"configuring-gcloud-cli-with-google-project\">Configuring gcloud CLI With Google Project<\/h3>\n<p>Now that we have the SDK installed, we need to configure the Google cloud SDK to talk to a specific project.<\/p>\n<p>We use the <code>gcloud<\/code> utility from the installed SDK for CLI based google cloud access.<\/p>\n<p>Follow the steps given below to configure <code>gcloud<\/code> with a specific google project.<\/p>\n<p><strong>Step 1:<\/strong> Initialise the project configuration using the following command.<\/p>\n<pre><code>gcloud init --console-only<\/code><\/pre>\n<p><strong>Step 2:<\/strong> You will see the following prompt. Select &#8220;Y&#8221;.<\/p>\n<pre><code>To continue, you must log in. Would you like to log in (Y\/n)? Y<\/code><\/pre>\n<figure class=\"kg-card kg-image-card kg-card-hascaption\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-7-34.png\" class=\"kg-image\" alt=\"google cloud sdk auth\" loading=\"lazy\" width=\"849\" height=\"520\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/image-7-34.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-7-34.png 849w\" sizes=\"auto, (min-width: 720px) 720px\"><figcaption><span style=\"white-space: pre-wrap;\">Click to view in HD<\/span><\/figcaption><\/figure>\n<p>Copy and paste the URL in the browser. Then log in to your google cloud account and grant permissions to access google cloud resources.<\/p>\n<p>You will see a page with a token. Copy the token and enter in the terminal.<\/p>\n<p>Now you will be logged in, and you can select the project that you want to set as default, as shown in the image below.<\/p>\n<figure class=\"kg-card kg-image-card kg-card-hascaption\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-8-30.png\" class=\"kg-image\" alt=\"google cloud SDK project configuration.\" loading=\"lazy\" width=\"799\" height=\"290\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/image-8-30.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-8-30.png 799w\" sizes=\"auto, (min-width: 720px) 720px\"><figcaption><span style=\"white-space: pre-wrap;\">Click to view in HD<\/span><\/figcaption><\/figure>\n<p>Like this, you can add <a href=\"https:\/\/devopscube.com\/setup-google-cloud-clisdk\/\">multiple gcloud configurations<\/a> if you work with multiple google cloud accounts. You can switch between different accounts. I have explained this in the next section.<\/p>\n<h2 id=\"gcloud-cli-sdk-auth-config-location\">gcloud CLI SDK Auth Config Location<\/h2>\n<p>To know all the configuration location of gcloud CLI SDK, execute the following command.<\/p>\n<pre><code>gcloud info<\/code><\/pre>\n<figure class=\"kg-card kg-image-card kg-card-hascaption\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-12-19.png\" class=\"kg-image\" alt=\"default google cloud SDK configurations locations\" loading=\"lazy\" width=\"664\" height=\"407\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/image-12-19.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-12-19.png 664w\"><figcaption><span style=\"white-space: pre-wrap;\">Click to view in HD<\/span><\/figcaption><\/figure>\n<p>All the SDK configs get stored in the <code>.config<\/code> folder in your workstation home location.<\/p>\n<p>If I run a tree from the <code>.config<\/code> folder, this is what I have. The JSON files under legacy_credentials have the <code>client id <\/code>and <code>client secret<\/code> to connect to the google cloud account.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-11-22.png\" class=\"kg-image\" alt=\"gcloud credentials location path\" loading=\"lazy\" width=\"337\" height=\"428\"><\/figure>\n<h2 id=\"validate-gcloud-cli-authentication\">Validate gcloud CLI Authentication<\/h2>\n<p>Now that we have a valid cloud configuration, let&#8217;s run some basic <code>gcloud<\/code> CLI commands to verify the installation.<\/p>\n<p><strong>Step 1:<\/strong> List the credential account.<\/p>\n<pre><code>gcloud auth list<\/code><\/pre>\n<p>In my case, I have two google cloud accounts configured in my workstation. So it lists two accounts, and there is a <code>*<\/code> in the active account.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-10-26.png\" class=\"kg-image\" alt=\"\" loading=\"lazy\" width=\"383\" height=\"193\"><\/figure>\n<p>When you have multiple <code>gcloud<\/code> accounts, you can switch between accounts using the following command<\/p>\n<pre><code>gcloud config set account account@devopscube.com<\/code><\/pre>\n<p><strong>Step 2:<\/strong> To view your default <code>gcloud<\/code> configs of the active configuration, use the following command.<\/p>\n<pre><code>gcloud config list<\/code><\/pre>\n<p>You can also manually change the configuration by editing the config file from the <code>~\/.config\/gcloud\/configurations<\/code> folder.<\/p>\n<p>Sometimes when you have multiple accounts configured, there could be a mismatch between activated account and active configuration. To solve this, first list the configruations.<\/p>\n<pre><code>gcloud config configurations list<\/code><\/pre>\n<figure class=\"kg-card kg-image-card kg-card-hascaption\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-13-21.png\" class=\"kg-image\" alt=\"list available gcloud configurations\" loading=\"lazy\" width=\"647\" height=\"102\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/image-13-21.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-13-21.png 647w\"><figcaption><span style=\"white-space: pre-wrap;\">Click to view in HD<\/span><\/figcaption><\/figure>\n<p>If there is a mismatch, you can activate the correct configuration using the following command, assuming you activate the default configuration.<\/p>\n<pre><code>gcloud config configurations activate default<\/code><\/pre>\n<p><strong>Step 3:<\/strong> To list all the <code>gcloud<\/code> commands, use the following command.<\/p>\n<pre><code>gcloud help<\/code><\/pre>\n<h2 id=\"gcloud-cheat-sheet\">gcloud Cheat Sheet<\/h2>\n<p>Well, I don&#8217;t have any cheat sheet for <code>gcloud<\/code>.<\/p>\n<p>However, there is a cool <code>gcloud<\/code> cheat sheet command which displays the gcloud cheat sheet.<\/p>\n<pre><code>gcloud cheat-sheet<\/code><\/pre>\n<p>Also you can use the <a href=\"https:\/\/cloud.google.com\/sdk\/docs\/cheatsheet?ref=devopscube.com\" rel=\"noreferrer noopener\">cheat sheet from google cloud documentation<\/a>.<\/p>\n<h2 id=\"other-gcloud-components\">Other gcloud Components<\/h2>\n<p>When you install the google SDK, only the core google cloud libraries get installed.<\/p>\n<p>But there are other components that are part of google cloud sdk.<\/p>\n<p>For example <code>kubectl<\/code>. A utility required to interact with Google <a href=\"https:\/\/devopscube.com\/setup-kubernetes-cluster-kubeadm\/\" rel=\"noreferrer noopener\">Kubernetes cluster<\/a>.<\/p>\n<p>To list all the available components execute the following command.<\/p>\n<pre><code>gcloud components list<\/code><\/pre>\n<figure class=\"kg-card kg-image-card kg-card-hascaption\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-14-32.png\" class=\"kg-image\" alt=\"list of gcloud components\" loading=\"lazy\" width=\"994\" height=\"789\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/image-14-32.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-14-32.png 994w\" sizes=\"auto, (min-width: 720px) 720px\"><figcaption><span style=\"white-space: pre-wrap;\">Click to view in HD<\/span><\/figcaption><\/figure>\n<h2 id=\"updating-gcloud-cli-components\">Updating gcloud CLI Components<\/h2>\n<p>Updating gcloud CLI is pretty simple.<\/p>\n<p>Use the following one-line to update gcloud components.<\/p>\n<pre><code>gcloud components update<\/code><\/pre>\n<h2 id=\"create-a-google-compute-instance-using-cli\">Create a Google Compute Instance Using CLI<\/h2>\n<p>This section is only optional.<\/p>\n<p>If you want to have a little hands-on with the gcloud command-line utility, you can follow this section to create a compute instance, access it, and then delete it.<\/p>\n<p>To start with, we will create a instance using the CLI.<\/p>\n<p><strong>Step 1:<\/strong> Get the list of images using the following command.<\/p>\n<pre><code>gcloud compute images list<\/code><\/pre>\n<figure class=\"kg-card kg-image-card kg-card-hascaption\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-15-22.png\" class=\"kg-image\" alt=\"gcloud list compute instance images from google cloud\" loading=\"lazy\" width=\"670\" height=\"430\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/image-15-22.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-15-22.png 670w\"><figcaption><span style=\"white-space: pre-wrap;\">Click to view in HD<\/span><\/figcaption><\/figure>\n<p><strong>Step 2:<\/strong> The following command will create an f1 micro ubuntu instance with the ubuntu image from the highlighted image family.<\/p>\n<pre><code>gcloud compute instances create devopscube-demo-instance \\\n --image centos-7-v20170523 \\\n --image-project centos-cloud --set-machine-type f1-micro --zone us-central1-a<\/code><\/pre>\n<p>There are so many flags associated with instance creations. Refer to <a href=\"https:\/\/cloud.google.com\/sdk\/gcloud\/reference\/compute\/instances\/create?ref=devopscube.com\">this official documentation<\/a> for more information on the flags.<\/p>\n<p><strong>Step 3: <\/strong>To connect the instance via ssh, just execute the following command. The <code>gcloud<\/code> command will automatically create the ssh key in your ~\/.ssh folder if it doesn&#8217;t exist and connects to your instance.<\/p>\n<div class=\"kg-card kg-callout-card kg-callout-card-grey\">\n<div class=\"kg-callout-text\"><b><strong style=\"white-space: pre-wrap;\">Security Note:<\/strong><\/b> Do not deploy any public images in enterprise accounts even if you have permissions. Always use security-approved images in enterprise accounts to stay out of trouble.<\/div>\n<\/div>\n<p>gcloud compute ssh (instance-name)<\/p>\n<p>For example,<\/p>\n<pre><code>gcloud compute ssh devopscube-demo-instance<\/code><\/pre>\n<p><strong>Step 4:<\/strong> Delete the created instance using the following command.<\/p>\n<pre><code>gcloud compute instances delete (instance name)<\/code><\/pre>\n<p>For example,<\/p>\n<pre><code>gcloud compute instances delete devopscube-demo-instance<\/code><\/pre>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>In this blog I have covered the steps to setup google cloud SDK and using it with gcloud CLI<\/p>\n<p>Hope it helped or you might have more tips that can add value to the existing content.<\/p>\n<p>Either way, please drop a comment.<\/p>\n<hr>\n<p><strong>Ngu\u1ed3n:<\/strong> <a href=\"https:\/\/devopscube.com\/setup-google-cloud-clisdk\/\" target=\"_blank\" rel=\"noopener noreferrer\">How to Setup Google Cloud SDK &#8211; gcloud CLI Beginners Guide \u2014 DevOpsCube<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Source: https:\/\/devopscube.com\/setup-google-cloud-clisdk\/<\/p>\n","protected":false},"author":1,"featured_media":811,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-810","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops"],"_links":{"self":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/810","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=810"}],"version-history":[{"count":0,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/810\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/media\/811"}],"wp:attachment":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=810"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=810"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=810"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}