Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 365
user791134 Avatar asked Dec 03 '17 04:12

user791134


People also ask

How do you fix the Windows Subsystem for Linux optional component is not enabled please enable it and try again?

The Windows Subsystem for Linux optional component is not enabled: Open Control Panel -> Programs and Features -> Turn Windows Feature on or off -> Check Windows Subsystem for Linux or using the PowerShell cmdlet mentioned at the beginning of this article.

How do you set up a development environment with WSL?

This could also be done mixing the Windows dir command with the Linux grep command: dir | wsl grep git . Run a Windows tool directly from the WSL command line: <tool-name>.exe For example, to open your . bashrc file (the shell script that runs whenever your Linux command line is started), enter: notepad.exe . bashrc.

Does WSL need Hyper V?

While WSL 2 uses Microsoft's Hyper-V as a hypervisor under the hood to run the utility VM, it does not require you to enable Windows' Hyper-V role or feature; WSL works perfectly fine without it.


1 Answers

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.

like image 75
janos Avatar answered Nov 10 '22 01:11

janos