Terraform from HashiCorp is provided as a command-line tool that must first be installed locally before excution. It’s easy to install, as the full tool is contained within a single executable. This makes it easy to put terraform in any folder on your machine for use. Although, to make it executable from anywhere on your system it needs to be installed, like when you put an executable like this in the /usr/local/bin location. Then you can simply type terraform to execute it from within any folder on your system. This article will take a look at the methods of installing Terraform locally on both macOS and Linux.

Bash Script to Install Latest Version of Terraform on macOS and Linux

While package managers are great, there may be a time when you want to install Terraform yourself instead. You could go to the Terraform release page form HashiCorp, download the executable or your system, and place it in the /usr/local/bin folder. While this is fairly simple to do, I have written a Bash script that’s part of the Build5Nines Terraform Quickstart Templates project to automate this for you.

You can use the following command to simultaneously download and execute the script to install the latest release version of HashiCorp Terraform to your local /usr/local/bin folder:

curl -s https://raw.githubusercontent.com/Build5Nines/terraform-quickstart-templates/main/terraform-tips/install/install-terraform.sh | bash

This script will check for the latest release version of Terraform to install. It will ignore any alpha, beta, or rc version, and instead only install the latest stable release.

If you’re interested in reading the bash code for this script, here’s the direct link to the script within the Build5Nines Terraform Quickstart Templates project: https://github.com/Build5Nines/terraform-quickstart-templates/blob/main/terraform-tips/install/install-terraform.sh

After you run the install script, and Terraform is installed, you can run Terraform just as you normally would.

call 'terraform version' from the macOS terminal

Install Terraform on macOS using Homebrew

If you have Homebrew installed on macOS, then you can easily use Homebrew to install HashiCorp Terraform:

brew tap hashicorp/tap
brew install hashicorp/tap/terraform

You will likely have Homebrew installed if you’re using macOS. It’s a great package manager that really helps when installing all the various tools that DevOps Engineers and Site Reliability Engineers use on a daily basis.

Install Terraform on Linux

For Linux, there are several different package managers available depending on the Linux distribution you are using. When setting up Terraform deployment automation with your release automation process (with Azure DevOps Pipelines, Github Actions, or others) you may use the appropriate package manager for your system to install HashiCorp Terraform.

The most common Linux distribution you’ll probably be using is Ubuntu, so here’s the commands to use apt to install HashiCorp Terraform on Ubuntu Linux:

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

If you’re looking for help with using package managers to install HashiCorp Terraform on Linux, and you are choosing not to use my amazing install script (see above), then the Terraform documentation will be helpful.

Chris Pietschmann is a Microsoft MVP, HashiCorp Ambassador, and Microsoft Certified Trainer (MCT) with 20+ years of experience designing and building Cloud & Enterprise systems. He has worked with companies of all sizes from startups to large enterprises. He has a passion for technology and sharing what he learns with others to help enable them to learn faster and be more productive.
Microsoft MVP HashiCorp Ambassador

Discover more from Build5Nines

Subscribe now to keep reading and get access to the full archive.

Continue reading