Installing Terraform on Ubuntu
Infrastructure as Code made easy
Want to manage your infrastructure with code? Terraform is what you need. It lets you define servers, databases, networks - basically everything - in configuration files and deploy them consistently.
We're installing it from HashiCorp's official repository so you always get the latest version and can update easily.
What You Need
- Ubuntu 20.04 or later
- Sudo privileges
- Internet connection
The Installation
Step 1: Update Package List
First, refresh your package database:
sudo apt-get update
Step 2: Install Required Tools
Install packages needed for adding repositories:
sudo apt-get install -y gnupg software-properties-common
Step 3: Add HashiCorp GPG Key
Download and install HashiCorp's signing key:
wget -O- https://apt.releases.hashicorp.com/gpg | \
gpg --dearmor | \
sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg > /dev/null
This verifies that packages actually come from HashiCorp.
Step 4: Verify the Key (Optional)
Check the key's fingerprint to make sure it's authentic:
gpg --no-default-keyring \
--keyring /usr/share/keyrings/hashicorp-archive-keyring.gpg \
--fingerprint
You can skip this if you trust your connection, but it's good practice.
Step 5: Add HashiCorp Repository
Add the official 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
The lsb_release -cs part automatically detects your Ubuntu version.
Step 6: Update Package List Again
Refresh to include the new repository:
sudo apt update
Step 7: Install Terraform
Now install Terraform:
sudo apt-get install terraform -y
Step 8: Verify Installation
Check that Terraform is installed:
terraform --version
You should see something like "Terraform v1.x.x".
All Commands Together
Here's the complete installation script:
sudo apt-get update
sudo apt-get install -y gnupg software-properties-common
wget -O- https://apt.releases.hashicorp.com/gpg | \
gpg --dearmor | \
sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg > /dev/null
gpg --no-default-keyring \
--keyring /usr/share/keyrings/hashicorp-archive-keyring.gpg \
--fingerprint
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-get install terraform -y
terraform --version
Why this method? Installing from HashiCorp's repo means you can easily update Terraform with sudo apt upgrade whenever there's a new version.
Enable Tab Completion
Make Terraform easier to use with autocomplete. For bash:
terraform -install-autocomplete
Then restart your shell or run:
source ~/.bashrc
Now you can press Tab to complete Terraform commands and arguments.
Quick Start
Ready to write some infrastructure code? Create a simple test file:
mkdir terraform-test && cd terraform-test
nano main.tf
Add this simple example:
terraform {
required_providers {
local = {
source = "hashicorp/local"
}
}
}
resource "local_file" "hello" {
content = "Hello, Terraform!"
filename = "${path.module}/hello.txt"
}
Initialize and apply:
terraform init
terraform plan
terraform apply
Type "yes" when prompted. Terraform will create a hello.txt file. Check it:
cat hello.txt
If you see "Hello, Terraform!" - congrats, everything works.
Pro tip: Store your Terraform state files remotely (S3, Terraform Cloud, etc.) for production use. Local state files are fine for learning, but not for real projects.
Update Terraform
Since you installed from the official repo, updating is easy:
sudo apt update
sudo apt upgrade terraform
That's It
Terraform is installed and ready to go. You can now start defining your infrastructure as code - whether it's AWS, Azure, GCP, or any of the hundreds of supported providers.
Happy terraforming! 🏗️
Comments
Post a Comment