HashiCorp Terraform is an open-source Infrastructure as Code (IaC) software that facilitates infrastructure management and creation. The tool is extremely popular with DevOps teams as it allows them to define the resources via config files to automate infrastructure provisioning for disparate providers.
This guide will show you how to install Terraform on Windows, Linux, and macOS with examples.
Prerequisites
- A user account with admin or sudo privileges.
- wget and unzip installed for Linux and curl for macOS.
- Access to the terminal/command-line tool.
How to Install Terraform on Windows
Installing Terraform on Windows requires you to download the correct Terraform package, unpack, and execute it via the CLI. Follow the instructions below to ensure you do not miss any steps.
Download Terraform File for Windows
To find the latest version of Terraform for Windows:
1. Browse to the Download Terraform page.
2. Select the Windows tab under the Operating System heading. The latest version is preselected.
3. Choose the binary to download. Select 386 for 32-bit systems or AMD64 for 64-bit systems. Choose the download location for the zip file if the download does not start automatically.
4. Unzip the downloaded file. For example, use the C:\terraform path. Remember this location so you can add the path to the environment variables.
Add Terraform Path to System Environment Variables
To add the Terraform executable to the system’s global path:
1. Open the start menu, start typing environment and click Edit system environment variables. The System Properties window opens.
2. Click the Environment Variables… button.
3. Select the Path variable in the System variables section to add terraform for all accounts. Alternatively, select Path in the User variables section to add terraform for the currently logged-in user only. Click Edit once you select a Path.
4. Click New in the edit window and enter the location of the Terraform folder.
5. Click OK on all windows to apply the changes.
Verify Windows Terraform Installation
To check the Terraform global path configuration:
1. Open a new command-prompt window.
2. Enter the command to check the Terraform version: terraform -version
terraform -version
The output shows the Terraform version you downloaded and installed on your Windows machine.
How to Install Terraform on Linux
There are two methods to install Terraform on Linux. The primary method is using the Terraform zip that contains the executable you can unpack anywhere on your system. The other method is using the HashiCorp repository for your Linux distribution.
Install Terraform on Linux Using Zip Archive
Follow the steps below to install Terraform on a Linux system using the downloaded zip file. The instructions are the same for all Linux distributions.
1. Browse to the Download Terraform page.
2. Select the Linux tab under the Operating System heading. The latest version is preselected.
3. Scroll down and right-click the Download button for your system’s architecture. In our case, it is AMD64.
4. Use the wget tool to download the file:
wget https://releases.hashicorp.com/terraform/1.3.5/terraform_1.3.5_linux_amd64.zip
5. Find a user directory in &PATH to put the Terraform binary in it:
echo $PATH
We will use /usr/local/bin.
6. Unzip the file to the directory you chose. Use the full file name with the extension when extracting the archive. Make sure to use the correct name for your architecture and the version you downloaded. For example, for version 1.3.5, enter:
sudo unzip terraform_1.3.5_linux_amd64.zip -d /usr/local/bin
The output shows the path where the extracted Terraform file is located.
To verify if the Terraform directory is in the selected location, use the list command. For example:
ls -l /usr/local/bin
7. Confirm the installation is successful by running a terraform command. For example, check the version with:
terraform -version
The output shows the Terraform version you installed.
Install Terraform on Linux Using Package Repository
Follow the steps for your Linux distribution to install Terraform from the official HashiCorp repository. The repository contains other HashiCorp packages that are not related to Terraform.
Install Terraform on CentOS/RHEL
To install Terraform from the HashiCorp repository on a CentOS system:
1. Install the yum-utils tools.
sudo yum install -y yum-utils
Note: For DNF-based distributions, use sudo dnf install -y dnf-plugins-core
.
2. Add the HashiCorp repository:
sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
3. Install Terraform using the yum package manager:
sudo yum -y install terraform
The output shows that Terraform is installed. You can double-check by running the terraform -version
command to see the installed Terraform version.
Install Terraform on Ubuntu/Debian
To install Terraform from the HashiCorp repository on an Ubuntu and Debian system:
1. Use wget and download the signing key to a new keyring:
wget -O- https://apt.releases.hashicorp.com/gpg | gpg --dearmor | sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg
2. Add the HashiCorp repository and find the distribution release codename for your OS:
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
3. Update the package information:
sudo apt update
4. Use apt to install Terraform from the new repository.
sudo apt install terraform
To verify the latest Terraform version is installed, run the terraform -version
command.
How to Install Terraform on MacOS
Installing Terraform on MacOS is possible via the zipped file with the Terraform binary and via the official HashiCorp repository. The repository contains other non-Terraform HashiCorp products you can install later.
Install Terraform on MacOS Using Zip Archive
The steps to install Terraform on macOS using the downloaded zip archive are similar to the instructions for Linux systems. Instead of wget, we’ll use curl on macOS.
1. Browse to the Download Terraform page.
2. Select the macOS tab under the Operating System heading. The latest version is preselected.
3. Right-click the Download button for your system’s architecture. In our case, it is ARM64.
4. Use curl to download the file:
curl https://releases.hashicorp.com/terraform/1.3.5/terraform_1.3.5_darwin_arm64.zip
5. Find a user directory in &PATH to put the Terraform binary in it:
echo $PATH
In our case, /usr/local/bin.
6. Use the full file name with the extension when extracting the archive. Make sure to use the correct name for your architecture and the version you downloaded. For version 1.3.5, enter:
sudo unzip terraform_1.3.5_darwin_arm64.zip -d /usr/local/bin
The output shows the path where the extracted Terraform file is located.
Use the list command to verify if the Terraform directory is in the selected location. For example:
ls -l /usr/local/bin
7. Confirm the installation is successful by running a terraform command:
terraform -version
The output shows the Terraform version you installed.
Install Terraform on MacOS from Repository
To install terraform on macOS using the official HashiCorp repository:
1. Add the Terraform repository:
brew tap hashicorp/tap
2. Install Terraform from the new repository by running:
brew install hashicorp/tap/terraform
3. Run the Terraform version command to check the installed version.
terraform -version
Conclusion
The steps in this guide showed you how to install Terraform on all major operating systems and how to verify the installation was successful.
Learn how to provision a Terraform infrastructure and utilize the full potential of this tool.