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.
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.
MacOS: brew install tfenv
Linux: yay --sync tfenv
(via Arch User Repository (AUR) — details on how to set that up can be found here)
Windows: Currently, tfenv has issues running on Windows. One option is to set up and use the WSL2 (Windows Subsystem for Linux). You can use the steps below to perform a manual installation of tfenv on WSL2 running Ubuntu 20.04:
- Clone the repo to your home directory:
git clone --depth=1 https://github.com/tfutils/tfenv.git ~/.tfenv
- Add
~/.tfenv/bin
to your$PATH
:
echo 'export PATH=$PATH:$HOME/.tfenv/bin' >> ~/.bashrc
- Add a symlink to the tfenv executable:
sudo ln -s ~/.tfenv/bin/* /usr/local/bin
- The
unzip
package will also need to be installed on your system:
apt-get install unzip
- Run tfenv to view the available options—
tfenv
tfenv install <version>
tfenv install 1.3.9
Set the version to your default:
tfenv use 1.3.9
Note that any versions set in the environment variable TFENV_TERRAFORM_VERSION or the .terraform-version file already will override this tfenv default. Note that you can write directly to the .terraform-version file using tfenv pin
.
You can find a list of available Terraform versions here, or directly on the command line using tfenv list-remote
Check your current version in Terraform and in tfenv:
terraform --version
tfenv --version-name
You can also install the latest available version conveniently using:
tfenv install latest
Two other interesting install options you can use:
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.
To switch and change between your installed Terraform versions, you can first view a list of them:
tfenv list
tfenv use 1.2.9
tfenv use latest
To cleanup versions you no longer need on your system, use the uninstall
command:
Tfenv is a simple-to-use open-source command line program that enables you to manage and switch between multiple versions of Terraform.
Check out the tfenv repo on Github for more information.
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.