How to install multiple or two versions of Terraform?
Asked Answered
T

5

20

I have a lot of Terraform modules written in Terraform 0.11 using gcp-provider of Terraform and want to upgrade the same to Terraform 0.12.

For this purpose, I need to keep both the versions installed on my system and use the version according to the version the module is written in.

I will go one by one in every module and upgrade the module using terraform 0.12upgrade to be compatible with Terraform 0.12 as per this documentation.

How to safely keep two versions of Terraform in one System?

Trueman answered 7/2, 2020 at 12:44 Comment(0)
T
22

I use Ubuntu 18.04 and I achieved this safely following the below steps. Similar steps can be followed to do the same on any Linux distro (making sure you are downloading the compatible binary. Confirm here)

NOTE Running the following commands as root or sudo user

Create directories to keep the Terraform binaries

$ mkdir -p /usr/local/tf
$ mkdir -p /usr/local/tf/11
$ mkdir -p /usr/local/tf/12

Download the binaries for both the versions

  1. Download and extract the binary for Terraform 0.11 in a separate directory:
    $ cd /usr/local/tf/11
    $ wget https://releases.hashicorp.com/terraform/0.11.14/terraform_0.11.14_linux_amd64.zip
    $ unzip terraform_0.11.14_linux_amd64.zip
    $ rm terraform_0.11.14_linux_amd64.zip
    
  2. Download and extract the binary for Terraform 0.12 in a separate directory:
    $ cd /usr/local/tf/12
    $ wget https://releases.hashicorp.com/terraform/0.12.20/terraform_0.12.20_linux_amd64.zip
    $ unzip terraform_0.12.20_linux_amd64.zip
    $ rm terraform_0.12.20_linux_amd64.zip
    
  3. Create symlinks for both the Terraform versions in /usr/bin/ directory:
    ln -s /usr/local/tf/11/terraform /usr/bin/terraform11
    ln -s /usr/local/tf/12/terraform /usr/bin/terraform12
    
    # Make both the symlinks executable
    chmod ugo+x /usr/bin/terraform*
    

Calling different versions

  • Now, command terraform11 invokes version 0.11 and terraform12 invokes version 0.12
  • Example:
    $ terraform11
    $ terraform12
    

NOTE

  • Keeping the binaries in separate directories helps to separate their plugins as well without disturbing each other.
Trueman answered 7/2, 2020 at 12:44 Comment(0)
E
22

I'd highly recommend the tfenv tool. It sanely and easily can be used to manage multiple terraform installations. It's familiar if you've ever used nvm (for nodejs) or rvm (for ruby).

You can even add a .terraform-version file to your modules and the tool will automatically switch terraform versions for you when you cd into a module.

Each answered 7/2, 2020 at 13:39 Comment(1)
For the doubters out there, this really works! A simple brew install tfenv enables you to do something like tfenv install min-required && tfenv use 0.15.4. Great for switching between projects easily!Healthful
I
5

Make your life easy and install tfswitch. It takes care of installing and switching between versions you need; and it works like a magic.

Indurate answered 1/12, 2020 at 1:0 Comment(0)
S
2

Now, you can also use tenv from OpenTofu. It also still supports Terraform.

Scorekeeper answered 28/3, 2024 at 14:28 Comment(0)
D
1

If using multiple environments with terraform, tfenv would be the best tool to use. I initially had the same error and was able to run tfenv use version-number and it solved my issue. For background, I use terraform for personal and work projects on the same computer.

Demented answered 20/3, 2022 at 0:11 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.