blog

How to Install Terraform on Ubuntu 24.04 Linux

Terraform, developed by HashiCorp, is an open-source Infrastructure as Code (IaC) tool that enables users to define and provision infrastructure using a high-level configuration language. As the DevOps movement continues to accelerate, tools like Terraform have become essential for automating cloud infrastructure management across providers like AWS, Azure, GCP, and more. If you’re using the latest Ubuntu 24.04 Linux distribution, installing Terraform is straightforward, and this guide will walk you through the process step-by-step.

Why Use Terraform?

Before diving into the installation, it’s worth understanding why Terraform stands out among other IaC tools. Here are some reasons why it’s widely adopted:

  • Provider Agnostic: Manage infrastructure across multiple cloud providers.
  • Declarative Syntax: Define the end state of your infrastructure, and let Terraform handle the rest.
  • Version Control: Configuration files can be treated as code and stored in Git repositories.
  • Modular Design: Reuse code through modules to improve maintainability.

With these advantages in mind, let’s get started on installing Terraform on Ubuntu 24.04.

Step 1: Update Your System

First, it’s always a good idea to update your package index and install any available upgrades. Open your terminal and run:

sudo apt update && sudo apt upgrade -y

This ensures that your system is in its optimal state before adding new software.

Step 2: Install Required Packages

Next, install the necessary packages that will help you add new repositories and fetch files securely:

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

Step 3: Add the HashiCorp GPG Key

To verify the authenticity of the Terraform packages, you need to add HashiCorp’s official GPG key:

curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg

Step 4: Add the HashiCorp Repository

After adding the GPG key, add the HashiCorp repository to your system’s sources:


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

Update your package list once again to include the newly added HashiCorp repository:

sudo apt update

Step 5: Install Terraform

With the repository added, you can now install Terraform by running:

sudo apt install terraform -y

This command will install the latest stable version of Terraform available for Ubuntu 24.04.

Step 6: Verify the Installation

To ensure Terraform was installed successfully, you can check the version using:

terraform -version

You should see output like the following, which confirms Terraform is ready to be used:


Terraform v1.6.0
on linux_amd64

Step 7: Test Terraform with a Basic Configuration

Once installed, you might want to test it with a simple configuration. Create a directory and enter it:


mkdir terraform-test
cd terraform-test

Create a file named main.tf with the following content:


terraform {
  required_providers {
    random = {
      source = "hashicorp/random"
    }
  }
}

provider "random" {}

resource "random_pet" "my_pet" {
  length    = 2
  separator = "-"
}

Then, initialize and apply the configuration:


terraform init
terraform apply

This will generate a random name using the random provider, confirming that your Terraform installation and configuration are working as expected.

Troubleshooting Tips

If you encounter issues during installation, consider the following:

  • GPG errors: Ensure that you’ve correctly added HashiCorp’s GPG key.
  • Repository not found: Double-check the repository URL and your Ubuntu version codename (use lsb_release -cs).
  • Command not found: Confirm that /usr/local/bin is in your system’s PATH.

Conclusion

Installing Terraform on Ubuntu 24.04 is a smooth process with just a few terminal commands. Once set up, you’ll unlock the ability to manage infrastructure as code, enhance automation, and collaborate more effectively with your team. With its power and flexibility, Terraform is an indispensable tool in any DevOps toolkit.

Now that you’ve mastered the installation, why not explore Terraform modules or integrate it with popular CI/CD tools like GitHub Actions? The possibilities are vast—and your infrastructure will thank you.