'"sudo: gcloud: command not found" when running Google Cloud SDK

Running Ubuntu.

Installed Google Cloud SDK via:

$ sudo curl https://sdk.cloud.google.com | sudo bash 
$ exec -l $SHELL 

Running "gcloud" works just fine.

Running "sudo gcloud" results in the following error:

sudo: gcloud: command not found

Oddly, when I installed Google Cloud SDK via apt-get, "sudo gcloud" works just fine. Unfortunately I cannot use Google Cloud SDK from apt-get as kubectl does not come with it and cannot be installed with the apt-get version.

Why would "gcloud" work and not "sudo gcloud"?

EDIT 5/21/2017: The following works if I manually set the path with the sudo command. I'd rather not do this every time though.

sudo env "PATH=$PATH" gcloud


Solution 1:[1]

First, here's a gcloud article where I found an answer for this, but I'm also going to try to summarize it here as the answer isn't totally straight forward.

If you install google-cloud-sdk using curl, by default the installation directory will be the user's home directory. Before completion it also asks "Modify profile to update your $PATH and enable shell command completion?" This will actually update the logged in user profile not the sudoers file or root user profile.

If you install gcloud manually (e.g. via a tar/gz file) as your individual user and not root, the process only adds gcloud to your users bashrc and subsequently your PATH variable. This means it is never added to your root PATH variable and isn't available to the sudo command.

When you run something such as:sudo env "PATH=$PATH" gcloud you are effectively grabbing your path (which includes the full path to gcloud) and temporarily assigning it to the root path. This then allows gcloud to work with sudo as it exists in the root PATH variable.

A few ways of getting around this include:

JUST PICK ONE OF THESE SOLUTIONS! (There is no need to do all of them.)

1) Try re-installing gcloud via aptitude (recommended, but not applicable in your case):

sudo apt-get install google-cloud-sdk

This should add gcloud to your sudoers path in the process. Check here for the full process if the command does not work.

2) Manually add the gcloud path to the sudoers secure_path variable:

sudo vi /etc/sudoers

Verify the installation directory for google-cloud-sdk and append your path to the end of the secure_path variable as below:

secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin:{YOUR_PATH_TO_GCLOUD}"

In your case, since you installed with sudo curl, this would be:

secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin:/home/<user-name>/google-cloud-sdk/bin"

3) Add the Path of gcloud to your /etc/environment file:

Here instead of your sudoers file you're editing your /etc/environment file. (Honestly, I wouldn't recommend this as it seems overkill).

4) Symlink to existing path directories:

You might also look to symlinking gcloud into one of the existing directories in the path, but I've gotten far enough to figure out which would be best. I'll update my answer with any feedback I get here.

Solution 2:[2]

I got it working by applying those 2 changes:

  1. Stop the VM instance and edit it to select "give full access to all APIs", then restart.

  2. In the terminal, as root user, run: apt-get install google-cloud-sdk

Solution 3:[3]

While installing Just modify your rc path with your shell, by default it takes root.

as like in bold line

enter a path to an rc file to update, or leave blank to use [/root/.zshrc]: /home/one/.zshrc

Backing up [/home/one/.zshrc] to [/home/one/.zshrc.backup]. [/home/one/.zshrc] has been updated

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 robinCTS
Solution 2 agenis
Solution 3 Bhuwan Chaudhary