Going to AWS re:Invent 2024?

➡️ Book a meeting with Spacelift

Terraform

How to Use tfenv to Manage Multiple Terraform Versions

Using tfenv to Manage Multiple Terraform Versions

In this article, we will explore the popular Terraform version manager tfenv, with practical examples of how to install it, and use it to add, change and uninstall different Terraform versions. Tfenv enables you to install multiple Terraform versions and switch between them easily as needed.

What is tfenv?

Tfenv is a version manager inspired by rbenv, a Ruby programming language version manager.

Different versions of Terraform might be required when working on multiple projects for different customers at the same time where a particular version is used or to ensure backward compatibility by running the code with multiple versions. Terraform version upgrades can introduce unwanted problems.

One tip to avoid this is to set the required_version in the Terraform block. When this is set, attempting to run the code with a different version will produce an explicit error, and the code will not run. Once you have tested using a different version, the required_version can be updated, effectively ensuring everyone running the code will see the same results.

terraform {
    required_version = "0.12.10"
}

Learn more about how to manage different Terraform versions.

Note: New versions of Terraform are placed under the BUSL license, but everything created before version 1.5.x stays open-source. OpenTofu is an open-source version of Terraform that expands on Terraform’s existing concepts and offerings. It is a viable alternative to HashiCorp’s Terraform, being forked from Terraform version 1.5.6.

Installing tfenv

  1. Clone the repo to your home directory:
git clone --depth=1 https://github.com/tfutils/tfenv.git ~/.tfenv
  1. Add ~/.tfenv/bin to your $PATH:
echo 'export PATH=$PATH:$HOME/.tfenv/bin' >> ~/.bashrc
  1. Add a symlink to the tfenv executable:
sudo ln -s ~/.tfenv/bin/* /usr/local/bin
  1. The unzip package will also need to be installed on your system:
apt-get install unzip
  1. Run tfenv to view the available options— tfenv
tfenv

Install Terraform Versions

tfenv install <version>

tfenv install 1.3.9
tfenv install

Set the version to your default:

tfenv use 1.3.9
tfenv use
list remote

Check your current version in Terraform and in tfenv:

terraform --version
terraform --version
tfenv --version-name
tfenv --version-name

You can also install the latest available version conveniently using:

tfenv install latest
  • latest-allowed is a syntax to scan your Terraform files to detect which version is maximally allowed.
  • min-required is a syntax to scan your Terraform files to detect which version is minimally required.

Change Terraform Versions

To switch and change between your installed Terraform versions, you can first view a list of them:

tfenv list 
tfenv list
tfenv use 1.2.9
tfenv use 1.2.9
tfenv use latest

Uninstall Terraform Versions

To cleanup versions you no longer need on your system, use the uninstall command:

tfenv uninstall 1.2.9

Key Points

We encourage you also to explore how Spacelift makes it easy to work with Terraform. 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.

Manage Terraform Better and Faster

If you are struggling with Terraform automation and management, check out Spacelift. It helps you manage Terraform state, build more complex workflows, and adds several must-have capabilities for end-to-end infrastructure management.

Start free trial

The Practitioner’s Guide to Scaling Infrastructure as Code

Transform your IaC management to scale

securely, efficiently, and productively

into the future.

ebook global banner
Share your data and download the guide