> **Page index:** https://km.oslo.systems/llms.txt lists every page on this site, with a link to its Markdown version.

# Terraform

[`tfswitch`](<https://warrensbox.github.io/terraform-switcher/>) is the recommended tool for managing your Terraform versions.

Install `tfswitch`:

```
brew install warrensbox/tap/tfswitch
```

Install Terraform:

```
tfswitch --latest
```

Add the location of the `tfswitch` binary to your `PATH`:

```
export PATH=$PATH:$HOME/bin
```

Verify that the correct version of Terraform is installed:

```
terraform version
```

Persist the location of Terraform versions managed by `tfswitch`:

```
echo 'export PATH=$PATH:$HOME/bin' >> ~/.zshrc
```

```
echo 'export PATH=$PATH:$HOME/bin' >> ~/.bash_profile
```

> [!TIP]
>
> **Executable PATH**
>
> When you open a new terminal window, the path to the Terraform binary managed by `tfswitch` is not set.
>
> To persist the path you must add the same command we ran manually in step 3 to your `~/.bash_profile` or `~/.zshrc` file. Below is an example of how to do this.

> [!TIP]
>
> **Caching Terraform plugins**
>
> Terraform can cache plugins. This saves you time when downloading the same plugin many times. More information can be found in [HashiCorp's configuration reference](<https://developer.hashicorp.com/terraform/cli/config/config-file#configuration-file-syntax>)

You can also refer to [HashiCorp's guide for installing Terraform](<https://developer.hashicorp.com/terraform/tutorials/aws-get-started/install-cli>)

## Add shell aliases (highly recommended)

Add the following aliases to your shell configuration file (for example `~/.bashrc` or `~/.zshrc`[^/set-up-your-machine/terraform/#1]):

```
alias tf='terraform'
alias tfa='tfswitch && terraform apply'
alias tfc='tfswitch && terraform console'
alias tfd='tfswitch && terraform destroy'
alias tff='tfswitch && terraform fmt'
alias tfi='tfswitch && terraform init'
alias tfo='tfswitch && terraform output'
alias tfp='tfswitch && terraform plan'
alias tfv='tfswitch && terraform validate'
alias tfl='tfswitch && terraform providers lock -platform=darwin_amd64 -platform=darwin_arm64 -platform=linux_amd64 -platform=windows_amd64'
```

[^/set-up-your-machine/terraform/#1]: See [What should/shouldn't go in `.zshenv`, `.zshrc`, `.zlogin`, `.zprofile`, `.zlogout`?](<https://unix.stackexchange.com/questions/71253/what-should-shouldnt-go-in-zshenv-zshrc-zlogin-zprofile-zlogout>).
