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!


Solution 1:

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.