How to Install Terraform on Ubuntu 20.04

Hi everyone, in this article, we will be exploring two different ways of installing Terraform on Ubuntu 20.04 and an additional method for automation using a script. The good thing about exploring these methods is that they follow the documented processes on Terraform documentation and can be applied to other Ubuntu versions.

Installling Terraform on Ubuntu 20.04

As mentioned earlier in the introduction section, Terraform has documented two methods of installing it which are:

  1. Installation as a binary package that falls in the manual installation category.

  2. Installation with package managers.

Then, I will add a bonus method of using a bash script that will help you install Terraform in under two minutes with minimal typing.

Steps for Method 1 (Manual Installation):

  1. On this install Terraform's official page, click on the Manual installation tab then, the pre-compiled binary option.

  2. The pre-compiled binary option gives you a set of instructions, one of which you are to click on the appropriate package link. The link takes you to a page to get the package for different operating systems as a zip archive.

  3. Since we are installing on an Ubuntu 20.04 machine, click on the Linux tab. Then, click on the right architecture version for your Linux system. In this case, I am going with the AMD64 version. So, right-click on the Download option and copy the link to the address.

  4. On the Ubuntu terminal, use this command to download the zipped folder to your system.

     wget -c https://releases.hashicorp.com/terraform/1.5.1/terraform_1.5.1_linux_amd64.zip
    
  5. Use the ls command to confirm the download and unzip the file with the unzip command. However, you will likely have to install the unzip package first with the package manager.

     sudo apt install unzip -y
     unzip terraform_1.5.1_linux_amd64.zip
    
  6. To confirm that the previous step was executed, the ls command should give terraform as one of the directories. After this, use the echo $PATH command to check for available paths on your Linux machine and move the terraform folder into one of them. As directed by the documentation, we will move it to the /usr/local/bin folder.

     echo $PATH
     sudo mv terraform /usr/local/bin
    

    To test our installation, you can use any of these commands: terraform -help, terraform -version, e.t.c.

Steps for Method 2 (Package Manager):

  1. On the same Install Terraform page, click on the Linux tab to access the Linux/Debian instructions. Then, copy and paste the code lines one after the other on your Ubuntu terminal.

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

    This first command helps to update the apt package manager repository and thereafter, install some other packages required for our installation.

  2. The next step is to install the Hashicorp GPG key and add the repository to your system:

     wget -O- https://apt.releases.hashicorp.com/gpg | \
     gpg --dearmor | \
     sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg
    
     echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] \
     https://apt.releases.hashicorp.com $(lsb_release -cs) main" | \
     sudo tee /etc/apt/sources.list.d/hashicorp.list
    
  3. The next step is to update the apt package manager and install Terraform.

     sudo apt update
    
     sudo apt-get install terraform -y
    

    To test the installation, open a new terminal session and use any of these commands: terraform -help, terraform -version, e.t.c.

Bonus Method:

Finally, to make the second method even faster, I have a GitHub repository where you can access a script I have written to install Terraform. All you have to do is Clone the repository with:

git clone https://github.com/Adephumie/bash-scripts-for-automation.git

Then, change into the terraform-installation directory, and make the code executable by running this code line:

sudo chmod +x terraform.sh

The next thing is to run the code to install Terraform on the Ubuntu machine as follows.

./terraform.sh

Conclusion

And now, you have the two methods of installing Terraform on Ubuntu 20.04 and the bonus method using a bash script to install Terraform. Thank you.