Multiple versions of Terraform

By mcs94, Mon 08 July 2019, in category Macos

homebrew, macos, terraform

Terraform

At work we use Terraform extensively. Unfortunately, Hashicorp decided that version 0.12 of Terraform would introduce some significant changes. While great, they will force us to rewrite our code base. Of course being just a binary there should be no problem with having different versions of the Terraform for different projects right? Surely this will be easy to solve with Homebrew...

Turns out it's not quite as simple as I thought.

How I broke it

$ brew upgrade terraform

This upgraded Terraform from v 0.11.x to 0.12.3. Great, unless you suddenly realise you don't want 0.12.3 because it breaks everything targetted at 0.11.x.

Fixing it

After a bit of fudging around and dead ends it turns out you can have Homebrew install multiple versions of a package. I'm not sure this is offically supported and I am definitely not responsible for breaking your system.

To begin with we will need to clone the Homebrew project.

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

Next we need to checkout the commit for the version of Terraform we want. I was after 0.11.4.

$ git log master -- Formula/terraform.rb

commit 042c118f93b5c4a29db33e56ff2473d07bf17c44 (HEAD)
Author: BrewTestBot <homebrew-test-bot@lists.sfconservancy.org>
Date:   Fri May 17 07:29:26 2019 +0000

terraform: update 0.11.14 bottle.

Once you have found the commit for the version you want check it out and move into the Formula directory:

$ git checkout 042c118f93b5c4a29db33e56ff2473d07bf17c44 && cd Formula

Next, we need to unpin and unlink Terraform before installing terraform from the branch we are on:

$ brew unpin terraform
$ brew unlink terraform
$ brew install terraform.rb

Now we should have 0.11.14 and 0.12.3 installed. We can see this using:

➜ ll /usr/local/Cellar/terraform
total 0
drwxr-xr-x  8 mark  staff   256B  8 Jul 13:18 0.11.14
drwxr-xr-x  8 mark  staff   256B  8 Jul 11:40 0.12.3

Switching between versions

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

Stopping Homebrew Removing Terraform versions on upgrades

We may pin Terraform to stop Homebrew removing older versions when we do an upgrade.

$ brew pin terraform