MYHOSPITALNOW.COM

Find Trusted Doctors & Hospitals

Discover healthcare providers, compare hospitals, and explore treatment options worldwide.

Free Treatment Quotes • Doctor Search • Hospital Comparison

Find Healthcare Options

www.myhospitalnow.com

Comprehensive Tutorial on TFSwitch

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

  1. Version Management: Switch between different Terraform versions effortlessly.
  2. Download Versions: If the desired version is not installed, TFSwitch will download it.
  3. Compatibility: Works with configuration files like .tfswitchrc, .tfswitch.toml, and version.tf.

Installation

For Windows

  1. Download the Windows version of TFSwitch from the release page.
  2. 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

CommandWhat it does
tfswitchUses a version from config files or environment if one is found. Otherwise it opens an interactive menu of recent stable releases.
tfswitch 1.5.7Switches to that exact version and downloads it if needed.
tfswitch -lOpens the interactive menu with all releases, including alpha, beta and rc builds.
tfswitch -uInstalls the latest stable release.
tfswitch -U / --show-latestPrints the latest stable version without installing it.
tfswitch -s 1.5Installs the latest patch release of the 1.5 line.
tfswitch -b ~/bin/terraform 1.5.7Installs to a custom bin path.
tfswitch --helpLists 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:

SourceExampleBest for
Command-line argumenttfswitch 1.5.7One-off switches. An explicit argument wins over the other sources.
TF_VERSION environment variableexport TF_VERSION=1.5.7CI jobs and temporary overrides
.tfswitchrcFile containing 1.5.7A simple per-repository pin
.terraform-versionFile containing 1.5.7Repositories that are also used with tfenv
.tfswitch.tomlbin and version keysCustom bin paths, in your home directory or a project
required_version in .tf filesrequired_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 .tfswitchrc or required_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 bash runs 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.versions to avoid downloading Terraform on every run.

Troubleshooting

SymptomLikely causeFix
permission denied when switchingThe bin path is in a system directory such as /usr/local/binUse -b $HOME/bin/terraform (or set bin in .tfswitch.toml) and add that directory to PATH
Switch succeeds but terraform version is unchangedAnother terraform appears earlier in PATH, or the shell has cached the old locationRun which -a terraform, remove the other binary or reorder PATH, then run hash -r
tfswitch: command not foundThe install directory is not on PATHAdd it to PATH in your shell profile and open a new shell
Download fails for old versions on Apple SiliconVery old Terraform releases have no darwin_arm64 buildUse 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 failsNo version source was foundAdd .tfswitchrc, required_version or TF_VERSION
Download fails behind a proxyNo proxy settings, or the release site is blockedSet 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 the required_version constraint from the Terraform files in the current directory. By convention that constraint usually lives in versions.tf, but any .tf file works.
  • The gist describes tfswitch -l as listing all available versions. It actually opens an interactive menu that includes pre-release versions. A plain tfswitch with 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

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services โ€” all in one place.

Explore Hospitals
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.

Related Posts

Give Your Documents the Discipline You Already Give Your Code

No engineer would touch production code without version history, controlled access, and a record of who changed what. Yet the contracts, policies, and reports that actually run…

Read More

Team Foundation Server 2015 Installation Guide (SQL Express + Update 3)

Install Team Foundation Server 2015 using the original Microsoft download paths, with clear guidance for online, offline, and ISO-based setup. This revised guide also includes an installation workflow, command example, and practical pitfalls to avoid.

Read More

Team Foundation Build 2010: Architecture, Build Definitions, and Migration from TFS 2008

Team Foundation Build 2010 introduced a controller/agent build architecture, richer build definitions, and gated check-in workflows for TFS teams. This revised guide turns the original topic list into a practical reference for setup, customization, and migration planning.

Read More

Team Foundation Server 2015: Practical Guide to Planning, Git, Builds, Testing, and Integrations

This guide turns an outline-style TFS 2015 post into a practical reference for real delivery workflows. Learn how to connect Agile planning, version control, CI builds, testing, and integrations in one on-prem pipeline.

Read More

TFS Interview Questions and Answers: Practical DevOps Guide

A structured, interview-focused guide to Team Foundation Server (TFS), including TFVC vs Git, build and reporting topics, and practical command examples. Use it to prepare concise, technically strong answers for common TFS interview scenarios.

Read More

How to Choose the Right Version Control System for Your Organization

Choosing a version control product is a process decision, not just a tooling decision. Use this practical framework to evaluate requirements, compare models, run pilots, and select confidently.

Read More
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
0
Would love your thoughts, please comment.x
()
x