Terraform is distributed as a tarball on Github. Check the latest release on Terraform releases page before downloading below.There are two methods from which we can install terraform on Ubuntu22.04|20.04 |18.04.
Method 1: Install Terraform from APT repository
Terraform team offers package repositories for Debian based Linux, which allow you to install Terraform using the apt package management tool or any other APT frontend.
First, install repository addition dependencies:
sudo apt update
sudo apt install software-properties-common gnupg2 curl
Now import repository GPG key
curl https://apt.releases.hashicorp.com/gpg | gpg --dearmor > hashicorp.gpg
sudo install -o root -g root -m 644 hashicorp.gpg /etc/apt/trusted.gpg.d/
With the key imported now add Hashicorp repository to your Ubuntu system:
sudo apt-add-repository "deb [arch=$(dpkg --print-architecture)] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
Expected execution output:
Repository: 'deb [arch=amd64] https://apt.releases.hashicorp.com jammy main'
Description:
Archive for codename: jammy components: main
More info: https://apt.releases.hashicorp.com
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
Hit:1 https://mirror.hetzner.com/ubuntu/packages jammy InRelease
Hit:2 https://mirror.hetzner.com/ubuntu/packages jammy-updates InRelease
Hit:3 https://mirror.hetzner.com/ubuntu/packages jammy-backports InRelease
Hit:4 https://mirror.hetzner.com/ubuntu/security jammy-security InRelease
Get:5 https://apt.releases.hashicorp.com jammy InRelease [12.0 kB]
Get:6 https://apt.releases.hashicorp.com jammy/main amd64 Packages [74.4 kB]
Fetched 86.4 kB in 1s (84.6 kB/s)
Reading package lists... Done
Now install terraform on your Ubuntu Linux system:
sudo apt install terraform
Check the version of terraform installed on your system
$ terraform --version
Terraform v1.3.7
on linux_amd64
Method 2: Install Terraform Manually
The primary distribution packages for Terraform are .zip archives containing single executable files that you can extract anywhere on your system
Ensure wget and unzip packages are installed on your Ubuntu system:
sudo apt install wget unzip -y
Then download the latest terraform archive.
TER_VER=`curl -s https://api.github.com/repos/hashicorp/terraform/releases/latest | grep tag_name | cut -d: -f2 | tr -d \"\,\v | awk '{$1=$1};1'`
wget https://releases.hashicorp.com/terraform/${TER_VER}/terraform_${TER_VER}_linux_amd64.zip
Once downloaded, extract the archive:
$ unzip terraform_${TER_VER}_linux_amd64.zip
Archive: terraform_1.3.7_linux_amd64.zip
inflating: terraform
This will create a terraform binary file on your working directory. Move this file to the directory/usr/local/bin
.
sudo mv terraform /usr/local/bin/
This will make the tool accessible to all user accounts.
$ which terraform
/usr/local/bin/terraform
Confirm the version installed
$ terraform version
Terraform v1.3.7
on linux_amd64