tfenv is a version manager for Terraform, it provides a simple interface for installing and managing multiple versions easily just like nvm for Node.js.
Install tfenv
Using Homebrew
$brew install tfenv
Manual Installation
1. Clone the Git repository
$# Clone the repo to ~/.tfenv
$# Truncate the history to the last commit (--depth=1)
$git clone --depth=1 https://github.com/tfutils/tfenv.git ~/.tfenv
2. Add the ~/.tfenv/bin
directory to the $PATH
environment variable
Add this line to ~/.bash_profile
or ~/.bashrc
files:
export PATH="$HOME/.tfenv/bin:$PATH"
Or create symlinks for the scripts in ~/.tfenv/bin
into a path that’s already added to the $PATH
:
$sudo ln -s ~/.tfenv/bin/* /usr/local/bin
3. Update tfenv
To update the tfenv
version:
$git -C ~/.tfenv pull
Usage
Check the installed tfenv
version:
$tfenv --version
List all the installable versions:
$tfenv list-remote
Install the latest Terraform version:
$tfenv install latest
Install a specific Terraform version:
$# tfenv install <version>
$tfenv install 1.2.9
Uninstall a specific Terraform version:
$# tfenv install <version>
$tfenv uninstall 1.2.9
List all the installed Terraform versions:
$tfenv list
Set a default Terraform version:
$# tfenv use <version>
$tfenv use 1.2.9
Switch Between Terraform Versions:
$# tfenv use <version>
$tfenv use 0.13.0
Manage Projects Using Different Terraform Versions
We can manage multiple projects that require a different Terraform version using a special file named .terraform-version
.
Create a .terraform-version
file in the current directory with the current Terraform version:
$tfenv pin
Use the Terraform version specified in .terraform-version
:
$tfenv use
We can also set the TFENV_TERRAFORM_VERSION
environment variable which overrides the value in .terraform-version
when switching the Terraform version using tfenv use
.
Uninstall tfenv
Remove the export line from ~/.bash_profile
or ~/.bashrc
files:
export PATH="$HOME/.tfenv/bin:$PATH"
Or remove the symlinks of the files in ~/.tfenv/bin/*
if you used this method:
$sudo rm /usr/local/bin/tfenv
$sudo rm /usr/local/bin/terraform
Then remove the tfenv
repository:
$rm -rf ~/.tfenv