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 tfenvManual 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 ~/.tfenv2. 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/bin3. Update tfenv
To update the tfenv version:
$git -C ~/.tfenv pullUsage
Check the installed tfenv version:
$tfenv --versionList all the installable versions:
$tfenv list-remoteInstall the latest Terraform version:
$tfenv install latestInstall a specific Terraform version:
$# tfenv install <version>
$tfenv install 1.2.9Uninstall a specific Terraform version:
$# tfenv install <version>
$tfenv uninstall 1.2.9List all the installed Terraform versions:
$tfenv listSet a default Terraform version:
$# tfenv use <version>
$tfenv use 1.2.9Switch Between Terraform Versions:
$# tfenv use <version>
$tfenv use 0.13.0Manage 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 pinUse the Terraform version specified in .terraform-version:
$tfenv useWe 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/terraformThen remove the tfenv repository:
$rm -rf ~/.tfenv