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

16,812

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.

Share:
16,812

Related videos on Youtube

AngryWhopper
Author by

AngryWhopper

Updated on June 04, 2022

Comments

  • AngryWhopper
    AngryWhopper over 1 year

    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
    
  • Adarsha Jha
    Adarsha Jha about 4 years
    this solution really helped @Gerik. Thanks !!
  • Tim
    Tim over 3 years
    I used method 2--manually add the gcloud path to the sudoers secure_path variable in /etc/sudoers. sudo gsutil now works as expected. Thanks!