What is tfenv
?
tfenv
is a Terraform version manager that allows you to easily install and manage multiple versions of Terraform on your system. You can switch between different versions of Terraform quickly, which is especially useful when you are working with multiple Terraform projects that require different versions of the tool.
Step 1: Installing tfenv
The first step is to install tfenv
on your system. You can install tfenv
using Homebrew, Git, or manually.
1.1: Installing with Homebrew (for macOS or Linux)
If you are using macOS or Linux and have Homebrew installed, you can easily install tfenv
:
brew install tfenv
1.2: Installing Manually (without Homebrew)
- Clone the
tfenv
repository from GitHub:git clone https://github.com/tfutils/tfenv.git ~/.tfenv
- Add the following to your
~/.bashrc
(or~/.zshrc
for Zsh users):export PATH="$HOME/.tfenv/bin:$PATH"
- Reload your terminal:
source ~/.bashrc # or source ~/.zshrc for Zsh users
- Verify the installation:
tfenv --version
If the installation was successful, you should see the version of tfenv
installed.
Step 2: Installing Terraform Versions Using tfenv
With tfenv
installed, you can now install specific versions of Terraform.
2.1: Installing the Latest Stable Version
To install the latest stable version of Terraform:
tfenv install latest
This will install the latest stable version of Terraform and set it as the default.
2.2: Installing a Specific Version
To install a specific version of Terraform, use the following command:
tfenv install 1.0.0
This installs Terraform 1.0.0. You can replace 1.0.0
with any version you need.
2.3: Installing a Version for a Specific Project
To install a version of Terraform for a specific project, you can create a .terraform-version
file in the project directory with the desired version.
For example, if you want to use Terraform 0.15.0
for a project:
- Navigate to the project directory:
cd ~/projects/my_project
- Create a
.terraform-version
file with the version number:echo "0.15.0" > .terraform-version
tfenv
will automatically use Terraform0.15.0
whenever you are in that directory.
Step 3: Switching Between Terraform Versions
With tfenv
, switching between installed versions of Terraform is simple.
3.1: Switching to a Specific Version
To switch to a different version that is already installed:
tfenv use 1.1.0
This will set Terraform 1.1.0 as the active version.
3.2: Switching to the Version Defined in .terraform-version
If you have a .terraform-version
file in your project directory, you can switch to the version specified in that file with:
tfenv use
This will automatically switch to the version specified in the .terraform-version
file.
3.3: Listing Installed Versions
You can list all installed versions of Terraform with:
tfenv list
This shows a list of Terraform versions installed on your system, and it marks the active version.
3.4: Removing a Version
If you want to remove a specific version, use:
tfenv uninstall 1.0.0
This removes Terraform version 1.0.0 from your system.
Step 4: Managing Multiple Terraform Versions
4.1: Setting a Global Version
You can set a global version of Terraform that will be used across all projects unless overridden by a .terraform-version
file.
To set a global version:
tfenv global 1.1.0
This sets Terraform 1.1.0 as the default version for all projects.
4.2: Setting a Local Version (for a specific project)
If you want to use a specific version of Terraform for a particular project, you can set a local version. You can do this by creating a .terraform-version
file in the project’s root directory, as shown in Step 2.3.
Alternatively, you can manually specify the version:
tfenv local 0.14.0
This will create a .terraform-version
file in the current directory, making Terraform 0.14.0 the local version for that project.
Step 5: Use Cases for tfenv
5.1: Working on Multiple Terraform Projects
With tfenv
, you can manage multiple versions of Terraform easily. For example:
- Project
A
requires Terraform 0.14.0. - Project
B
requires Terraform 1.0.0. - Project
C
requires the latest stable version.
You can use tfenv
to easily switch between these versions without any issues.
5.2: Collaborating with Teams
If your team works with different versions of Terraform, tfenv
helps ensure everyone is using the same version for a project. By checking in the .terraform-version
file, you ensure consistency across your team.
Step 6: Troubleshooting
If you encounter any issues, here are some tips for troubleshooting:
6.1: Verify Installed Versions
If you’re unsure whether a version is installed:
tfenv list
6.2: Verify Active Version
To check the current active version of Terraform:
terraform version
This will display the currently active version of Terraform in use.
6.3: Reinstall a Version
If a Terraform version seems to be misbehaving, you can uninstall and then reinstall it:
tfenv uninstall 1.0.0
tfenv install 1.0.0
Conclusion
tfenv
is an excellent tool for managing multiple versions of Terraform, especially if you’re working with multiple Terraform projects or need to ensure consistent versions across your team. By following the steps in this tutorial, you can easily install, switch between, and manage different versions of Terraform.
I’m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I am working at Cotocus. I blog tech insights at DevOps School, travel stories at Holiday Landmark, stock market tips at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at I reviewed , and SEO strategies at Wizbrand.
Do you want to learn Quantum Computing?
Please find my social handles as below;
Rajesh Kumar Personal Website
Rajesh Kumar at YOUTUBE
Rajesh Kumar at INSTAGRAM
Rajesh Kumar at X
Rajesh Kumar at FACEBOOK
Rajesh Kumar at LINKEDIN
Rajesh Kumar at PINTEREST
Rajesh Kumar at QUORA
Rajesh Kumar at WIZBRAND