简体   繁体   中英

Issues setting $PATH on Bash on Ubuntu on Windows (Linux Subsystem)

I am using the "Bash on Ubuntu on Windows" (Linux Subsystem) and want to add Terraform to my $PATH. Since Terraform can't be installed via apt-get, I did the following steps:

  1. Navigated to this directory, where I wanted to install Terraform:

    cd /usr/local

  2. In the above path, I used wget to download Terraform:

    wget https://releases.hashicorp.com/terraform/0.9.8/terraform_0.9.8_linux_amd64.zip

  3. Terraform successfully unzips! When I open the file in VIM it is all good:

    unzip terraform_0.9.8_linux_amd64.zip

  4. I then enter this command to check to see if the Terraform binary is accessible from the command line:

    terraform -version

However the following message gets returned:

terraform: command not found

This tells me that the Terraform downloaded location needs to be added to my $PATH.

  1. Already being logged in as the root user ("sudo su") I enter the following command to access ".profile":

vim ~/.profile

The following is already in this file, which I leave untouched:

 # ~/.profile: executed by Bourne-compatible login shells.

 if [ "$BASH" ]; then
   if [ -f ~/.bashrc ]; then
     . ~/.bashrc
   fi
 fi

 mesg n

Immediately below this text, I add the following, and successfully save the file using :wq! :

 export PATH=/usr/local/bin:$PATH
 export PATH=$PATH:/usr/local/terraform

6. I then again enter the following command to check to see if terraform is detected

terraform -version

Still the same "terraform: command not found" message is returned. I even tried closing out and starting a new command line session and even restarting my computer. Still no change.

Anyone have any ideas on how to resolve this? Again, note that I am using "Bash on Ubuntu on Windows" (Linux Subsystem). Any input would be appreciated!

The direct answer to your problem is at the end. But I think it will make more sense if you keep reading from here.

Before trying to add to PATH , I recommend to test a program first. In your case I would do like this:

wget https://releases.hashicorp.com/terraform/0.9.8/terraform_0.9.8_linux_amd64.zip
unzip terraform_0.9.8_linux_amd64.zip
./terraform

Notice the last line ./terraform . The zip file contains a single file, terraform , which now should be in the current directory, so I can run it with ./terraform . If it's executable. If it's not executable then confirm it:

ls -l terraform

And make it executable if needed:

chmod +x terraform

Now let's add it to PATH . But first, let's decide where to put this executable. /usr/local/bin seems a reasonable location. So let's move the terraform executable into that directory.

Usually /usr/local/bin is already on PATH , so you might not need to change anything. Now you can try your check, and there's a good chance it already works:

terraform -version

If it doesn't, then /usr/local/bin is not on the PATH . To add it, add this line in ~/.profile :

export PATH=$PATH:/usr/local/bin

Two things looked fundamentally wrong with your approach:

  1. Adding /usr/local/terraform to PATH . This is fishy, because the entries on PATH must be directories, and in your post nothing indicates that you created a directory at /usr/local/terraform .

    • You cd into /usr/local , and then unzip the zip file of terraform. The linked zip contains a single file named terraform , so /usr/local/terraform in your example should be a file.
    • If it is a file, then you could make it executable as terraform by adding to add to PATH its base directory. But adding /usr/local to PATH would not be a good idea. It's conventional to put binaries into /usr/local/bin , not directly into /usr/local
  2. You did not mention how you reloaded ~/.profile . After editing this file, the new commands you added do not get automatically executed in your current shell. They will get executed when you open a new shell. Or you could manually execute the added commands in the current shell.

点击下面的命令

export PATH=$PATH:/usr/local/bin

Jan, 2022 Update:

You better install Terraform again following The Official Terraform Website . But for example, to install Terraform for Ubuntu and Debian , we need to run 4 commands as shown below which is troublesome. Moreover each time we open a terminal, we need to run the 4 commands below which is more troublesome:

sudo apt-get update && sudo apt-get install -y gnupg software-properties-common curl
curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
sudo apt-get update && sudo apt-get install terraform

So I concatenate them above with "&&" as shown below so that we can run them above at once and it works fine. Just copy, paste and run the command below for Ubuntu and Debian :

sudo apt-get update && sudo apt-get install -y gnupg software-properties-common curl &&

curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add - &&

sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main" &&

sudo apt-get update && sudo apt-get install terraform

I searched a lot for the error "bash: terraform: command not found" but there aren't any easy solutions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM