homebrew

Installing Multiple Versions of Terraform with Homebrew

At Gruntwork, we work with a number of customers and environments and find it’s necessary to switch among multiple versions of Terraform…
Installing Multiple Versions of Terraform with Homebrew
Matt Calhoun
Published May 22, 2019

At Gruntwork, we work with many customers and environments and find it’s necessary to switch among multiple versions of Terraform daily.

I happen to work on a Mac and find the Homebrew project to be an invaluable tool to help me install software. The biggest complaint I have about Homebrew, though, is that they don’t make it easy to install older versions of software.

Luckily, through some experimentation, I was able to come up with the procedure outlined below to use Homebrew to manage multiple versions of Terraform and to be able to switch among them easily.

[NOTE: YMMV with this particular workaround and I don’t know if this is ‘officially supported’ by the Homebrew project]

Let’s start by cloning the Homebrew project to our local machine.

$ git clone git@github.com:Homebrew/homebrew-core.git
$ cd homebrew-core

Next, find the Git commit id that added the version of Terraform we’re interested in installing. For this example, we’ll look to install Terraform version 0.11.8.

$ git log master -- Formula/terraform.rb
commit 49017817fb34b76fdc81f33665fdd7661fe5e558 (HEAD)
Author: BrewTestBot <homebrew-test-bot@lists.sfconservancy.org>
Date:   Tue Oct 23 23:17:42 2018 +0000
terraform: update 0.11.10 bottle.
commit 252d11cbf0d63aad22d043ef490b996b7a1c115c
Author: James Gregory <james@jagregory.com>
Date:   Wed Oct 24 09:28:21 2018 +1100
terraform 0.11.10
Closes #33324.
Signed-off-by: commitay <commitay@users.noreply.github.com>
commit 12c25cb2fc4b51bd0271f5d1fd00c6f500863966
Author: BrewTestBot <homebrew-test-bot@lists.sfconservancy.org>
Date:   Sat Oct 20 02:56:24 2018 +0000
terraform: update 0.11.9 bottle.
commit 7373f02c6f028904ac5c7f8d94290a44f911a736
Author: FX Coudert <fxcoudert@gmail.com>
Date:   Fri Oct 19 22:03:27 2018 +0200
terraform 0.11.9
Closes #33186.
Signed-off-by: commitay <commitay@users.noreply.github.com>
commit 3507fce2ba1f36cc371fe888fd093bf5aa79981d
Author: BrewTestBot <homebrew-test-bot@lists.sfconservancy.org>
Date:   Mon Aug 20 10:47:28 2018 +0000
terraform: update 0.11.8 bottle.
<***remaining output truncated***>

We can see commit 3507fce2ba1f36cc371fe888fd093bf5aa79981d is the one that last updated 0.11.8, so we’ll use that commit.

$ git checkout 3507fce2ba1f36cc371fe888fd093bf5aa79981d
$ cd Formula
$ brew unpin terraform (I'll explain this further below)
$ brew unlink terraform
$ brew install terraform.rb

It’s as simple as that, and we now have Terraform 0.11.8 installed on our machine!

Now that we have multiple versions of Terraform installed, there are a few other commands you’ll want to get familiar with.

The brew switch command will allow you to switch among versions of Terraform that are installed on your system.

$ brew switch terraform 0.11.8
$ terraform --version
Terraform v0.11.8
$ brew switch terraform 0.11.14
$ terraform --version
Terraform v0.11.14

The brew pin command will prevent Homebrew from updating/upgrading your version of Terraform when you run the brew upgrade command. I would strongly suggest pinning Terraform because otherwise, the brew upgrade command will remove all older versions of Terraform from your system.

$ brew pin terraform
$ brew info terraform
terraform: stable 0.11.14 (bottled), HEAD [pinned at 0.11.14]
Tool to build, change, and version infrastructure
https://www.terraform.io/
Conflicts with:
tfenv (because tfenv symlinks terraform binaries)
/usr/local/Cellar/terraform/0.11.8 (6 files, 88.9MB) *
Poured from bottle on 2019-05-21 at 15:07:13
/usr/local/Cellar/terraform/0.11.14 (6 files, 42.4MB)
Poured from bottle on 2019-05-21 at 15:02:18
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/terraform.rb
==> Dependencies
Build: go ✔, gox ✔
==> Options
--HEAD
Install HEAD version
==> Analytics
install: 18,754 (30 days), 64,658 (90 days), 200,703 (365 days)
install_on_request: 17,871 (30 days), 61,462 (90 days), 189,133 (365 days)
build_error: 0 (30 days)

Finally, if you’d like to see all of the versions of Terraform installed on your system, the easiest way I’ve found is by looking at the Homebrew Cellar directory.

$ ls /usr/local/Cellar/terraform
drwxrwxr-x  88 matt  admin  2816 May 21 14:50 ..
drwxr-xr-x   4 matt  staff   128 May 21 15:02 .
drwxr-xr-x   8 matt  staff   256 May 21 15:07 0.11.14
drwxr-xr-x   8 matt  staff   256 May 21 15:09 0.11.8

I hope this(relatively) simple procedure helps you easily install and manage the various versions of Terraform (and other packages) on your system!