Terraform is a single binary written in Go, with no dependencies and no services to start. That makes installing it straightforward, but it also makes it easy to end up with three different versions scattered across your system and no idea which one is running. In this lesson we will install Terraform properly on Linux, macOS and Windows, verify the integrity of the downloaded binary, learn to juggle several versions at once, configure the editor and the CLI, and set up the infra/ directory for EcoMarket's platform team, where we will write the code for the rest of the course.

  1. Before you start: what you are about to install

Terraform is distributed as a ZIP archive containing a single executable. There are no background services, no database, no daemon. Installing Terraform means, literally, dropping that executable into some directory on your PATH.

Concept Detail
Binary name terraform (or terraform.exe on Windows)
Approximate size 80–110 MB uncompressed, depending on version and platform
Dependencies None
Reference version for the course 1.5 or later (we use 1.9.x as our example)
Free alternative OpenTofu, tofu binary
Where it usually ends up /usr/local/bin (Linux/macOS), %LOCALAPPDATA%\Programs (Windows)

You have two possible routes, and it is worth choosing deliberately:

  • Package manager (HashiCorp repository, Homebrew, winget, Chocolatey): convenient, updates with the system, ideal for teams and personal workstations.
  • Version manager (tfenv): lets you keep several versions installed and switch between them per project. This is the recommendation if you work with more than one infrastructure repository, which is what will happen to the EcoMarket team as soon as they have more than one project.

This decision tree summarises which method suits you:

flowchart TD
    A{"Where are you<br/>installing it?"} --> B["Workstation"]
    A --> C["Container or<br/>CI runner"]

    B --> D{"Will you work with more<br/>than one infrastructure<br/>repository?"}
    D -- Yes --> E["tfenv / tofuenv<br/>+ .terraform-version"]
    D -- No --> F{"Operating<br/>system"}
    F --> G["Linux: HashiCorp<br/>repository"]
    F --> H["macOS: Homebrew<br/>hashicorp/tap"]
    F --> I["Windows: winget<br/>or Chocolatey"]

    C --> J["Manual binary<br/>with GPG + SHA-256<br/>verification"]

We will begin with the classic installation and move on to tfenv in section 7.

  1. Installing on Linux

This is the most maintainable approach on Debian/Ubuntu, because updates arrive with apt upgrade.

# 1. Install the utilities needed to add a signed repository.
sudo apt-get update
sudo apt-get install -y gnupg software-properties-common curl

# 2. Download HashiCorp's GPG key and store it in binary format
#    in APT's keyring. The key is used to check that the packages
#    we install really do come from HashiCorp.
wget -O- https://apt.releases.hashicorp.com/gpg | \
  gpg --dearmor | \
  sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg > /dev/null

# 3. Add the repository, pointing at that key (signed-by) and at our
#    distribution's release (lsb_release -cs -> e.g. "noble").
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] \
https://apt.releases.hashicorp.com $(lsb_release -cs) main" | \
  sudo tee /etc/apt/sources.list.d/hashicorp.list

# 4. Refresh the package index and install.
sudo apt-get update
sudo apt-get install -y terraform

What each step does:

  • gpg --dearmor converts the key from text format (ASCII armored) into the binary format APT expects in /usr/share/keyrings.
  • signed-by= ties that repository to that specific key. Without it, the key would be valid for any repository on the system, which is far less secure.
  • lsb_release -cs returns your distribution's codename. If you are on a very recent release and the repository does not carry it yet, replace that command with the name of the previous LTS release.

For RHEL / Fedora / Rocky / Alma:

sudo dnf install -y dnf-plugins-core
sudo dnf config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
sudo dnf install -y terraform

2.2. Option B: manual binary

Useful in containers, on machines with no package manager, or when you need an exact version.

# We put the version in a variable so we do not repeat it and can change it easily.
TF_VERSION="1.9.8"

# Detect the architecture and translate it into the name HashiCorp uses:
# x86_64 -> amd64 ; aarch64 -> arm64
ARCH=$(uname -m)
case "$ARCH" in
  x86_64)  TF_ARCH="amd64" ;;
  aarch64) TF_ARCH="arm64" ;;
  *) echo "Unsupported architecture: $ARCH"; exit 1 ;;
esac

# Work in a temporary directory so we do not clutter the project.
cd "$(mktemp -d)"

# Download the binary's ZIP archive.
curl -fsSLO "https://releases.hashicorp.com/terraform/${TF_VERSION}/terraform_${TF_VERSION}_linux_${TF_ARCH}.zip"

# Unzip it (it produces a single file: "terraform").
unzip "terraform_${TF_VERSION}_linux_${TF_ARCH}.zip"

# Move it into a PATH directory and make it executable.
sudo install -o root -g root -m 0755 terraform /usr/local/bin/terraform

Details worth understanding:

  • curl -fsSLO: -f fails on an HTTP error instead of saving an error page; -s/-S silences progress but still shows errors; -L follows redirects; -O saves under the remote file's name.
  • install -m 0755 copies the file and sets permissions and ownership in a single operation, which avoids the classic "permission denied" after a cp.
  • If you do not have sudo, you can leave the binary in ~/.local/bin and make sure that directory is on your PATH.

  1. Installing on macOS

The standard route is Homebrew, using HashiCorp's official tap:

# Add HashiCorp's official formula repository.
brew tap hashicorp/tap

# Install Terraform from that tap.
brew install hashicorp/tap/terraform

# To upgrade later on:
brew upgrade hashicorp/tap/terraform

Beware of the generic formula: brew install terraform (without the tap) may install an old version from before the licence change, or in some setups install OpenTofu. Always use the full path hashicorp/tap/terraform so you know what you are installing.

If you prefer OpenTofu on macOS:

brew install opentofu   # installs the "tofu" binary

The manual binary installation works on macOS too; you just swap linux for darwin in the download URL. Bear in mind that on Apple Silicon Macs the architecture is arm64, and that macOS may ask you to authorise a binary downloaded through the browser (this does not happen with curl).

  1. Installing on Windows

4.1. winget (bundled with Windows 10/11)

# Installs Terraform and adds it to the user's PATH automatically.
winget install --id HashiCorp.Terraform -e

# To upgrade:
winget upgrade --id HashiCorp.Terraform -e

The -e (exact) flag stops winget installing some other similarly named package.

4.2. Chocolatey

# Requires a PowerShell console running as administrator.
choco install terraform -y

# Upgrade:
choco upgrade terraform -y

4.3. Manual, with PATH

  1. Download the Windows ZIP from https://developer.hashicorp.com/terraform/downloads.
  2. Extract terraform.exe, for example into C:\Tools\terraform\.
  3. Add that directory to the user's PATH:
# Read the USER's current PATH (not the system one) and append the directory.
$dir = "C:\Tools\terraform"
$current = [Environment]::GetEnvironmentVariable("Path", "User")
[Environment]::SetEnvironmentVariable("Path", "$current;$dir", "User")

# Close and reopen the terminal for the change to take effect.

Recommendation for Windows: if you are going to work with Terraform seriously, consider using WSL2 with a Linux distribution. Many examples, scripts and ecosystem tools assume a Unix-like environment, and you will avoid problems with line endings (CRLF) and with paths.

  1. Verification: terraform version and terraform -help

With Terraform installed, check that the system finds it:

$ terraform version
Terraform v1.9.8
on linux_amd64

If you also have providers already downloaded in a project, the command lists them:

$ terraform version
Terraform v1.9.8
on linux_amd64
+ provider registry.terraform.io/hashicorp/local v2.5.2
+ provider registry.terraform.io/hashicorp/random v3.6.3

Check which binary is actually running as well, something crucial once you have several installations:

# Path of the executable the shell will resolve.
$ which terraform
/usr/local/bin/terraform

# Every "terraform" on the PATH, in priority order.
$ type -a terraform
terraform is /usr/local/bin/terraform
terraform is /home/dev/.tfenv/bin/terraform

If terraform version warns that a newer version is available, that is not an error: it is just an informational notice.

Now the general help:

$ terraform -help
Usage: terraform [global options] <subcommand> [args]

The available commands for execution are listed below.
The primary workflow commands are given first, followed by
less common or more advanced commands.

Main commands:
  init          Prepare your working directory for other commands
  validate      Check whether the configuration is valid
  plan          Show changes required by the current configuration
  apply         Create or update infrastructure
  destroy       Destroy previously-created infrastructure

All other commands:
  console       Try Terraform expressions at an interactive command prompt
  fmt           Reformat your configuration in the standard style
  ...

And the help for a specific subcommand:

$ terraform plan -help

We will study the commands themselves in the lesson Terraform Core Concepts; here we are only confirming that the binary responds.

  1. Verifying the GPG signature of the downloaded binary

If you installed through a package repository, APT or DNF already did the verification for you. But if you download the ZIP by hand — common in container images and CI runners — you must verify that what you downloaded is authentic. It is a supply-chain security practice that any serious platform team applies.

Each release publishes three artefacts alongside the ZIP:

File Contents
terraform_<ver>_SHA256SUMS SHA-256 hashes of every ZIP in that release
terraform_<ver>_SHA256SUMS.sig GPG signature of that hashes file
terraform_<ver>_linux_amd64.zip The compressed binary

The full procedure:

TF_VERSION="1.9.8"
BASE="https://releases.hashicorp.com/terraform/${TF_VERSION}"
cd "$(mktemp -d)"

# 1. Download the binary, the hashes and the signature.
curl -fsSLO "${BASE}/terraform_${TF_VERSION}_linux_amd64.zip"
curl -fsSLO "${BASE}/terraform_${TF_VERSION}_SHA256SUMS"
curl -fsSLO "${BASE}/terraform_${TF_VERSION}_SHA256SUMS.sig"

# 2. Import HashiCorp's public key.
curl -fsSL https://www.hashicorp.com/.well-known/pgp-key.txt | gpg --import

# 3. Verify that the hashes file is signed by that key.
gpg --verify "terraform_${TF_VERSION}_SHA256SUMS.sig" \
             "terraform_${TF_VERSION}_SHA256SUMS"

# 4. Check that OUR zip's hash matches the one in the signed file.
#    --ignore-missing avoids errors about the other ZIPs we did not download.
sha256sum --ignore-missing -c "terraform_${TF_VERSION}_SHA256SUMS"

Expected output from step 3:

gpg: Signature made Wed 06 Nov 2024 10:12:31 AM CET
gpg:                using RSA key 374EC75B485913604A831CC7C820C6D5CD27AB87
gpg: Good signature from "HashiCorp Security (hashicorp.com/security) <[email protected]>" [unknown]
gpg: WARNING: This key is not certified with a trusted signature!
gpg:          There is no indication that the signature belongs to the owner.

How to read this:

  • Good signature is what we are after: the hashes file has not been tampered with and was signed by the holder of that key.
  • The WARNING does not mean something is wrong. It indicates that you have not personally certified that key in your web of trust. If you want the warning gone, you can sign the key locally with gpg --lsign-key <fingerprint> after checking the fingerprint against the one published on HashiCorp's site.

And from step 4:

terraform_1.9.8_linux_amd64.zip: OK

If instead of OK you see FAILED, do not use that file: delete it and download it again.

  1. Several versions at once with tfenv (and tofuenv)

Sooner or later you will have an old repository that needs Terraform 1.5 and a new one on 1.9. tfenv solves that problem the same way nvm solves it for Node.js.

Installation:

# Clone tfenv into the home directory.
git clone --depth=1 https://github.com/tfutils/tfenv.git ~/.tfenv

# Add its bin directory to the PATH (adjust to ~/.zshrc if you use zsh).
echo 'export PATH="$HOME/.tfenv/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc

Important: the tfenv directory must come first on the PATH so that its shim takes priority over a terraform installed by the package manager. If you have both, the cleanest option is to uninstall the system one.

Everyday use:

# List the versions available to install (the most recent ones).
$ tfenv list-remote | head -5
1.9.8
1.9.7
1.9.6
1.9.5
1.9.4

# Install a specific version.
$ tfenv install 1.9.8
Installing Terraform v1.9.8
Downloading release tarball from https://releases.hashicorp.com/...
Archive:  tfenv_download.XXXX/terraform_1.9.8_linux_amd64.zip
  inflating: /home/dev/.tfenv/versions/1.9.8/terraform
Installation of terraform v1.9.8 successful.

# Set the system default version.
$ tfenv use 1.9.8
Switching default version to v1.9.8

# See which versions you have installed (the asterisk marks the active one).
$ tfenv list
* 1.9.8 (set by /home/dev/.tfenv/version)
  1.5.7

The most valuable part is pinning the version per project with a .terraform-version file at the root of the repository:

# Inside EcoMarket's infrastructure repository
$ echo "1.9.8" > .terraform-version

# From now on, in this directory, tfenv will always use that version,
# and will install it automatically if it is missing:
$ tfenv install
$ terraform version
Terraform v1.9.8

That file goes into Git: it guarantees that the whole EcoMarket team and the CI pipeline use exactly the same version, avoiding the classic "the plan looks different on my machine".

For OpenTofu there is the equivalent tofuenv, with the same commands over the tofu binary and a .opentofu-version file:

git clone --depth=1 https://github.com/tofuutils/tofuenv.git ~/.tofuenv
echo 'export PATH="$HOME/.tofuenv/bin:$PATH"' >> ~/.bashrc
tofuenv install 1.8.4
tofuenv use 1.8.4

A comparative summary:

Method Several versions Updating Best for
APT/DNF repository No apt upgrade Servers, runners on a single version
Homebrew Limited brew upgrade Simple macOS workstations
winget / Chocolatey No winget upgrade Windows workstations
Manual binary Manual Manual Containers, exact versions
tfenv / tofuenv Yes, per project tfenv install Day-to-day work across several repositories

  1. Shell autocompletion

Terraform can install its own autocompletion for bash and zsh:

# Installs autocompletion into your shell's profile file.
$ terraform -install-autocomplete

# Reload the profile (or open a new terminal).
$ source ~/.bashrc

The command adds a line complete -C /usr/local/bin/terraform terraform to your ~/.bashrc (or the equivalent block in ~/.zshrc). From then on:

$ terraform pl<TAB>        # completes to "plan"
$ terraform apply -<TAB>   # suggests the available flags

To remove it: terraform -uninstall-autocomplete.

If you use tfenv, run the command after tfenv is on your PATH, so that the registered path is the shim's and not that of one specific binary.

  1. Configuring the editor: VS Code and terraform fmt on save

Writing HCL with no editor support is needlessly uncomfortable. The official extension adds highlighting, autocompletion of resources and attributes, real-time validation, go-to-definition and formatting.

Installation from the terminal:

# HashiCorp's official Terraform extension.
code --install-extension hashicorp.terraform

Or from VS Code's extensions view by searching for "HashiCorp Terraform" (check that the publisher is HashiCorp; there are older third-party extensions around).

Recommended configuration in the EcoMarket repository's .vscode/settings.json, so the whole team shares the same rules:

{
  "[terraform]": {
    "editor.defaultFormatter": "hashicorp.terraform",
    "editor.formatOnSave": true,
    "editor.tabSize": 2
  },
  "[terraform-vars]": {
    "editor.defaultFormatter": "hashicorp.terraform",
    "editor.formatOnSave": true,
    "editor.tabSize": 2
  },
  "terraform.validation.enableEnhancedValidation": true,
  "files.eol": "\n",
  "files.trimTrailingWhitespace": true,
  "files.insertFinalNewline": true
}

What each option does:

  • [terraform] and [terraform-vars]: per-file-type settings. The second covers .tfvars files, which VS Code treats as a separate language.
  • editor.formatOnSave: runs terraform fmt every time you save. It puts an end to indentation arguments forever and keeps pull request diffs clean.
  • editor.tabSize: 2: the canonical HCL style uses 2 spaces.
  • files.eol: "\n": forces Unix line endings, important if anyone on the team works on Windows.

If you do not use VS Code, the extension is built on terraform-ls, the official language server, which works in any LSP-capable editor (Neovim, Helix, Sublime, IntelliJ with its plugin).

  1. The CLI configuration file and the plugin cache

Terraform reads a global configuration file, independent of any project:

System Path
Linux / macOS ~/.terraformrc
Windows %APPDATA%\terraform.rc

Its most profitable use from day one is the plugin cache. By default, each project downloads its own copy of every provider into .terraform/providers/. If you have five projects using the AWS provider (which weighs in at hundreds of MB), you will have five copies. The cache makes them download only once.

# 1. Create the cache directory.
mkdir -p ~/.terraform.d/plugin-cache

# 2. Write the CLI configuration file.
cat > ~/.terraformrc <<'EOF'
# Shared directory where Terraform will store downloaded plugins.
plugin_cache_dir = "$HOME/.terraform.d/plugin-cache"

# If a provider is not in the cache, it is downloaded and added to it.
# With this option set to "true", Terraform would NOT download anything missing.
plugin_cache_may_break_dependency_lock_file = false
EOF

With the cache enabled, the first terraform init in a new project takes seconds instead of minutes and uses no extra bandwidth.

Another frequent use is the provider_installation block, for working without internet access or against an internal mirror:

# ~/.terraformrc — example of a local provider mirror
provider_installation {
  filesystem_mirror {
    path    = "/opt/terraform/providers"
    include = ["registry.terraform.io/*/*"]
  }
  direct {
    exclude = ["registry.terraform.io/*/*"]
  }
}

This block tells Terraform: "look for every provider in that local directory and do not go out to the public registry". It is the typical pattern for corporate environments with restricted networks. We will not need it in this course, but it is worth knowing it exists.

  1. Useful environment variables

Terraform is also configured through environment variables. These are the ones that matter now:

Variable What it does Example
TF_PLUGIN_CACHE_DIR Plugin cache, equivalent to plugin_cache_dir and taking priority over it ~/.terraform.d/plugin-cache
TF_CLI_ARGS Arguments appended to every command -no-color
TF_CLI_ARGS_plan Arguments appended only to terraform plan -parallelism=5
TF_CLI_CONFIG_FILE Alternative path to the .terraformrc file /etc/terraform/cli.tfrc
TF_INPUT Set to 0/false, Terraform never prompts for keyboard input 0 in CI
TF_IN_AUTOMATION Set to any non-empty value, it tones down messages suggesting interactive commands true in CI
TF_DATA_DIR Changes the location of the .terraform directory /tmp/tfdata
TF_LOG Debug logging level Covered in module 9

A practical example for EcoMarket's CI runner:

# No ANSI colours (so the pipeline logs stay readable),
# no interactive prompts, and a cache shared between runs.
export TF_CLI_ARGS="-no-color"
export TF_INPUT=0
export TF_IN_AUTOMATION=true
export TF_PLUGIN_CACHE_DIR="/var/cache/terraform/plugins"

And one for your own machine, if you are short on CPU and want plan to be less aggressive:

# Limits concurrent operations to 5 in the plan command ONLY.
export TF_CLI_ARGS_plan="-parallelism=5"

Careful with TF_CLI_ARGS: it applies to every subcommand. If you put an argument there that only plan understands, commands such as init will fail. Use the specific TF_CLI_ARGS_<command> variant whenever you can.

As for cloud provider credentials (AWS_PROFILE, ARM_CLIENT_ID and so on): we do not need them in this module, because we will work only with local and random. They are covered in module 5.

  1. EcoMarket's working directory

Let us create the skeleton of the repository where the platform team will write the course's code. In the next lesson we will explain what each file means, and in 01-04 we will fill them in and run them.

# Create the infrastructure repository.
mkdir -p ~/projects/ecomarket-infra/infra
cd ~/projects/ecomarket-infra

# Set up version control: infrastructure lives in Git
# from minute one.
git init -b main

# Pin the project's Terraform version (tfenv will read it).
echo "1.9.8" > .terraform-version

# Create the empty files we will use in lesson 01-04.
cd infra
touch versions.tf providers.tf main.tf variables.tf outputs.tf terraform.tfvars

And the .gitignore, which is essential from the very beginning so you never push provider binaries to the repository or, far worse, state files containing sensitive data:

cd ~/projects/ecomarket-infra
cat > .gitignore <<'EOF'
# Working directory holding the downloaded plugins: regenerated by "init".
.terraform/

# State files: they can contain secrets and must NOT be versioned.
*.tfstate
*.tfstate.*
.terraform.tfstate.lock.info

# Variable files with local or sensitive values.
*.auto.tfvars
crash.log
crash.*.log

# Plans saved in binary form.
*.tfplan
EOF

Notice one deliberate exception: .terraform.lock.hcl is NOT ignored. That file pins the exact provider versions and must be versioned so the whole team uses the same ones. We will look at it in the next lesson.

The result:

$ tree -a -I '.git' ~/projects/ecomarket-infra
/home/dev/projects/ecomarket-infra
├── .gitignore
├── .terraform-version
└── infra
    ├── main.tf
    ├── outputs.tf
    ├── providers.tf
    ├── terraform.tfvars
    ├── variables.tf
    └── versions.tf

A final check, now inside the working directory:

$ cd ~/projects/ecomarket-infra/infra
$ terraform version
Terraform v1.9.8
on linux_amd64

$ terraform fmt -check
# No output and exit code 0: there is nothing to reformat
# (unsurprisingly, the files are empty).

Environment ready.

Common Mistakes and Tips

  • terraform: command not found after installing. The binary is not on the PATH, or the terminal was opened before the change. Check with echo $PATH and open a new terminal. On Windows, close and reopen the console after modifying the PATH.
  • Two versions living side by side. Typical symptom: terraform version shows a version you were not expecting. Diagnose it with type -a terraform and remove the surplus installation. If you use tfenv, uninstall the system package.
  • Installing terraform from Homebrew's generic formula and unknowingly ending up with an old version or with OpenTofu. Use hashicorp/tap/terraform.
  • Not verifying a manual download. Container images and CI runners are where it matters most: it is a classic supply-chain attack point. Automate the SHA-256 check in your Dockerfile.
  • Putting a command-specific argument in TF_CLI_ARGS. If you export TF_CLI_ARGS="-auto-approve", terraform init will fail with a confusing error about an unknown flag. Use TF_CLI_ARGS_apply.
  • Forgetting the .gitignore. Pushing a terraform.tfstate to a repository, even a private one, is a data leak: it may contain database passwords in plain text.
  • Tip: pin the version with .terraform-version and commit it from the very first commit. It saves hours of debugging version differences.
  • Tip: enable the plugin cache today, not once you have ten projects. It is one line in ~/.terraformrc.
  • Tip: if you work on Windows without WSL, set files.eol: "\n" in VS Code and git config --global core.autocrlf input to stop your .tf files changing line endings between machines.

Exercises

Exercise 1: a verified installation from scratch

Install Terraform 1.9.8 on your system using the manual binary method, verifying the GPG signature and the SHA-256 hash before installing it. Then check:

  1. That terraform version returns v1.9.8.
  2. Which path the binary is running from.
  3. That there is no other Terraform installation on the PATH.

Exercise 2: the EcoMarket project environment

Set up the platform team's complete working environment:

  1. Install tfenv and pin version 1.9.8 as the project version through .terraform-version.
  2. Install 1.5.7 as well and check that entering the project directory selects 1.9.8.
  3. Enable the plugin cache in ~/.terraformrc.
  4. Enable shell autocompletion.
  5. Create the ecomarket-infra repository with its .gitignore and make the first commit.

Exercise 3: environment variables for CI

The EcoMarket team wants to run Terraform in its pipeline. Write the block of environment variables the runner should export so that:

  • The output carries no colour codes.
  • Terraform never waits for keyboard input.
  • Messages are adapted to an automated environment.
  • Plugins are cached in /var/cache/terraform/plugins.
  • Only the plan command limits concurrency to 10 operations.

Explain why the last condition cannot be solved with TF_CLI_ARGS.


Solutions

Solution 1:

TF_VERSION="1.9.8"
BASE="https://releases.hashicorp.com/terraform/${TF_VERSION}"
cd "$(mktemp -d)"

curl -fsSLO "${BASE}/terraform_${TF_VERSION}_linux_amd64.zip"
curl -fsSLO "${BASE}/terraform_${TF_VERSION}_SHA256SUMS"
curl -fsSLO "${BASE}/terraform_${TF_VERSION}_SHA256SUMS.sig"

curl -fsSL https://www.hashicorp.com/.well-known/pgp-key.txt | gpg --import
gpg --verify "terraform_${TF_VERSION}_SHA256SUMS.sig" "terraform_${TF_VERSION}_SHA256SUMS"
sha256sum --ignore-missing -c "terraform_${TF_VERSION}_SHA256SUMS"

unzip "terraform_${TF_VERSION}_linux_amd64.zip"
sudo install -m 0755 terraform /usr/local/bin/terraform

Checks:

$ terraform version
Terraform v1.9.8
on linux_amd64

$ command -v terraform
/usr/local/bin/terraform

$ type -a terraform
terraform is /usr/local/bin/terraform

If type -a returned more than one line, there would be duplicate installations and the first would win. You should remove the surplus ones (sudo apt remove terraform, brew uninstall terraform and so on).

Solution 2:

# 1 and 2. tfenv with two versions
git clone --depth=1 https://github.com/tfutils/tfenv.git ~/.tfenv
echo 'export PATH="$HOME/.tfenv/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc
tfenv install 1.9.8
tfenv install 1.5.7
tfenv use 1.5.7          # global version: the old one, on purpose

mkdir -p ~/projects/ecomarket-infra/infra
cd ~/projects/ecomarket-infra
echo "1.9.8" > .terraform-version
terraform version        # -> v1.9.8, because the file rules in this directory

# 3. Plugin cache
mkdir -p ~/.terraform.d/plugin-cache
printf 'plugin_cache_dir = "$HOME/.terraform.d/plugin-cache"\n' > ~/.terraformrc

# 4. Autocompletion
terraform -install-autocomplete
source ~/.bashrc

# 5. Repository and first commit
git init -b main
cat > .gitignore <<'EOF'
.terraform/
*.tfstate
*.tfstate.*
.terraform.tfstate.lock.info
*.auto.tfvars
*.tfplan
crash.log
EOF
touch infra/main.tf infra/providers.tf infra/variables.tf \
      infra/outputs.tf infra/versions.tf infra/terraform.tfvars
git add .
git commit -m "Skeleton of the EcoMarket infrastructure repository"

The key check for point 2 is that outside the directory terraform version returns v1.5.7 and inside it returns v1.9.8.

Solution 3:

export TF_CLI_ARGS="-no-color"
export TF_INPUT=0
export TF_IN_AUTOMATION=true
export TF_PLUGIN_CACHE_DIR="/var/cache/terraform/plugins"
export TF_CLI_ARGS_plan="-parallelism=10"

The last condition cannot be solved with TF_CLI_ARGS because that variable appends its arguments to every subcommand. Commands such as terraform init, terraform fmt or terraform output do not accept -parallelism and would abort with an unknown-flag error. TF_CLI_ARGS_plan confines the argument to the subcommand named.

(Note: -no-color in TF_CLI_ARGS is safe, because practically every subcommand accepts it.)

Conclusion

You now have a professional Terraform working environment:

  • Terraform installed through the method that suits your operating system, with the manual binary as the alternative for containers and CI.
  • The GPG signature and the SHA-256 hash verified, a supply-chain security practice you should automate.
  • tfenv managing several versions, with .terraform-version pinning the project's version for the whole team.
  • Shell autocompletion enabled and VS Code formatting with terraform fmt on save.
  • ~/.terraformrc with a shared plugin cache, and the key environment variables (TF_CLI_ARGS, TF_PLUGIN_CACHE_DIR, TF_INPUT) understood.
  • The ecomarket-infra repository created, with a correct .gitignore and its infra/ directory ready.
  • OpenTofu and tofuenv identified as the free, compatible alternative.

Before writing real code it pays to master the vocabulary. In the next lesson, Terraform Core Concepts, we will see exactly what a provider, a resource, an attribute and the state are; how the dependency graph between EcoMarket's resources is built; what the +, ~, -/+ and - symbols in a plan mean; and what the .terraform/ directory and the .terraform.lock.hcl file we have just decided to version actually hold.

© Copyright 2026. All rights reserved