Install Terraform DevOps Beginners Guide
Terraform is a DevOps tool that everyone needs to know since it is one of the most popular ones in use today with organizations. Let’s see how to install it and get up and running so you can start getting into DevOps.
Table of contents
Introduction to Terraform
Terraform is a tool for automating your infrastructure and it helps you to capture things inside code, unlike traditional infrastructure. The syntax it uses helps to do this since it is simple and it is declarative. It describes and makes it happen with the infrastructure. Let’s look at installing Terraform.
Preparing to Install Terraform on Windows
First, let’s understand how we install Terraform on Windows. You can use the GUI, or you can use the lesser-known winget command. I prefer the winget approach as it is less cumbersome and automatically pulls the binary from the package repository, but let’s look at both.
Understanding the Terraform Executable File for Windows
When you install Terraform in Windows you will be just copying down the single binary from Hashicorp to your machine. So the installation process is not difficult at all. You simply need to download the correct version of the Terraform binary for your system.
Step-by-Step Guide to Installing Terraform on Windows
To install Terraform on Windows, follow these steps:
- Download the appropriate Terraform zip file from HashiCorp’s official repository.
- Extract the Terraform executable file from the downloaded zip file.
- Place the extracted file in a suitable directory, like C:\Terraform.
- Add the Terraform directory to your system environment variables to make Terraform commands accessible from the command prompt.
To download the appropriate Terraform ZIP file from here: HashiCorp Developer. Choose the appropriate architecture for your Windows machine. Hashicorp distributes Terraform repository downloads in the ZIP format. The download defaults to the latest Terraform version.
Once you have the ZIP file, extract the ZIP file to reveal the EXE file.
You will then need to copy this to the directory in which you want to house Terraform. This is important as we will want to add a Path variable to reference the Terraform location so we don’t have to change the directory each time to run Terraform.
Launch the sysdm.cpl tool and navigate to Advanced > Environment variables > System variables > PATH
Once you have the PATH variable updated, you will need to relaunch your PowerShell or command prompt so the new variable takes effect. Then, you should be able to check to see if the terraform command is found:
terraform --version
Install Terraform on Windows using Winget
Many do not know, if you are using Windows installing Terraform can be accomplished using a very “linux like” experience installing Terraform on Windows using the winget command. Winget is a fantastic command line tool to install and update software in Windows 11, and it is native to the operating system.
Using winget it automatically installs the terraform package, using the extracted terraform file, and sets up the tool accordingly. One thing about this approach as well is you don’t have to install the environment variable to allow the terraform executable to be referenced properly.
You can see the terraform packages available for installation with winget using the command:
winget search terraform
To install Terraform using winget, we can use the below command:
winget install Hashicorp.Terraform
After installation, you can check the version of Terraform and that it has been installed correctly, using the command:
terraform --version
Terraform Installation on Debian and Ubuntu
- Add the official HashiCorp Linux repository to your package repositories using sudo apt-add-repository.
- Update your package list with sudo apt-get update.
- Install Terraform using sudo apt install terraform.
The above steps for Debian and Ubuntu can be accomplished using the following:
wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
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 install terraform
Pulling down the GPG key.
Adding the repository via the GPG key.
Below is running the command to install Terraform in Ubuntu Linux.
Installing Terraform on Red Hat, Fedora, and CentOS
For RPM-based Linux distributions, such as Red Hat, Fedora, and CentOS, you can use dnf or yum package managers. The process involves adding the HashiCorp repository and then installing Terraform.
For CentOS and RHEL:
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
sudo yum -y install terraform
For Fedora:
sudo dnf install -y dnf-plugins-core
sudo dnf config-manager --add-repo https://rpm.releases.hashicorp.com/fedora/hashicorp.repo
sudo dnf -y install terraform
Verifying Your Terraform Installation
After installation, like Windows, you can run terraform –version to see the installed version. This step ensures that the Terraform executable is correctly placed and that your path environment variable is properly set.
The shift to Immutable Infrastructure
Terraform allows you to make a shift towards immutable infrastructure. What does this mean? It is where changes are made by replacing entire configurations rather than modifying or upgrading existing infrastructure, as we have done with legacy systems in years past.
Terraform and Cloud Providers
Terraform supports most of the major cloud providers like AWS, Azure Cloud, and Oracle Cloud. This allows for integration and management of cloud infrastructure across different platforms. So, no matter which cloud or multi-clouds are used, Terraform can be at the center of your DevOps strategy.
Wrapping up
Getting Terraform up and running is not difficult at all since we have the binaries to bring down with Terraform from Hashicorp. So, it is usually just a matter of copying these down and having them in your path statement.