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:
Terraform is really powerful, but to achieve an end-to-end secure Gitops approach, you need to use a product that can run your Terraform workflows. Spacelift takes managing Terraform to the next level by giving you access to a powerful CI/CD workflow and unlocking features such as:
- Policies (based on Open Policy Agent) – You can control how many approvals you need for runs, what kind of resources you can create, and what kind of parameters these resources can have, and you can also control the behavior when a pull request is open or merged.
- Multi-IaC workflows – Combine Terraform with Kubernetes, Ansible, and other infrastructure-as-code (IaC) tools such as OpenTofu, Pulumi, and CloudFormation, create dependencies among them, and share outputs
- Build self-service infrastructure – You can use Blueprints to build self-service infrastructure; simply complete a form to provision infrastructure based on Terraform and other supported tools.
- Integrations with any third-party tools – You can integrate with your favorite third-party tools and even build policies for them. For example, see how to integrate security tools in your workflows using Custom Inputs.
Previously, the Affinity team used Terraform both as their runner and for state management. Now, they use Amazon S3 for state management, but because this is connected to the Spacelift provider, state management is something they don’t have to worry about.
Spacelift enables you to create private workers inside your infrastructure, which helps you execute Spacelift-related workflows on your end. Read the documentation for more information on configuring private workers.
You can check it out for free by creating a trial account or booking a demo with one of our engineers.
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.
Achieve Terraform at scale with Spacelift
Spacelift takes managing infrastructure at scale to a whole new level, offering a more open, more customizable, and more extensible product. It’s a better, more flexible CI/CD for Terraform, offering maximum security without sacrificing functionality.