167

Use case

I have installed Terraform v0.11.13 via homebrew and as recommended by terraform I want to ugprade to version v0.11.14 before doing the major upgrade to v0.12.0.

The problem

When I run brew upgrade terraform or download the Mac package from the terraform website it would immediately update my terraform version to v0.12.0 I think.

So how can I upgrade to v0.11.14 instead?

13 Answers 13

432

Especially when playing around with Terraform 0.12 betas, I learned to love tfenv.

After installation (via brew install tfenv on MacOS), this allows you to easily discover, install and activate any Terraform version:

$ tfenv list-remote
0.12.0
0.12.0-rc1
0.12.0-beta2
0.12.0-beta1
0.12.0
0.11.14
...

$ tfenv install 0.11.14
[INFO] Installing Terraform v0.11.14
[INFO] Downloading release tarball from https://releases.hashicorp.com/terraform/0.11.14/terraform_0.11.14_darwin_amd64.zip
...
[INFO] Installation of terraform v0.11.14 successful
[INFO] Switching to v0.11.14
[INFO] Switching completed

If you want to manually switch to a different version:

$ tfenv use 0.12.0
[INFO] Switching to v0.12.0
[INFO] Switching completed

Alternatively, adding .terraform-version file makes tfenv automatically switch to the right version for a given directory and it will even take care of auto-installing the correct version if not already installed.

4
  • 19
    Stephen, you are the King! Commented Jan 7, 2020 at 16:06
  • 13
    Adding a .terraform-version file makes tfenv automatically switch to the right version for a given directory and it will even take care of auto-installing the correct version if not already installed.
    – luk2302
    Commented Aug 13, 2020 at 12:45
  • I'me getting that: 'Initializing provider plugins... - Finding latest version of hashicorp/google... Error: Failed to query available provider packages' Commented Mar 18, 2021 at 16:22
  • 4
    if already a latest version is installed. You may need to unlink first brew unlink terraform before installing specific version from tfenv. Once installed, type tfenv use 0.12.30 to use new version. Commented Feb 4, 2022 at 11:47
47

For anyone looking to do the same without using homebrew:

  1. Get source
$ wget https://releases.hashicorp.com/terraform/0.11.14/terraform_0.11.14_linux_amd64.zip

$ unzip terraform_0.11.14_linux_amd64.zip
  1. Give binary executable permissions and install (will overwrite current version)
$ chmod +x terraform

$ sudo mv terraform /usr/local/bin/
  1. Confirm version
$ terraform --version

Source: https://titosoft.github.io/kvm/terraform-and-kvm/#installing-terraform

3
  • i am getting this error mate with your code -bash: /usr/local/bin/terraform: cannot execute binary file Commented Aug 16, 2019 at 10:25
  • simple, straightforward. Commented Mar 12, 2020 at 23:46
  • This method is actually nicer than the accepted one as it avoids breaking terraform's simlink Thanks for the help !
    – Alex
    Commented Aug 30, 2022 at 19:27
17

There is a nice alternative to tfenv that I've been using for few years now - tfswitch.

  1. Simple installation process with:
# MacOS
brew install warrensbox/tap/tfswitch

# Linux
curl -L https://raw.githubusercontent.com/warrensbox/terraform-switcher/release/install.sh | bash
  1. Supplies alpha, beta and release candidates terraform versions.
  2. Supports multiple options for changing versions like:
  • Manual with tfswitch VERSION_NUMBER.
  • Automatically detects and switch to version described in version.tf file.
  • Automatically switch to custom binaries under custom path (helpful for users or services with limited privileges).
  • Automatically switch with .tfswitchrc, .terrafom-version - requires minimum setup and supports bash, zsh and fish.
  1. Can be easily integrated with Jenkins and CircleCI - I haven't tested it out, these features were added 10 and 5 months ago respectively.

All in all it's a great and simple helper, would recommend this one.

7

Apart from the conventional solutions, an easy implementation to the required problem would be installing tfswitch. It can change terraform versions with only one command. Installation and usage guide is provided in the given link.

Note: You can not downgrade to previous versions after the script has been initialised (terraform init).

5

Easy and best way to do:-

# MacOS
 brew install warrensbox/tap/tfswitch

# Linux
curl -L https://raw.githubusercontent.com/warrensbox/terraform- 
switcher/release/install.sh | bash

then use Either one of command to change version-

1. tfswitch ==> you will see option to select version.


2. tfswitch 0.14.3 ==> this way
4
  • ashishkarpe@Ashishs-MacBook-Air terraform % brew install warrensbox/tap/tfswitch ==> Tapping warrensbox/tap Cloning into '/opt/homebrew/Library/Taps/warrensbox/homebrew-tap'... remote: Enumerating objects: 549, done. remote: Counting objects: 100% (107/107), done. remote: Compressing objects: 100% (106/106), done. remote: Total 549 (delta 49), reused 5 (delta 1), pack-reused 442 Receiving objects: 100% (549/549), 69.88 KiB | 1.89 MiB/s, done. Resolving deltas: 100% (251/251), done. Commented Mar 7, 2022 at 12:04
  • Error: Invalid formula: /opt/homebrew/Library/Taps/warrensbox/homebrew-tap/Formula/jscheck.rb jscheck: Calling bottle :unneeded is disabled! There is no replacement. Please report this issue to the warrensbox/tap tap (not Homebrew/brew or Homebrew/core): /opt/homebrew/Library/Taps/warrensbox/homebrew-tap/Formula/jscheck.rb:6 Commented Mar 7, 2022 at 12:04
  • Error: Invalid formula: /opt/homebrew/Library/Taps/warrensbox/homebrew-tap/Formula/hubapp.rb hubapp: Calling bottle :unneeded is disabled! There is no replacement. Please report this issue to the warrensbox/tap tap (not Homebrew/brew or Homebrew/core): /opt/homebrew/Library/Taps/warrensbox/homebrew-tap/Formula/hubapp.rb:9 Commented Mar 7, 2022 at 12:04
  • Error: Invalid formula: /opt/homebrew/Library/Taps/warrensbox/homebrew-tap/Formula/health-check.rb health-check: Calling bottle :unneeded is disabled! There is no replacement. Please report this issue to the warrensbox/tap tap (not Homebrew/brew or Homebrew/core): /opt/homebrew/Library/Taps/warrensbox/homebrew-tap/Formula/health-check.rb:9 Error: Cannot tap warrensbox/tap: invalid syntax in tap! Commented Mar 7, 2022 at 12:04
1

I have used TF since 0.6 and actively use many different versions on my workstation. I do this with direnv [https://direnv.net/] since that was available for quite a while, and it works well for managing the TF binary version within a dir, plus it allows me to pass env vars per dir into TF with little hassle.

Sample .envrc:

export AWS_PROFILE=prod
export PATH=/usr/local/terraform/terraform-0.12.20:$PATH

export TF_VAR_dd_api_key=REDACTED
export TF_VAR_dd_app_key=REDACTED

When I cd into the dir containing this .envrc, not only does it put the correct TF binary in my PATH, it also sets my AWS profile and in this case some DataDog API keys.

When I want to upgrade TF, I ensure I have the desired binary placed into the correct location and edit the .envrc so it is in PATH. Ideally, the .envrc is not pushed to Github since other people could have different setups, and especially API keys should not go into the repo.

I know it's a little old fashioned but it works great for me.

1

just use tfenv or tfswitch both tools are really good. good point is that if you have version requirements in terraform files when you do tfswitch it will automatically download and install version which you need.

https://tfswitch.warrensbox.com/

1

I realised that I was using remote execution with terraform cloud instead of local execution, Thus, even though I upgraded my local terraform to 1.3.7, my remote version remain 1.1.7 therefore I had update the remote version by simply selecting the appropriate version from my workspace settings and everything works.

1

I can recommend checking out tenv.

tenv allows you to manage not only Terraform versions, but also OpenTofu and Terragrunt - one simple tool for all three binaries.

1

Your version is quite old now but here is my suggestion:

  1. Uninstall the current Terraform version (if installed): If you already have Terraform v0.11.13 installed via Homebrew, first uninstall it to avoid conflicts:
brew uninstall terraform
  1. Use Terraform's official binaries to install v0.11.14: Homebrew does not maintain older versions directly, so you can manually download and install the required version.

  2. Download binary

curl -O https://releases.hashicorp.com/terraform/0.11.14/terraform_0.11.14_darwin_amd64.zip
  1. Extract and move
unzip terraform_0.11.14_darwin_amd64.zip
sudo mv terraform /usr/local/bin/terraform

5 Verify the installation:

terraform -v

Above is for direct installation method You can follow tfenv approach to get multiple version in use.

Here is quick commands

#Download tfenv for Mac
brew install tfenv

#Download required version
tfenv install 0.11.14

#Set default version
tfenv use 0.11.14

#Optional you can set version globally as well
tfenv use 1.0.11 --global


0

below command will show all the available versions of terraform

sudo apt policy terraform

Install the desired version:

sudo apt-get install terraform=1.4.4-1

If you have a old version already, then uninstall it and run above commands

sudo apt remove terraform

1
  • This answer is for debian based systems. OP originally asked for help with "homebrew" which would be for macs most likely.
    – apanzerj
    Commented Jul 29 at 19:11
0

My two cents

There's a tool that works pretty much like tfenv but it also handles many other software

mise-en-place: https://mise.jdx.dev

You can manage versions for terraform terragrunt go java node python ruby .net awscli postgres redis mongodb etc etc via plugins

Full list of plugins: https://github.com/mise-plugins/registry/blob/main/README.md

Hope you like it

0

#Terraform installation through downloaded binary with respect to a specific version

There are cases when your organisation is using a particular terraform version & you know that current terraform version is way latest than the one in your organisation is using and you are tasked to use the same without the latest version. so that you can use below steps to update the binary versions as in when needed under /usr/bin

    wget https://releases.hashicorp.com/terraform/1.6.5/terraform_1.6.5_linux_amd64.zip
    wget https://releases.hashicorp.com/terraform/1.6.5/terraform_1.6.5_SHA256SUMS
    SHA256SUM terraform_1.6.5_linux_amd64.zip
    mv terraform_1.6.5_linux_amd64.zip terraform.zip
    unzip terraform.zip
    sudo mv terraform /usr/bin
    terraform version

Just in case a video based handson reference is needed - #https://www.youtube.com/watch?v=1vBJNjv9h8I

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.