Installing Terraform
Step-by-step guide to installing Terraform on macOS, Linux and Windows using the official HashiCorp package managers. Verify the install and run your first real Terraform configuration.
Before You Begin
Terraform ships as a single static binary: no runtime, no dependencies, no virtual environments. You download one file, place it on your PATH, and you are done. This simplicity is one of Terraform's best qualities.
This tutorial covers three installation paths:
- macOS: using Homebrew (recommended) or manual download
- Linux: using the HashiCorp apt/yum repository or manual download
- Windows: using Chocolatey/Winget or manual download
After installing, you will verify the setup and run a real (but harmless) Terraform configuration that writes a local file, no cloud account needed.
Installing on macOS
Option 1: Homebrew (Recommended)
Homebrew is the fastest and cleanest way to install Terraform on macOS. It handles upgrades automatically.
# Add the official HashiCorp tap
brew tap hashicorp/tap
# Install Terraform
brew install hashicorp/tap/terraform
To upgrade to a newer version later:
brew upgrade hashicorp/tap/terraform
Option 2: Manual Download
If you do not use Homebrew, download the binary directly:
- Go to releases.hashicorp.com/terraform
- Download the latest
.zipfordarwin_amd64(Intel) ordarwin_arm64(Apple Silicon) - Unzip it and move the binary to
/usr/local/bin:
unzip terraform_*.zip
sudo mv terraform /usr/local/bin/
Installing on Linux
Debian and Ubuntu (apt)
Add the official HashiCorp repository and install:
# Install dependencies
sudo apt-get update && sudo apt-get install -y gnupg software-properties-common
# Add HashiCorp's GPG key
wget -O- https://apt.releases.hashicorp.com/gpg | \
sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
# Add the repository
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
# Install Terraform
sudo apt-get update && sudo apt-get install terraform
To upgrade later, run sudo apt-get update && sudo apt-get upgrade terraform.
RHEL, CentOS, Amazon Linux (yum/dnf)
# Add the HashiCorp repo
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
# Install Terraform
sudo yum -y install terraform
Manual Download (any Linux)
# Find the latest version at releases.hashicorp.com
TERRAFORM_VERSION="1.9.0"
wget "https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_linux_amd64.zip"
unzip "terraform_${TERRAFORM_VERSION}_linux_amd64.zip"
sudo mv terraform /usr/local/bin/
sudo chmod +x /usr/local/bin/terraform
Tip: Replace
linux_amd64withlinux_arm64if you are on an ARM machine such as a Raspberry Pi or AWS Graviton instance.
Installing on Windows
Option 1: Winget (Windows Package Manager)
Windows 11 and recent Windows 10 builds include Winget:
winget install HashiCorp.Terraform
Option 2: Chocolatey
choco install terraform
Option 3: Manual Download
- Go to releases.hashicorp.com/terraform and download the latest
windows_amd64.zip - Unzip and place
terraform.exesomewhere, for exampleC:\tools\terraform\ - Add that folder to your system
PATH:- Open System Properties → Advanced → Environment Variables
- Under System Variables, find
Pathand click Edit - Click New and add
C:\tools\terraform
Managing Multiple Terraform Versions with tfenv
In production you often work across several projects that use different Terraform versions. tfenv is a version manager (similar to nvm for Node.js) that lets you switch between versions per project.
Install tfenv (macOS/Linux)
git clone --depth=1 https://github.com/tfutils/tfenv.git ~/.tfenv
echo 'export PATH="$HOME/.tfenv/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc
Install and switch versions
# List available versions
tfenv list-remote
# Install a specific version
tfenv install 1.9.0
# Use it
tfenv use 1.9.0
Create a .terraform-version file in your project root to pin the version automatically:
echo "1.9.0" > .terraform-version
Anyone who clones the repo and has tfenv installed will automatically use the same version.
Verifying the Installation
No matter how you installed it, confirm Terraform is available:
terraform -version
Expected output (version numbers will differ):
Terraform v1.9.0
on darwin_arm64
If you see command not found, the binary is not on your PATH. Double-check the installation steps above.
Enabling Shell Autocomplete
Terraform supports tab completion for commands and resource types. Enable it once:
terraform -install-autocomplete
Restart your shell. Now you can type terraform pl<TAB> and it will complete to terraform plan.
Your First Terraform Project
Let us run a real Terraform configuration, one that does not require a cloud account. You will use the built-in local provider to create a text file on your computer.
Step 1: Create a project folder
mkdir ~/terraform-hello
cd ~/terraform-hello
Step 2: Write the configuration
Create a file called main.tf with this content:
terraform {
required_providers {
local = {
source = "hashicorp/local"
version = "~> 2.4"
}
}
required_terraform = {
version = ">= 1.5.0"
}
}
resource "local_file" "hello" {
filename = "${path.module}/hello.txt"
content = "Hello from Terraform! Built by DevOpsLesson."
}
Step 3: Initialise the project
terraform init
Output:
Initializing the backend...
Initializing provider plugins...
- Finding hashicorp/local versions matching "~> 2.4"...
- Installing hashicorp/local v2.4.1...
Terraform has been successfully initialized!
terraform init downloaded the local provider plugin. You will see a new .terraform/ directory and a .terraform.lock.hcl file, commit the lock file to Git, and add .terraform/ to your .gitignore.
Step 4: Preview with terraform plan
terraform plan
Output:
Terraform will perform the following actions:
# local_file.hello will be created
+ resource "local_file" "hello" {
+ content = "Hello from Terraform! Built by DevOpsLesson."
+ filename = "/Users/you/terraform-hello/hello.txt"
+ id = (known after apply)
}
Plan: 1 to add, 0 to change, 0 to destroy.
+ means a resource will be created. Nothing has changed yet, this is just the preview.
Step 5: Apply the configuration
terraform apply
Terraform shows the plan again and prompts you:
Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.
Enter a value: yes
Type yes and press Enter. Terraform creates hello.txt.
cat hello.txt
# Hello from Terraform! Built by DevOpsLesson.
Step 6: Inspect the state file
cat terraform.tfstate
You will see a JSON file describing the resource Terraform just created. This is the state file: Terraform's record of what it manages. Never edit it by hand.
Step 7: Clean up
terraform destroy
Terraform will show what it plans to delete and ask for confirmation. Type yes. The hello.txt file is removed.
What Goes in .gitignore
When you commit a Terraform project, include this in your .gitignore:
# Terraform directory (downloaded providers)
.terraform/
# State files, store these in a remote backend, not Git
*.tfstate
*.tfstate.backup
# Override files
override.tf
override.tf.json
*_override.tf
*_override.tf.json
# Variables file with secrets
*.auto.tfvars
terraform.tfvars
Important: Never commit
terraform.tfstateorterraform.tfvars(which may contain secrets) to a public repository.
Recommended Project Structure
For a real project, a clean file layout looks like this:
my-project/
├── main.tf # Core resources
├── variables.tf # Input variable declarations
├── outputs.tf # Output value declarations
├── providers.tf # Provider configuration
├── versions.tf # terraform{} block with version constraints
└── terraform.tfvars # Variable values (gitignored if sensitive)
Splitting files by concern, rather than dumping everything in main.tf, makes large configurations much easier to navigate.
Knowledge Check
Question 1: The .terraform Directory
After running terraform init, a .terraform/ directory appears. What does it contain?
Question 2: The Lock File
You find a .terraform.lock.hcl file in your project. What should you do with it?
Question 3: terraform plan Output
You run terraform plan and see this line: "Plan: 1 to add, 0 to change, 0 to destroy." What does this mean?
Question 4: Gitignore Best Practice
Which of the following should you commit to a Git repository for a Terraform project?