How to install Terraform on Ubuntu 20.04

Terraform is an open-source infrastructure as code software tool that provides a consistent CLI workflow to manage hundreds of cloud services. Terraform codifies cloud APIs into declarative configuration files.

In this tutorial, we are going to learn how to install Terraform on Ubuntu 20.04.

Infrastructure as code (IAC) tools allow you to manage your infrastructure with a configuration file rather than through a graphical interface. IAC allows you to build, change and manage your infrastructure in a safe, consistent, and repeatable way by defining configurations that you can version, reuse and share.

Advantages of Using Terraform

  • Terraform can manage infrastructure on multiple cloud platforms.
  • The human-readable form helps you write infrastructure code quickly.
  • Terraforms state helps you to track resource changes throughout your deployments.
  • You can commit your configurations to version control to safely collaborate on infrastructure.

Related Articles

Prerequisites

  • Have Ubuntu 20.04 up and running
  • Make sure you have basic knowledge of terminal.
  • Have a user with sudo privileges

Install Terraform on Ubuntu 20.04

Before we can install Terraform, make sure your system is up to date, You have GnuPG, software-properties-common, and curl packages installed.

1. Run system updates

Begin by updating your system repositories to make them up to date. Use the following command to run system updates.

$ sudo apt update && apt upgrade -y 

2. Install Terraform Package dependencies

Install the following dependencies, software-properties-common, GnuPG, and curl on your system using the following command.

$ sudo apt install -y gnupg software-properties-common curl

It will not installed any packages because Ubuntu 20.04 comes preinstalled with the following packages. So what you can do is just update them.

3. Add Hashicorp GPG key

Next in line is to add Hashicorp GPG key into our system. We are going to install using the curl command we installed earlier.

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

4. Add Hashicorp repository to Ubuntu 20.04

Now we need to tell our system where to get installation packages from and that is by adding Hashicorp repository to our system repositories. To add repositories, we need to use the following command.

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

Check what has been added in your system. See below output

Output
Get:1 https://apt.releases.hashicorp.com focal InRelease [9495 B]
Hit:2 http://mirrors.digitalocean.com/ubuntu focal InRelease                                                    
Hit:3 https://repos-droplet.digitalocean.com/apt/droplet-agent main InRelease                    
Hit:4 http://mirrors.digitalocean.com/ubuntu focal-updates InRelease                             
Hit:5 http://mirrors.digitalocean.com/ubuntu focal-backports InRelease     
Hit:6 http://security.ubuntu.com/ubuntu focal-security InRelease
Get:7 https://apt.releases.hashicorp.com focal/main amd64 Packages [41.1 kB]
Fetched 50.6 kB in 1s (65.6 kB/s) 
Reading package lists... Done

Lastly is to update our system repositories again for changes to take effect.

$ sudo apt update 

5. Install Terraform on Ubuntu 20.04

After we have met all the requirements, we can now run terraform installation with ease. Run the following command on your terminal.

$ sudo apt install terraform

Sample output will be like this;

Output
The following NEW packages will be installed:
  terraform
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 18.7 MB of archives.
After this operation, 62.0 MB of additional disk space will be used.
Get:1 https://apt.releases.hashicorp.com focal/main amd64 terraform amd64 1.1.2 [18.7 MB]
Fetched 18.7 MB in 0s (61.8 MB/s)
Selecting previously unselected package terraform.
(Reading database ... 94647 files and directories currently installed.)
Preparing to unpack .../terraform_1.1.2_amd64.deb ...
Unpacking terraform (1.1.2) ...
Setting up terraform (1.1.2) ...

Now that we have installed Terraform, we can verify the installation with the following command;

$ terraform -help init

Let’s install a package using Terraform to see if it’s actually working.

First let’s enable tab autocompletion

$ touch ~/.bashrc

Then install auto-completion package with the following command;

$ terraform -install-autocomplete

Restart your shell for changes to take effect.

Conclusion

We have successfully installed Terraform on Ubuntu 20.04. Continue learning more about Terraform using Its rich documentation.

About Mason Kipward

I am a technology enthusiast who loves to share gained knowledge through offering daily tips as a way of empowering others. I am fan of Linux and all other things open source.
View all posts by Mason Kipward →

Leave a Reply

Your email address will not be published. Required fields are marked *