DevOpsLesson
DevOpsLesson

Free, comprehensive DevOps tutorials and learning roadmaps. Master Docker, Kubernetes, CI/CD, and more.

Stay Updated

Get notified about new tutorials and features.

Tutorials

  • What is DevOps?
  • Docker Tutorial
  • Terraform Tutorial
  • CI/CD Pipeline
  • All Tutorials

Roadmaps

  • DevOps Engineer
  • Cloud Engineer
  • SRE Path
  • All Roadmaps

Company

  • About Us
  • Blog
  • Contact
  • Privacy Policy
  • Terms of Service

© 2026 DevOpsLesson. All rights reserved.

DOCKERKUBERNETESTERRAFORMAWSCI/CDLINUXGITDEVOPS ROADMAPCLOUD ROADMAPSRE ROADMAPGIT CHEATSHEETDOCKER CHEATSHEETK8S CHEATSHEETTF CHEATSHEETLINUX CHEATSHEETDOCKERFILE LINTERYAML VALIDATORCRON PARSERREGEX TESTER

Terraform Tutorial

Introduction to Terraform
Installing Terraform
The Terraform Core Workflow
Terraform Variables and Outputs
Terraform State and Backends
Terraform Modules
Terraform Data Sources
Terraform Count and for_each
Terraform Expressions and Functions
Terraform Dynamic Blocks
Terraform Workspaces
Terraform with AWS
Terraform Provisioners
Terraform CI/CD Pipeline
Terraform Security

Installing Terraform

PreviousPrev
Next

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:

  1. Go to releases.hashicorp.com/terraform
  2. Download the latest .zip for darwin_amd64 (Intel) or darwin_arm64 (Apple Silicon)
  3. 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_amd64 with linux_arm64 if 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

  1. Go to releases.hashicorp.com/terraform and download the latest windows_amd64.zip
  2. Unzip and place terraform.exe somewhere, for example C:\tools\terraform\
  3. Add that folder to your system PATH:
    • Open System Properties → Advanced → Environment Variables
    • Under System Variables, find Path and 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.tfstate or terraform.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

Exercise

Question 1: The .terraform Directory

After running terraform init, a .terraform/ directory appears. What does it contain?

Exercise

Question 2: The Lock File

You find a .terraform.lock.hcl file in your project. What should you do with it?

Exercise

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?

Exercise

Question 4: Gitignore Best Practice

Which of the following should you commit to a Git repository for a Terraform project?

PreviousPrev
Next

Continue Learning

Introduction to Terraform

Learn what Terraform is, how Infrastructure as Code works, why it beats clicking through cloud consoles, and how Terraform compares to Ansible, CloudFormation and Pulumi.

12 min read·Easy

The Terraform Core Workflow

The four commands that power every Terraform project, init, plan, apply, and destroy. Learn the complete workflow loop used by every DevOps engineer managing infrastructure as code.

5 min read·Easy

terraform init

Learn exactly what terraform init does, downloading providers, setting up backends, creating the lock file, and how to use init flags like -upgrade and -reconfigure.

10 min read·Easy

Explore Related Topics

AW

AWS Tutorials

Provision AWS infrastructure with Terraform

CI

CI/CD Tutorials

Run terraform plan/apply in CI pipelines

Try the Tool

YAML Validator

Validate Terraform and Kubernetes YAML configs before applying.

On This Page

Before You BeginInstalling on macOSOption 1: Homebrew (Recommended)Option 2: Manual DownloadInstalling on LinuxDebian and Ubuntu (apt)RHEL, CentOS, Amazon Linux (yum/dnf)Manual Download (any Linux)Installing on WindowsOption 1: Winget (Windows Package Manager)Option 2: ChocolateyOption 3: Manual DownloadManaging Multiple Terraform Versions with tfenvInstall tfenv (macOS/Linux)Install and switch versionsVerifying the InstallationEnabling Shell AutocompleteYour First Terraform ProjectStep 1: Create a project folderStep 2: Write the configurationStep 3: Initialise the projectStep 4: Preview with terraform planStep 5: Apply the configurationStep 6: Inspect the state fileStep 7: Clean upWhat Goes in .gitignoreRecommended Project StructureKnowledge Check