TerraformPilot

Technology

Install Terraform on Ubuntu 22.04 & 24.04 with apt

Install Terraform on Ubuntu 22.04 and 24.04 LTS using the official HashiCorp apt repository. Covers GPG key setup, version pinning, upgrades, and verification.

LLuca Berton2 min read
Install Terraform on Ubuntu 22.04 & 24.04 with apt

Quick Install (TL;DR)

#
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
sudo apt update && sudo apt install terraform
terraform --version

Four commands and you're running the latest Terraform (1.12+) on Ubuntu 22.04 or 24.04 LTS. Read on for the GPG fingerprint check, version pinning, and upgrades.

Introduction

#

Terraform is a powerful open-source tool created by HashiCorp that allows you to define, provision, and manage infrastructure as code. This article provides a detailed guide on installing Terraform on Ubuntu Linux, ensuring you can manage your infrastructure effectively.

Ubuntu, known for its ease of use and robustness, is a popular choice for many developers and sysadmins. Pairing it with Terraform enhances your ability to manage infrastructure seamlessly. This guide installs the latest Terraform (1.12+) on Ubuntu 22.04 and 24.04 LTS using the official HashiCorp apt repository.

Prerequisites

#
  • Ubuntu Linux system
  • Internet connectivity
  • Sudo privileges

Installation Steps

#

1. Update and Install Dependencies

#

Before proceeding, update your package lists to ensure you have the latest versions available:

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

2. Add HashiCorp GPG Key

#

HashiCorp signs their packages with a GPG key. Add it to your system:

wget -O- https://apt.releases.hashicorp.com/gpg | gpg --dearmor | sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg

Verify the fingerprint for added security:

gpg --no-default-keyring --keyring /usr/share/keyrings/hashicorp-archive-keyring.gpg --fingerprint

3. Add HashiCorp Repository

#

Next, add the HashiCorp repository to your system:

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

4. Update Package Lists

#

After adding the new repository, update your package lists again:

sudo apt update

Output:

Hit:1 http://ports.ubuntu.com/ubuntu-ports mantic InRelease
Hit:2 http://ports.ubuntu.com/ubuntu-ports mantic-updates InRelease
Hit:3 http://ports.ubuntu.com/ubuntu-ports mantic-backports InRelease
Hit:4 http://ports.ubuntu.com/ubuntu-ports mantic-security InRelease
Get:5 https://apt.releases.hashicorp.com mantic InRelease [9,523 B]
Get:6 https://apt.releases.hashicorp.com mantic/main arm64 Packages [4,413 B]
Fetched 13.9 kB in 1s (18.7 kB/s)
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done

5. Install Terraform

#

Now, you can install Terraform:

sudo apt install terraform

Output:

Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following NEW packages will be installed:
  terraform
0 upgraded, 1 newly installed, 0 to remove and 45 not upgraded.
Need to get 24.4 MB of archives.
After this operation, 80.5 MB of additional disk space will be used.
Get:1 https://apt.releases.hashicorp.com mantic/main arm64 terraform arm64 1.7.2-1 [24.4 MB]
Fetched 24.4 MB in 13s (1,943 kB/s)
Selecting previously unselected package terraform.
(Reading database ... 101523 files and directories currently installed.)
Preparing to unpack .../terraform_1.7.2-1_arm64.deb ...
Unpacking terraform (1.7.2-1) ...
Setting up terraform (1.7.2-1) ...
Scanning processes...
Scanning linux images...
 
Running kernel seems to be up-to-date.
 
No services need to be restarted.
 
No containers need to be restarted.
 
No user sessions are running outdated binaries.
 
No VM guests are running outdated hypervisor (qemu) binaries on this host.

6. Verify Terraform Installation

#

To ensure Terraform is installed correctly:

terraform --version

This command should show the installed version of Terraform.

Terraform v1.7.2
on linux_arm64

7. Enable Terraform Autocomplete (Optional)

#

For an enhanced command-line experience, enable the autocomplete feature:

terraform -install-autocomplete

Restart your shell to activate the autocomplete functionality.

#

Frequently asked questions

#

How do I install Terraform on Ubuntu?

#

Add the HashiCorp apt repository and GPG key, then run sudo apt update && sudo apt install terraform. Verify with terraform --version. The full commands are in the Quick Install section above.

How do I install Terraform with apt?

#

Terraform is distributed through HashiCorp's official apt repository. After adding the repo and its signing key, sudo apt install terraform installs the latest release and keeps it updated through apt upgrade.

Does this work on Ubuntu 22.04 and 24.04?

#

Yes. The same apt repository and commands work on Ubuntu 22.04 LTS, 24.04 LTS, and other Debian-based distributions like Debian, Linux Mint, and Pop!_OS.

How do I fix "terraform: command not found" after installing?

#

Reopen your shell so the updated PATH takes effect, or run hash -r. If it persists, confirm the install succeeded with apt list --installed | grep terraform and that /usr/bin is on your PATH.

Hands-On Courses

#

Learn by doing with interactive courses on CopyPasteLearn:

Conclusion

#

You have now successfully installed Terraform on Ubuntu Linux. This setup will allow you to efficiently manage and automate your infrastructure. Regularly check for Terraform updates and explore its vast documentation to fully leverage its capabilities in your DevOps workflow.

#Terraform#HashiCorp#Infrastructure as Code#Ubuntu Linux#software installation

Share this article