Terraform is an open-source Infrastructure as Code (IaC) tool developed by HashiCorp. It allows you to define and provision infrastructure resources, such as virtual machines, networks, and storage using the configuration files.
With Terraform, you can automate the creation and management of your infrastructure across various cloud providers and on-premises environments, ensuring consistency and reproducibility.
In this blog post, I will show you how to install Terraform in Linux using the APT repository and manually.
Prerequisites
- A user account with sudo privileges.
- wget and unzip installed
You can follow the mentioned steps to install Terraform on Linux using the APT Repository:
STEP 1 – Update Package List
Open a terminal and run the following command to update the package list:
sudo apt update
STEP 2 – Install Software-Properties-Common
This package provides an abstraction for adding and removing software repositories, which will be useful to add the HashiCorp repository for Terraform.
sudo apt install software-properties-common
STEP 3 – Add HashiCorp Repository
Add the HashiCorp repository to your system using the add-apt-repository
command:
sudo add-apt-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
STEP 4 – Import HashiCorp GPG Key
Download and add the HashiCorp GPG key to your system to verify the authenticity of the HashiCorp packages:
wget -qO - https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
STEP 5 – Update the package List Again
Update the package list to include the HashiCorp repository
sudo apt update
STEP 6 – Install Terraform
Finally, install Terraform using the following command:
sudo apt install terraform
STEP 7 – Verify Installation
After the installation is complete, you can verify that Terraform was installed successfully by checking its version:
terraform version
You can follow the mentioned steps to install Terraform manually
STEP 1 – Download Terraform
Visit the Terraform website (https://www.terraform.io/downloads.html) and download the latest version of Terraform for Linux. The download will be a zip archive containing the Terraform binary.
TER_VER=$(curl -s https://api.github.com/repos/hashicorp/terraform/releases/latest | grep tag_name | cut -d: -f2 | tr -d \"\,\v | awk '{$1=$1};1')
sudo wget https://releases.hashicorp.com/terraform/${TER_VER}/terraform_${TER_VER}_linux_amd64.zip
STEP 2 – Extract the Archive
After downloading the Terraform zip archive, open a terminal window and navigate to the directory where the library is located. Use the following command to extract the contents of the archive:
sudo unzip terraform_*.zip
STEP 3 – Move Terraform Binary to a Directory in PATH
Move the Terraform binary to a directory that is included in your system’s PATH variable. This step allows you to run Terraform from any location in the terminal.
For example, you can move the binary to the /usr/local/bin
directory:
sudo mv terraform /usr/local/bin/
Note: You may need to use sudo
to have the necessary permissions to move the binary to the system-wide PATH.
STEP 4 – Verify the Installation
To verify that Terraform is installed correctly, open a new terminal window and type:
terraform version