# Terraform

[`tfswitch`](https://warrensbox.github.io/terraform-switcher/){:target="_blank"} is the recommended tool for managing your Terraform versions.

Install `tfswitch`:

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

Install Terraform:

```sh
tfswitch --latest
```

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

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

Verify that the correct version of Terraform is installed:

```bash
terraform version
```

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

=== "macOS"

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

=== "Linux"

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

<!-- prettier-ignore-start -->
!!! 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)
<!-- prettier-ignore-end -->

You can also refer to [HashiCorp's guide for installing Terraform](https://developer.hashicorp.com/terraform/tutorials/aws-get-started/install-cli){:target="_blank"}

## Add shell aliases (highly recommended)

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

```sh
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'
```

[^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){:target="_blank"}.
