bash: terraform: command not found, export $PATH unresolved

All my efforts to run terraform(exe) from command line in Ubuntu are unsuccessful

~/tectonic_1.7.9-tectonic.2$ terraform init ./platforms/metal
terraform: command not found

I changed ~/.profile file

PATH="$HOME/bin:$HOME/.local/bin:$HOME/bin/tectonic_1.7.9-tectonic.2:$PATH"

and latter sym link

/usr/bin# ls -l terraform
lrwxrwxrwx 1 root root 39 dec  6 16:29 terraform -> /home/milenko/tectonic_1.7.9-tectonic.2

I don't understand what is wrong.If I try what Asere suggested

ln -s /home/milenko/tectonic_1.7.9-tectonic.2/terraform /usr/bin/terraform
ln: failed to create symbolic link '/usr/bin/terraform/terraform': File exists

Solution 1:

First the fix, and then the explanation:

ln -snf /home/milenko/tectonic_1.7.9-tectonic.2/terraform /usr/bin/terraform
# or equivalently:
#rm /usr/bin/terraform
#ln -s /home/milenko/tectonic_1.7.9-tectonic.2/terraform /usr/bin/terraform

Currently you have /usr/bin/terraform pointing to /home/milenko/tectonic_1.7.9-tectonic.2.

Which is not correct, because the terraform binary is in /home/milenko/tectonic_1.7.9-tectonic.2/terraform.

You need to make /usr/bin/terraform point to /home/milenko/tectonic_1.7.9-tectonic.2/terraform.

Let's understand what's happening here:

ln -s /home/milenko/tectonic_1.7.9-tectonic.2/terraform /usr/bin/terraform
ln: failed to create symbolic link '/usr/bin/terraform/terraform': File exists

Why does the command fail? Since /usr/bin/terraform exists, and it points to /home/milenko/tectonic_1.7.9-tectonic.2, the above comment will not try to replace /usr/bin/terraform, but try to create /usr/bin/terraform/terraform. And since /usr/bin/terraform points to /home/milenko/tectonic_1.7.9-tectonic.2, /usr/bin/terraform/terraform already exists, it's actually the same thing as /home/milenko/tectonic_1.7.9-tectonic.2/terraform. And so the file exists, and the command fails.

One solution is to remove the incorrect symlink /usr/bin/terraform and then re-run ln -s /home/milenko/tectonic_1.7.9-tectonic.2/terraform /usr/bin/terraform.

Another solution is to add the -f and -n flags to ln, to force replace the symlink:

ln -snf /home/milenko/tectonic_1.7.9-tectonic.2/terraform /usr/bin/terraform

Solution 2:

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.