If you have multiple terraform projects, it can be necessary to support multiple versions of the terraform binary to match module and provider dependencies.
Instead of creating a custom solution of binary copies and links, this can be done using the Alternatives concept which handles these symbolic links in a standard way using links in /etc/alternatives on Debian/Ubuntu based systems.
For this example, let’s say we need terraform version 12 for one of our projects, while using terraform 14 for everything else.
First, let’s install both versions of terraform into /usr/local/bin
# install binary named 'terraform12' wget https://releases.hashicorp.com/terraform/0.12.31/terraform_0.12.31_linux_amd64.zip unzip terraform_0.12.31_linux_amd64.zip sudo chown root:root terraform sudo mv terraform /usr/local/bin/terraform12 # install binary named 'terraform14' wget https://releases.hashicorp.com/terraform/0.14.11/terraform_0.14.11_linux_amd64.zip unzip terraform_0.14.11_linux_amd64.zip sudo chown root:root terraform sudo mv terraform /usr/local/bin/terraform14 # validate PATH and installation terraform12 --version terraform14 --version
Below is how you would create a preferred usage.
# create alternative for terraform12 # parameters=<link> <name> <path> <priority> sudo update-alternatives --install /usr/local/bin/terraform terraform /usr/local/bin/terraform12 10 # create alternative for terraform14, higher priority means preferred sudo update-alternatives --install /usr/local/bin/terraform terraform /usr/local/bin/terraform14 20 # show preferences, terraform14 is listed as best choice $ sudo update-alternatives --query terraform Name: terraform Link: /usr/local/bin/terraform Status: auto Best: /usr/local/bin/terraform14 Value: /usr/local/bin/terraform14 Alternative: /usr/local/bin/terraform12 Priority: 10 Alternative: /usr/local/bin/terraform14 Priority: 20 # internally, link was added to /etc/alternatives ls -l /etc/alternatives/terraform*
Validate the terraform version 14 (the preferred choice) is invoked:
$ terraform --version
And if you need to change the preference to terraform version 12:
$ sudo update-alternatives --config terraform There are 2 choices for the alternative terraform (providing /usr/local/bin/terraform). Selection Path Priority Status ------------------------------------------------------------ * 0 /usr/local/bin/terraform14 20 auto mode 1 /usr/local/bin/terraform12 10 manual mode 2 /usr/local/bin/terraform14 20 manual mode Press <enter> to keep the current choice[*], or type selection number:
Validate that terraform version 12 is now invoked
$ terraform --version
Use ‘–set’ to switch back to terraform version 14 without the interactive menu of ‘–config’
sudo update-alternatives --set terraform /usr/local/bin/terraform14
REFERENCES
manpages ubuntu, updates-alternatives man page
NOTES
If link group becomes broken and you must remove all
sudo update-alternatives --remove-all terraform
If you need to remove one of the alternatives
# 'name' then 'path' sudo update-alternatives --remove terraform /usr/local/bin/terraform14