How to Download and Install Terraform on Windows, Linux & macOS

How to download and install Terraform - tutorial

When I started learning Terraform, the first thing I struggled with was getting proper instructions on how to install Terraform on my system or any cloud server which I was using. There are many resources available online that guide you through the installation steps, and of course Terraform docs also have a very good installation guide.

In this guide you will find all the necessary steps in one place regardless of whether you are installing Terraform on Windows, Linux or macOS.

Prerequisites

Before you start the installation process, there are some prerequisites which you will need to fulfill:

  • A Windows PC if you are installing on Windows
  • A Linux system (virtual or local) if you are installing on Linux (in this post, I am using Ubuntu 20.04 to demonstrate the steps)
  • Some basic CLI knowledge

In this post, I will be covering the installation steps for Terraform on three different operating systems. You can follow the steps based on the system you are using. These are the high level topics I will be covering in this post: 

  • Installing Terraform on Windows
  • Installing Terraform on Linux (Ubuntu) using Package repository
  • Installing Terraform on Linux using Zip archive file
  • Installing Terraform on macOS

How to Install Terraform on Windows

Let’s start with the basics.

Download the installation file

1. Navigate to the Terraform download page (https://www.terraform.io/downloads.html). It should list out Terraform downloads for various platforms. Navigate to the Windows section and download the respective version. For this example I am downloading V1.1.0.

downloading V1.1.0 from Terraform downloads on Windows

2. It will download a zip file. Create a folder on the C drive as C:/terraform. Download the zip file in this folder. Unzip the file to extract the .exe file.

C:/terraform

Update Path Environment Variable

1. Next open the Start menu and search for Environment variables. Open the Environment variables settings page.

Update Path Environment Variable

2. On the Environment variables edit page, open the Path variable as shown below:

Edit environment variables

3. On the opened Path pop up, click New and add the Terraform download folder. This is the folder where the zip file was downloaded and unzipped (C:/terraform).

add the Terraform download folder

4. Click OK on the above window to save the Path variable addition. If needed, restart your system to apply the variable changes.

5. Open a Command prompt and run this command to verify Terraform is installed successfully:

verify if Terraform is installed successfully

Verify if the Correct Version of Terraform Is Installed

Just like below:

correct version of Terraform

How to Install Terraform on Linux

Here I am taking an example of installing on Ubuntu 20.04 to demonstrate the Linux installation. There are two paths to install on Linux. We’ll go through each path below. 

Installation Using Package Repositories on Linux (Ubuntu)

First, I will go through the steps to install Terraform using the Linux package repository. Follow these steps for Ubuntu. For other Linux versions steps are similar and will depend on the specific package repositories. Log in to the system and run these steps as root for sudo access:

1. Make sure the system is up-to-date and install these packages which are needed for further steps:

sudo apt-get update && sudo apt-get install -y gnupg software-properties-common curl

2. Next add the Hashicorp GPG key needed by the repository:

curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -

3. Then, add the official repository for HashiCorp for Linux:

sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"

4. Now that we have added the repo to the list, let’s update to add the relevant repository content:

sudo apt-get update

5. Once update, run this command to install Terraform:

sudo apt-get install terraform

That should complete the installation. To verify the installation, run this and verify the output:

verify the installation

To verify the version installed, run: terraform –version

correct version of Terraform

Install Using Package repositories on Linux

Next, we will see the method for installing Terraform using the downloaded zip file. Log in to the system and run these commands as root:  

1. Before starting the installation, get the download link from the Terraform website. Navigate to https://www.terraform.io/downloads.html and move to the specific OS version. Here we are looking for Linux:

download link for Linux from the Terraform site

2. Copy the link for the respective download:

Copy the link for the Linux download

3. Now, log in to the Ubuntu system. Make sure Wget and Unzip are installed. If not, install the same:

sudo apt-get install wget unzip

4. Download the Terraform zip file by running the following command. Put the URL copied earlier in the command for download:

wget
wget https://releases.hashicorp.com/terraform/1.1.0/terraform_1.1.0_linux_amd64.zip

5. The zip file should download:

The downloaded zip file

6. Unzip the zip file in the current folder:

unzip terraform_1.1.0_linux_amd64.zip
unzip terraform_1.1.0_linux_amd64.zip

7. The Terraform executable should get unzipped in the current folder:

terraform executable

8. Move the Terraform executable file to /usr/bin folder:

sudo mv terraform /usr/local/bin/

9. That should complete the Terraform installation. Verify the installation by verifying the version:

Verify the installation on Linux

How to Install Terraform on macOS

Next, we will go through the steps to install Terraform on macOS. 

1. Log in to the system and create a folder for the Terraform executable:

sudo mkdir -p /opt/terraform

2. Navigate to the new folder:

cd /opt/terraform

3. From the Terraform downloads website, copy the macOS Terraform zip file download link:

copy the Mac OS terraform zip file download link

4. Download the zip file in the previously created folder:

sudo curl -O <copied_url>

5. Unzip the Terraform files in the current folder:

sudo unzip <zip_file_name>

The unzipped files will be in the current folder.

6. Add Terraform to the PATH. Navigate to the home folder and execute this command to open and edit the bash profile:

sudo vi ~/.bash_profile

Add these two lines on the opened file:

PATH="/opt/terraform:${PATH}"
export PATH

7. Execute the file for the path changes to get into effect:

source ~/.bash_profile

8. Now that Terraform is installed, verify the Terraform version by running

terraform –version

This should show the Terraform version which is installed in the system.

That completes your installation of Terraform on Mac OS.

Next step? Import existing infrastructure into Terraform. See more here: How to Import Existing Infrastructure into Terraform (Tutorial with Examples)

Key Points

Terraform allows you to convert a whole cloud infrastructure to config or code files. These files can be used to spin up the entire infrastructure quickly and easily. It is an open source tool by Hashicorp. Anyone can use it to manage a cloud infrastructure on any of the cloud providers. 

In this post, we went through the steps to install Terraform on Windows, Linux, as well as macOS. I hope this helps you with your installations. And if you’re interested in how to upgrade Terraform to the latest version, see: Upgrading Terraform to the Latest Version

Terraform Management Made Easy

Spacelift effectively manages Terraform state, more complex workflows, supports policy as code, programmatic configuration, context sharing, drift detection, resource visualization and includes many more features.

Start free trial

Share this post

twitter logo