Introduction
Terraform is an infrastructure-as-code (IaC) tool that was designed by HashiCorp. It can be used to create, modify and maintain infrastructure in a safe and efficient manner. It works with various service providers or custom solutions made in-house. Here, we will explain how to install Terraform on Ubuntu 24.04, 22.04 and 20.04 through Personal Package Archive (PPA).
Prerequisites
To get started, you’ll need the following:
- An Ubuntu system (24.04, 22.04, or 20.04 LTS).
- A user account with sudo privileges to execute administrative commands.
- An active internet connection to download necessary files.
Installation Steps
Step 1: System Update
Before installing Terraform, it’s important to ensure that your system is up-to-date. Use the following commands to update your package list and upgrade the system:
sudo apt update
sudo apt upgrade
Install curl
and software-properties-common
which are required to add repositories:
sudo apt install curl software-properties-common
Step 2: Adding the HashiCorp Repository
To add Terraform to your system, start with the HashiCorp GPG key:
wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
output:
insha@ip-10-13-33-129:~$ wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
--2024-04-28 07:06:32-- https://apt.releases.hashicorp.com/gpg
Resolving apt.releases.hashicorp.com (apt.releases.hashicorp.com)... 108.156.184.5, 108.156.184.19, 108.156.184.46, ...
Connecting to apt.releases.hashicorp.com (apt.releases.hashicorp.com)|108.156.184.5|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 3980 (3.9K) [binary/octet-stream]
Saving to: ‘STDOUT’
- 100%[========================================================>] 3.89K --.-KB/s in 0s
2024-04-28 07:06:32 (564 MB/s) - written to stdout [3980/3980]
insha@ip-10-13-33-129:~$
Then, add the 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
Step 3: Install Terraform
Now, install Terraform by updating your repositories and installing the package:
sudo apt update && sudo apt install terraform
Step 4: Verify the Installation
Check the installation by querying the Terraform version:
terraform version
The output should display the installed version, indicating a successful setup:
Terraform v1.8.2
on linux_amd64
Step 5: Starting Your Terraform Project
Kickstart your infrastructure automation:
- Create a project directory:
mkdir quadrect-terraform-project
cd quadrect-terraform-project
- Open a main configuration file to write your configurations:
vi main.tf
Step 6: Creating a Local File with Terraform
- Add a simple Terraform configuration to create a local file. Insert the following content into your
main.tf
file:
provider "local" {
# No configuration is needed for the local provider
}
resource "local_file" "example" {
content = "Hello from Quadrect Intotech Team."
filename = "${path.module}/output.txt"
}
- Initialize the Terraform project to set up the necessary plugins:
terraform init
- Preview the actions Terraform will take based on your configuration:
terraform plan
insha@ip-10-13-33-129:~/quadrect-terraform-project$ terraform plan
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
+ create
Terraform will perform the following actions:
# local_file.example will be created
+ resource "local_file" "example" {
+ content = "Hello from Quadrect Into tech ll."
+ content_base64sha256 = (known after apply)
+ content_base64sha512 = (known after apply)
+ content_md5 = (known after apply)
+ content_sha1 = (known after apply)
+ content_sha256 = (known after apply)
+ content_sha512 = (known after apply)
+ directory_permission = "0777"
+ file_permission = "0777"
+ filename = "./output.txt"
+ id = (known after apply)
}
Plan: 1 to add, 0 to change, 0 to destroy.
─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Note: You didn't use the -out option to save this plan, so Terraform can't guarantee to take exactly these actions if you run "terraform apply" now.
Note: You didn't use the -out option to save this plan, so Terraform can't guarantee to take exactly these actions if you run "terraform apply" now.
insha@ip-10-13-33-129:~/quadrect-terraform-project$
- Apply the configuration to create the file:
terraform apply
Confirm the actions by typing yes
when prompted.
terraform apply
output:
insha@ip-10-13-33-129:~/quadrect-terraform-project$ terraform apply
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
+ create
Terraform will perform the following actions:
# local_file.example will be created
+ resource "local_file" "example" {
+ content = "Hello from Quadrect Into tech ll."
+ content_base64sha256 = (known after apply)
+ content_base64sha512 = (known after apply)
+ content_md5 = (known after apply)
+ content_sha1 = (known after apply)
+ content_sha256 = (known after apply)
+ content_sha512 = (known after apply)
+ directory_permission = "0777"
+ file_permission = "0777"
+ filename = "./output.txt"
+ id = (known after apply)
}
Plan: 1 to add, 0 to change, 0 to destroy.
Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.
Enter a value: yes
local_file.example: Creating...
local_file.example: Creation complete after 0s [id=605bd7e2e8c341ec5b6924db21f7935e208f37ae]
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
insha@ip-10-13-33-129:~/quadrect-terraform-project$ ls
main.tf output.txt terraform.tfstate
insha@ip-10-13-33-129:~/quadrect-terraform-project$ cat output.txt
Hello from Quadrect Into tech ll.insha@ip-10-13-33-129:~/quadrect-terraform-project$
Troubleshooting: HashiCorp Repository Issue on Ubuntu 24.04
If you see a 404 Not Found
error for the HashiCorp repository on Ubuntu 24.04, apply this workaround:
- Remove the incorrect list file:
sudo rm /etc/apt/sources.list.d/hashicorp.list
- Use “jammy” instead of the release code in the repository addition:
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com jammy main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
- Update and install Terraform:
sudo apt update
sudo apt install terraform
Conclusion
With Terraform successfully installed on your Ubuntu system, you are well-equipped to streamline and automate your infrastructure management tasks. Whether you’re dealing with Ubuntu 24.04, 22.04, or 20.04 LTS, this guide from Quadrect Info Tech has shown you the necessary steps to get started and also how to handle potential issues with third-party repository support. We encourage you to explore further Terraform functionalities and integrate them into your projects to fully leverage this powerful tool. For more insightful tutorials and tech tips, keep visiting Quadrect Info Tech.
Happy Infra-structuring from team Quadrect!