How to Manage Terraform/OpenTofu/Terragrunt Versions?

How often do you need to switch Terraform, Terragrunt, or OpenTofu versions when jumping between projects? For me, it’s pretty often and honestly, I used to hate it.

Package managers like brew only let you install one version of a tool at a time. So every time I needed a different version, I had to install it manually. Not fun.

Handling Multiple Versions with Ease

Thankfully, there are tools that make managing multiple versions much easier. Here are some of them:

  1. tfenv - terraform
  2. tgenv - terragrunt
  3. tofuenv - opentofu
  4. tgswitch - terragrunt
  5. tfswitch - terraform
  6. tenv - one tool to rule them all :)

I’ve used tfenv and tgenv before, but recently I switched to tenv. Why? Because it supports Terraform, OpenTofu, Terragrunt, Atmos, and Terramate - all in one tool.

How to Use tenv

  1. Install tenv on macOS:

    brew install tenv
    
  2. Install the required version of Terraform:

    tenv tf install 1.11.4
    
  3. Switch between installed versions:

    tenv tf use 1.11.0
    

This is already pretty cool, but switching versions manually for every project is still a hassle. Is there a way to automate this?

Automating Version Switching

Yes, you can automate it!

Just add a version file to your project repository. Here are the filenames that tenv understands:

  • .terraform-version for Terraform
  • .opentofu-version for OpenTofu
  • .terragrunt-version for Terragrunt
  • .terramate-version for Terramate
  • .atmos-version for Atmos

You just put version inside that file, like this:

echo 1.11.4 > .terraform-version

Also, don’t forget to set the environment variable:

export TENV_AUTO_INSTALL=true

This tells tenv to automatically install the required version if it’s not already installed. YOu can add that line to your ~/.zshrc file.

Conclusion

Now, when you switch between projects, tenv will automatically pick the right version of each tool. No more manual switching, no more frustration. Just endless happiness.

Happy automating!