How to Upgrade Terraform to the Latest Version

How to install and upgrade Terraform to the latest version

Just like a versioning tool such as Git or SVN, which allows you to rollback or upgrade if there is a problem with the current version of your code, tfenv lets you upgrade as well as rollback versions of Terraform. 

But why do we need multiple versions of Terraform? A typical infrastructure setup always consists of development, testing, staging, and the production environment. As a rule of thumb, the production environment gets the most stable version. On the other hand, development, test, and sometimes staging environments receive newer versions of any software package. The same goes for Terraform. 

If during the testing phase there is a bug which breaks the existing infrastructure setup, tfenv can help roll back Terraform to the previous stable version. Similarly, you can upgrade to the next stable version so that your Terraform stack is up to date.

In this post, we will see how you can install Terraform, as well as how to upgrade it to a specific release version (Terraform v0.15, v0.14, v0.13, v0.12, or any other) using tfenv.

Step 1 - Install tfenv and Verify tfenv Installation

Before we go into the installation part of tfenv let’s first see what tfenv is.

tfenv is a versioning tool for Terraform, maintained by the open source community on Github, under the MIT Open source license. Keep in mind it is not an official tool by HashiCorp.

Installing tfenv on MacOS

Lets first see how we can install tfenv on MacOS. For MacOS we are going to use the brew package manager. Refer to the following command for installation: 

$ brew install tfenv

Installing tfenv on Linux

Installing on Linux is a bit more manual. Use the following installation instructions:

1: Clone the GitHub repo

$ git clone https://github.com/tfutils/tfenv.git ~/.tfenv

2: Update $PATH 

$ echo ‘export PATH=”$HOME/.tfenv/bin:$PATH”‘ >> ~/.bash_profile

3: Create a symlinks

$ sudo ln -s ~/.tfenv/bin/* /usr/local/bin

Installing tfenv on Windows

tfenv does not currently support Windows due to symlink issues. 

Verify tfenv installation

Whether you are on MacOS or Linux, you can verify the installation of tfenv by simply running the following command:

$ tfenv -v

It should return with the tfenv version. At the time of writing this article, the latest version of tfenv was tfenv 2.2.2

How to install and upgrade Terraform to the latest version

Step 2 - List Available Terraform Versions Using tfenv

Now that we have seen how to install tfenv, let’s dig into the tfenv commands. The first command which we are going to see is

 $ tfenv list-remote

The above command will list out all available versions of Terraform to date. Here’s a screenshot after running the tfenv list-remote command.

How to install and upgrade Terraform to the latest version

If you are looking for a specific version of Terraform (e.g. v0.12, v0.13, v0.14, v0.15) but tfenv list-remote returns with a really long list of versions, you can run the following command that includes the version you want. In this example, I am looking for Terraform 0.12.0

$ tfenv list-remote | grep 0.12.0

 

How to install and upgrade Terraform to the latest version

Step 3 - Upgrade to a Specific Terraform Version Using tfenv

You have seen in the previous steps how to install tfenv and how to list all or some specific version of Terraform. 

Let’s install a specific version (0.12.0) of Terraform using tfenv. Use the following tfenv for installation: 

$ tfenv install 0.12.0

How to install and upgrade Terraform to the latest version

After installing Terraform 0.12.0, you need to run one more command to actually use that version:

$ tfenv use 0.12.0

How to install and upgrade Terraform to the latest version

Verify the installation of Terraform by running

$ terraform -v 

How to install and upgrade Terraform to the latest version

Note: the above-mentioned command can be used for any version of Terraform , e.g. 0.13.0, 0.14.0, 0.15.0

Step 4 - Upgrade to the Latest Terraform Version Using tfenv

In the previous step we saw how to upgrade to a specific version of Terraform, but what if you want to upgrade to the latest version irrespective of any specific one?

Well, tfenv provides the latest flag which can be used along with the $ tfenv install command and it will let you install the latest stable version of Terraform.

Here is the tfenv command for installing the latest version of Terraform: 

$ tfenv install latest

How to install and upgrade Terraform to the latest version

To switch and use the latest version of Terraform, run: 

$ tfenv use 1.0.8

How to install and upgrade Terraform to the latest version

Verify the installation by running the $ terraform -v command:

How to install and upgrade Terraform to the latest version

Step 5 - Install and Switch to a Specific Version Using tfenv

In the previous steps we have seen how to install specific versions (0.12.0) as well as the latest version of Terraform.

Let’s now see how to install and switch to some other version, 0.13.0 for example.

tfenv always mandates you to first install the version (if you have not installed before) of Terraform which you want to have, and then to make the switch to that specific version.

First, install Terraform version 0.13.0:

$ tfenv install 0.13.0

How to install and upgrade Terraform to the latest version

Then, make the switch to 0.13.0 (if you have already installed other versions of Terraform, you can make direct use of the tfenv use command):

$ tfenv use 0.13.0

How to install and upgrade Terraform to the latest version

Note: the above-mentioned steps can be used for switching to any desired version of Terraform.

Step 6 - Uninstall the Terraform version using tfenv

Last but not least, tfenv provides you with a feature to uninstall any version of Terraform. To uninstall any version of Terraform, you must provide the exact version (e.g. latest, 0.15.0, 0.14.0 etc.).

Here’s how to uninstall the latest version of Terraform:

$ tfenv uninstall latest

How to install and upgrade Terraform to the latest version

To uninstall a specific version of Terraform, such as 0.12.0, run

$ tfenv uninstall 0.12.0

How to install and upgrade Terraform to the latest version

Key Points

tfenv is a great tool to utilize when you are managing multiple environments that might have different versions of Terraform. With the help of tfenv, you can try out as well as install different versions of Terraform, and with the tfenv use command you can easily switch between the various versions of Terraform. 

In addition to tfenv, you can use Spacelift to manage your Terraform versioning out of the box. If you need any help managing your Terraform infrastructure, building more complex workflows based on Terraform, and managing AWS credentials per run, instead of using a static pair on your local machine, Spacelift is a fantastic tool for this. You can check it for FREE, by going here and creating a trial account.

Share this post

twitter logo