Automation

Install Terraform DevOps Beginners Guide

DevOps beginners guide on how to install Terraform in Windows and Linux manually and using the command line, including winget

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.

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:

  1. Download the appropriate Terraform zip file from HashiCorp’s official repository.
  2. Extract the Terraform executable file from the downloaded zip file.
  3. Place the extracted file in a suitable directory, like C:\Terraform.
  4. 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.

Download the latest version of terraform
Download the latest version of terraform

Once you have the ZIP file, extract the ZIP file to reveal the EXE file.

Extract the terraform executable
Extract the terraform executable

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.

Copy the terraform install to the target folder location
Copy the terraform install to the target folder location

Launch the sysdm.cpl tool and navigate to Advanced > Environment variables > System variables > PATH

Set your path environment variable to the terraform install location
Set your path environment variable to the terraform install location

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
Check the terraform version information to confirm the installation
Check the terraform version information to confirm the installation

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
Search for terraform using winget
Search for terraform using winget

To install Terraform using winget, we can use the below command:

winget install Hashicorp.Terraform
Install terraform using winget
Install terraform using winget

After installation, you can check the version of Terraform and that it has been installed correctly, using the command:

terraform --version
Check the terraform version from the command line
Check the terraform version from the command line

Terraform Installation on Debian and Ubuntu

  1. Add the official HashiCorp Linux repository to your package repositories using sudo apt-add-repository.
  2. Update your package list with sudo apt-get update.
  3. 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.

Download the gpg key to install terraform
Download the gpg key to install terraform

Adding the repository via the GPG key.

Add the terraform install repository
Add the terraform install repository

Below is running the command to install Terraform in Ubuntu Linux.

Installing terraform in ubuntu linux
Installing 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.

Terraform version command on linux
Terraform version command on linux

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.

Subscribe to VirtualizationHowto via Email 🔔

Enter your email address to subscribe to this blog and receive notifications of new posts by email.



Brandon Lee

Brandon Lee is the Senior Writer, Engineer and owner at Virtualizationhowto.com, and a 7-time VMware vExpert, with over two decades of experience in Information Technology. Having worked for numerous Fortune 500 companies as well as in various industries, He has extensive experience in various IT segments and is a strong advocate for open source technologies. Brandon holds many industry certifications, loves the outdoors and spending time with family. Also, he goes through the effort of testing and troubleshooting issues, so you don't have to.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.