TFSwitch (tfswitch) is a small open-source CLI that downloads the Terraform version you need and points your terraform command at it, so each project can stay on its own release. This tutorial covers installation, the commands you will use every day, how to pin a version per project, switching automatically when you change directory, CI/CD usage and fixes for common errors.
TFSwitch is a command-line tool designed to switch between different versions of Terraform with ease. This tool is particularly useful for managing multiple projects that require different Terraform versions.
Key Features
- Version Management: Switch between different Terraform versions effortlessly.
- Download Versions: If the desired version is not installed, TFSwitch will download it.
- Compatibility: Works with configuration files like
.tfswitchrc,.tfswitch.toml, andversion.tf.
Installation
For Windows
- Download the Windows version of TFSwitch from the release page.
- Extract the executable and add it to your system PATH.
For macOS
Using Homebrew, the installation is straightforward:
Advanced Usage
Integration with CI/CD
TFSwitch can be integrated into CI/CD pipelines to manage Terraform versions dynamically based on project requirements. You can specify the Terraform version directly in your pipeline configuration, ensuring consistent environments across different stages.
Troubleshooting
- Permission Issues: If you encounter permission issues, especially on Linux, ensure that the binary is installed in a directory where you have write access, such as
$HOME/bin.
Conclusion
TFSwitch is a powerful tool for managing multiple versions of Terraform, simplifying the development and deployment process across different environments. By leveraging configuration files and environment variables, TFSwitch ensures that the correct Terraform version is used consistently, reducing potential issues caused by version mismatches.
For more detailed information, visit the official TFSwitch documentation and the GitHub repository.
How tfswitch works
When you run tfswitch, it works out which Terraform version you want. You can pass it as an argument, set it in an environment variable or config file, or pick it from a menu. If that version is not already cached, tfswitch downloads the official binary into a local cache directory, ~/.terraform.versions by default. It then links or copies that binary to the bin path, which is the file your shell runs when you type terraform.
Because of this, two things must be true for switching to work. The bin path must be writable by your user, and its directory must come before any other terraform binary in your PATH. Most tfswitch problems come down to one of these two.
Install and verify
The Homebrew, Linux script and AUR commands in the gist above all work. On Linux, install to a directory you own so you do not need sudo, and make sure that directory is on your PATH:
# Install tfswitch into ~/.local/bin
mkdir -p "$HOME/.local/bin"
curl -L https://raw.githubusercontent.com/warrensbox/terraform-switcher/master/install.sh | bash -s -- -b "$HOME/.local/bin"
# Add the directory to PATH (use ~/.zshrc for zsh)
echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc
# Verify
tfswitch --version
which tfswitchCode language: PHP (php)
Next, install Terraform through tfswitch and check that your shell picks it up:
tfswitch -b "$HOME/.local/bin/terraform" 1.5.7
which terraform
terraform version
# Terraform v1.5.7
# on linux_amd64Code language: PHP (php)
Note: If Terraform is already installed by a package manager (for example the Homebrew terraform formula or an apt package), remove it or make sure the tfswitch bin path comes first in PATH. If you skip this, terraform version can keep showing the old version after a successful switch.
On Windows, extract tfswitch.exe from the release archive into a folder such as C:\tools\bin and add that folder to your user PATH. Then run tfswitch with a bin path inside that same folder, for example tfswitch -b C:\tools\bin\terraform.exe 1.5.7, so that the Terraform binary also lands somewhere you can write to.
Everyday commands
| Command | What it does |
|---|---|
tfswitch | Uses a version from config files or environment if one is found. Otherwise it opens an interactive menu of recent stable releases. |
tfswitch 1.5.7 | Switches to that exact version and downloads it if needed. |
tfswitch -l | Opens the interactive menu with all releases, including alpha, beta and rc builds. |
tfswitch -u | Installs the latest stable release. |
tfswitch -U / --show-latest | Prints the latest stable version without installing it. |
tfswitch -s 1.5 | Installs the latest patch release of the 1.5 line. |
tfswitch -b ~/bin/terraform 1.5.7 | Installs to a custom bin path. |
tfswitch --help | Lists every flag that your installed release supports. |
Note: Flags change between releases. Recent versions add options such as a custom download mirror and support for other products such as OpenTofu. Check tfswitch --help for the options your version actually has.
Pinning a version per project
The real value of tfswitch is that a plain tfswitch run inside a repository selects the right version on its own. It can read the version from several sources:
| Source | Example | Best for |
|---|---|---|
| Command-line argument | tfswitch 1.5.7 | One-off switches. An explicit argument wins over the other sources. |
TF_VERSION environment variable | export TF_VERSION=1.5.7 | CI jobs and temporary overrides |
.tfswitchrc | File containing 1.5.7 | A simple per-repository pin |
.terraform-version | File containing 1.5.7 | Repositories that are also used with tfenv |
.tfswitch.toml | bin and version keys | Custom bin paths, in your home directory or a project |
required_version in .tf files | required_version = "~> 1.5.0" | Keeping the pin in Terraform code itself |
Using required_version is often the cleanest option, because Terraform enforces the same constraint itself. tfswitch reads the constraint from the Terraform files in the current directory and installs the newest release that satisfies it:
# versions.tf
terraform {
required_version = "~> 1.5.0"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}Code language: PHP (php)
cd my-terraform-project
tfswitch
terraform version # a 1.5.x releaseCode language: CSS (css)
Note: Use one source per repository. If a project has both a .tfswitchrc and a different required_version, readers cannot easily tell which version will be used, and terraform init may refuse to run if the two disagree.
Switching automatically when you change directory
Running tfswitch by hand is easy to forget. In zsh you can add a hook that runs it every time you cd into a directory that contains a pin. Add this to ~/.zshrc:
autoload -U add-zsh-hook
load-tfswitch() {
if [ -f ".tfswitchrc" ] || [ -f ".terraform-version" ] || [ -f ".tfswitch.toml" ]; then
tfswitch
fi
}
add-zsh-hook chpwd load-tfswitch
load-tfswitchCode language: JavaScript (javascript)
The hook runs only when one of these files exists. That keeps it from opening the interactive menu in unrelated directories. In bash you can get the same effect by wrapping cd in a function that calls load-tfswitch after changing directory.
Using tfswitch in CI/CD
The CI/CD idea in the section above works like this in practice. The pipeline installs tfswitch, and tfswitch installs whatever version the repository pins. Changing the pin in the repository is then enough to upgrade the pipeline. Here is a GitHub Actions example:
name: terraform
on: [pull_request]
jobs:
plan:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install tfswitch
run: |
mkdir -p "$HOME/.local/bin"
curl -L https://raw.githubusercontent.com/warrensbox/terraform-switcher/master/install.sh | bash -s -- -b "$HOME/.local/bin"
echo "$HOME/.local/bin" >> "$GITHUB_PATH"
- name: Install pinned Terraform
run: tfswitch -b "$HOME/.local/bin/terraform"
- name: Plan
run: |
terraform version
terraform init -input=false
terraform plan -input=falseCode language: PHP (php)
- The repository must contain a pin such as
.tfswitchrcorrequired_version. CI has no terminal, so the interactive menu cannot be used there. - Pass a release tag to the install script so that every run uses the same tfswitch build, rather than whatever is newest.
- Piping a remote script into
bashruns code you have not reviewed. In stricter environments, download the release archive, check its checksum and extract it instead. - If many jobs run, cache
~/.terraform.versionsto avoid downloading Terraform on every run.
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
permission denied when switching | The bin path is in a system directory such as /usr/local/bin | Use -b $HOME/bin/terraform (or set bin in .tfswitch.toml) and add that directory to PATH |
Switch succeeds but terraform version is unchanged | Another terraform appears earlier in PATH, or the shell has cached the old location | Run which -a terraform, remove the other binary or reorder PATH, then run hash -r |
tfswitch: command not found | The install directory is not on PATH | Add it to PATH in your shell profile and open a new shell |
| Download fails for old versions on Apple Silicon | Very old Terraform releases have no darwin_arm64 build | Use a newer Terraform version, or use the amd64 build under Rosetta if your tfswitch release supports an architecture flag |
| Menu appears in CI and the job hangs or fails | No version source was found | Add .tfswitchrc, required_version or TF_VERSION |
| Download fails behind a proxy | No proxy settings, or the release site is blocked | Set HTTPS_PROXY, or use a download mirror if your tfswitch release supports one |
Corrections and clarifications to the original
- The feature list mentions
version.tf. tfswitch does not look for a file with that name. It reads therequired_versionconstraint from the Terraform files in the current directory. By convention that constraint usually lives inversions.tf, but any.tffile works. - The gist describes
tfswitch -las listing all available versions. It actually opens an interactive menu that includes pre-release versions. A plaintfswitchwith no pin shows a menu of stable releases. - The examples above use very old versions (0.10.x to 0.14.x). They are fine for showing the syntax, but new projects should pin a current 1.x release. Releases older than 1.0.2 have no native Apple Silicon build.
- The Linux, Arch, usage and configuration file instructions exist only in the embedded gist. They do not show up if scripts are blocked. The core commands are repeated in plain text in this tutorial.
Frequently asked questions
What is the difference between tfswitch and tfenv?
Both manage several Terraform versions. tfenv uses shims and a .terraform-version file. tfswitch replaces the binary at a bin path, can read required_version from your Terraform code and offers an interactive menu. tfswitch also reads .terraform-version, so moving from one to the other is easy.
Where does tfswitch store downloaded Terraform versions?
By default it keeps them in ~/.terraform.versions. Switching back to a version you have used before needs no new download.
How do I make tfswitch work without sudo?
Point the bin path at a directory you own, either with tfswitch -b $HOME/bin/terraform or with bin = "$HOME/bin/terraform" in ~/.tfswitch.toml. Then add $HOME/bin to the front of your PATH.
Can tfswitch read the version from my Terraform code?
Yes. If no explicit version is given, tfswitch reads the required_version constraint from the .tf files in the current directory. It then installs the newest release that satisfies that constraint.
How do I upgrade to the latest Terraform with tfswitch?
Run tfswitch -u to install the latest stable release. To see the latest version without installing it, run tfswitch --show-latest. Remember to update any pin files in your repositories as well.
Related tutorials
- Terraform Tutorials: TFSec for Security Scanning
- Version Control Workflow Master Tutorial
- More Version Control tutorials
I’m Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms.
I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.
Find Trusted Cardiac Hospitals
Compare heart hospitals by city and services โ all in one place.
Explore Hospitals