How to install Terraform on Ubuntu 22.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 22.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.

Where do we use Terraform?

Terraform is used in many use cases such as:

  • Infrastructure as code. We use Terraform to automate and provision our infrastructure such as servers, databases, firewalls, etc. This helps to accelerate cloud adoption because manual provisioning is always slow and cumbersome.
  • Manage network infrastructure. Terraform is used to automate key networking tasks such as updating load balancer member pools.
  • For multi-cloud deployments. Terraform helps in deploying serverless functions such as AWS Lambda.

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.

To deploy infrastructure with Terraform you need to understand the following aspects:

  • Scope: You need to identify the infrastructure for your project in advance.
  • Author: Write the configuration manifest for your infrastructure.
  • Initialize: Install all the plugins needed by Terraform to manage your infrastructure.
  • Plan: Preview the changes Terraform will make to match your configurations.
  • Apply: Make the planned changes to suit your configs

What is good about Terraform is that it keeps track of your infrastructure in a state file. Terraform uses the state file to determine the changes to make in your infrastructure in order to match your configurations.

Install Terraform on Ubuntu 22.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 install any packages because Ubuntu 22.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 the Hashicorp GPG key into our system. We are going to install using the curl command we installed earlier.

curl -fsSL | sudo apt-key add -

4. Add Hashicorp repository to Ubuntu 22.04

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

sudo apt-add-repository "deb [arch=amd64] $(lsb_release -cs) main"

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

Repository: 'deb [arch=amd64] jammy main'
Archive for codename: jammy components: main
More info:
Adding repository.
Press [ENTER] to continue or Ctrl-c to cancel.
Adding deb entry to /etc/apt/sources.list.d/archive_uri-https_apt_releases_hashicorp_com-jammy.list
Adding disabled deb-src entry to /etc/apt/sources.list.d/archive_uri-https_apt_releases_hashicorp_com-jammy.list
Get:1 jammy InRelease [270 kB]
Hit:2 jammy-updates InRelease                                                                        
Get:3 jammy InRelease [10.3 kB]                                                                          
Hit:4 jammy-backports InRelease                                                                      
Hit:5 main InRelease                    
Hit:6 jammy-security InRelease           
Get:7 jammy/main amd64 Packages [57.7 kB]
Fetched 338 kB in 7s (48.3 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 22.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

The sample output will be like this;

Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following NEW packages will be installed:
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 19.9 MB of archives.
After this operation, 62.9 MB of additional disk space will be used.
Get:1 jammy/main amd64 terraform amd64 1.2.3 [19.9 MB]
Fetched 19.9 MB in 0s (91.7 MB/s)
Selecting previously unselected package terraform.
(Reading database ... 93711 files and directories currently installed.)
Preparing to unpack .../terraform_1.2.3_amd64.deb ...
Unpacking terraform (1.2.3) ...
Setting up terraform (1.2.3) ...
Scanning processes...                                                                                                                        
Scanning candidates...                                                                                                                       
Scanning linux images...  

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

$ terraform -help init

Usage: terraform [global options] init [options]

  Initialize a new or existing Terraform working directory by creating
  initial files, loading any remote state, downloading modules, etc.

  This is the first command that should be run for any new or existing
  Terraform configuration per machine. This sets up all the local data
  necessary to run Terraform that is typically not committed to version

  This command is always safe to run multiple times. Though subsequent runs
  may give errors, this command will never delete your configuration or
  state. Even so, if you have important information, please back it up prior
  to running this command, just in case.

Check the version of installed Terraform with terraform -v command.

terraform -v 
Terraform v1.2.3

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

First, enable tab autocompletion

$ touch ~/.bashrc

Then install the auto-completion package with the following command;

$ terraform -install-autocomplete

Restart your shell for changes to take effect.

Installing Terraform using the downloaded zip file

Another hustle-free way to install Terraform is to download the zip file and extract it to your preferred location. Let’s see how we can do that.

  1. Go to Terraform download section and hit download amd64 to download a zip file.
  2. To unzip the file install unzip using the following command apt install unzip.
  3. To unzip the file use unzip
  4. Move the unzipped file to /usr/local/bin directory using this command mv terraform /usr/local/bin
  5. Terraform is now installed, you can check the version with terraform -v command.

To initialize a project in Terraform we use, terraform init command

terraform init

To provision a container, use terraform apply command

terraform apply

To stop a container use terraform destroy command


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

About Kipkoech Sang

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 Kipkoech Sang →

Leave a Reply

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