Home Infrastructure as Code (IaC) Installing Terraform on Linux (Ubuntu) and Windows (WSL2 with Ubuntu)

Installing Terraform on Linux (Ubuntu) and Windows (WSL2 with Ubuntu)

430
0

Terraform is one of the most popular Infrastructure-as-Code (IaC) tools used by DevOps engineers and cloud architects. It lets us define our infrastructure as code, automate provisioning, and manage cloud resources consistently across environments.

In this post, we’ll learn how to install Terraform on Linux (Ubuntu) and Windows (WSL2 with Ubuntu) — two setups that share nearly identical steps but differ slightly in environment handling.

🧠 If you’re using Windows and have not set up WSL 2 yet, check out the post Getting Started with WSL2: Run Linux Inside Windows before continuing.


🧠 Why Terraform?

Terraform, developed by HashiCorp, allows us to define infrastructure using declarative configuration files. Instead of manually creating servers, networks, or databases in a console, you describe them as code.
When you run commands like:

terraform init
terraform plan
terraform apply

Terraform takes care of building, updating, or destroying resources as defined in your code.

Before you can do that, you need to install Terraform on your system. Let’s get started.


⚙️ Installing Terraform on Ubuntu (and WSL2 Ubuntu)

Whether you’re using native Ubuntu Linux or Windows with WSL2 Ubuntu, the installation process is the same. WSL2 provides a full Linux environment that behaves just like Ubuntu — meaning Terraform commands and configurations will run identically.


Step 1: Update your system

Always start by updating existing packages:

sudo apt update && sudo apt upgrade -y

This ensures your system has the latest software lists and fixes.


Step 2: Install required packages

Terraform’s installation uses HashiCorp’s repository, so you’ll need a few dependencies first:

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

Step 3: Add HashiCorp’s official GPG key and repository

There are two supported methods depending on your Ubuntu version.


🟩 Modern Method (Recommended for Ubuntu 22.04+, including 24.04 and newer WSL2 images)

Download and save HashiCorp’s GPG key:

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

Add the official HashiCorp repository:

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 to include the new repository:

sudo apt update

✅ This refreshes the package list to include the new repository.

🟨 Legacy method (works on older systems and older WSL2 images)

If your WSL2 Ubuntu image is still on 20.04 or an older 22.04 build, the older commands still work:

These steps add HashiCorp’s repository to your system so you can install and update Terraform securely:

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

Then update your repository list:

sudo apt update

Step 4: Install Terraform

Now simply install Terraform:

sudo apt install terraform -y

This will install the latest stable version.

You can confirm the repository is configured correctly with:

apt-cache policy terraform

Step 5: Verify the installation

Once installed, check that Terraform is working correctly:

terraform -version

You should see something like:

Terraform v1.13.4
on linux_amd64

(The exact version may vary)


🪟 Notes for Windows Users with WSL2

If you’re using Windows Subsystem for Linux 2, Terraform will run inside your Ubuntu environment — not your Windows PowerShell.
Here are a few quick pointers:

  • You don’t need to install Terraform on Windows separately. Everything runs inside Ubuntu on WSL2.
  • If you use Visual Studio Code, install the WSL extension so you can open folders from Ubuntu by running: code .
  • You can store your Terraform files either inside your Ubuntu home directory (/home/<username>) or in your Windows filesystem (e.g., /mnt/c/Projects/terraform). Just make sure paths are handled correctly when referencing local files.

🔁 Updating Terraform

Whenever you want to upgrade to a newer version:

sudo apt update && sudo apt upgrade terraform -y

Alternatively, to remove it entirely:

sudo apt remove terraform

🧰 Troubleshooting Common Issues

ProblemPossible Fix
terraform: command not foundThe package wasn’t installed correctly — rerun the installation or ensure you’re in WSL2 Ubuntu, not PowerShell.
GPG key errorRe-add the key using the commands in Step 3.
Permission deniedAdd sudo to installation commands or verify your user privileges.
VS Code “Terraform not found”Reopen the folder using the Remote – WSL extension in VS Code.

🚀 Next Steps

Now that Terraform is installed, you can:

  • Configure your first provider (like AWS, Azure, or Google Cloud)
  • Create a simple main.tf file defining infrastructure resources
  • Initialize your workspace with terraform init
  • Plan and deploy using terraform plan and terraform apply

✅ Summary

Installing Terraform on Ubuntu or Windows WSL2 with Ubuntu follows the same process — because under the hood, both are Linux environments.
You:

  1. Update your packages
  2. Add HashiCorp’s repository
  3. Install Terraform via apt
  4. Verify it with terraform -version

That’s it! You now have Terraform ready to automate your cloud deployments from the comfort of Linux — even if you’re on Windows.


✨ Pro Tip

If you plan to manage multiple Terraform versions (for example, for different projects), consider using a version manager like tfenv. It allows quick switching between versions with commands like:

tfenv install 1.8.2
tfenv use 1.8.2
Previous articleUnderstanding Linux Families: The Major Types Explained
Next articleInstalling Terraform on MacOS
Heartin Kanikathottu
As a seasoned Cloud and Security Architect, I’ve led transformative initiatives in key roles, including Vice President at Morgan Stanley, Principal Architect at Societe Generale, and Tech Lead & Cloud Security Architect at VMware, among others. I’m also an internationally published author with multiple books available on platforms like Amazon and O'Reilly. Notably, one of my books was recognized as the 8th best cloud computing book of all time in 2020, reflecting the impact of my contributions to the field. With over 15 professional certifications from providers such as Microsoft (Azure), Amazon (AWS), Oracle (Java), Pivotal (Spring), and IBM, I bring a wealth of expertise to my work. Academically, I hold dual Master’s degrees in Cloud Computing and Data Analytics. I’m passionate about sharing knowledge and mentoring others, which is why I actively speak at global technical forums such as Tech Opportunities Fest at Platform Calgary, Google's Kubernetes Meetup, Java User Group, Elasticsearch Meetup, and the Agile India Conference.

LEAVE A REPLY

Please enter your comment!
Please enter your name here