Tutorial 2: Install Terraform
Learning Objectives
- Install Terraform on your operating system
- Verify Terraform installation
- Understand Terraform version management
- Configure your development environment
System Requirements
Supported Operating Systems
- macOS (Intel and Apple Silicon)
- Windows (64-bit)
- Linux (various distributions)
- FreeBSD
- OpenBSD
- Solaris
Hardware Requirements
- Minimum 1GB RAM
- 100MB disk space for Terraform binary
- Internet connection for provider downloads
Installation Methods
Option 1: Package Managers (Recommended)
macOS - Using Homebrew
# Install Homebrew if not already installed
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# Install Terraform
brew tap hashicorp/tap
brew install hashicorp/tap/terraform
# Verify installation
terraform --version
Windows - Using Chocolatey
# Install Chocolatey if not already installed
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
# Install Terraform
choco install terraform
# Verify installation
terraform --version
Linux - Using Package Manager
Ubuntu/Debian:
# Add HashiCorp GPG key
curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
# Add HashiCorp repository
sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
# Update and install
sudo apt-get update && sudo apt-get install terraform
# Verify installation
terraform --version
CentOS/RHEL/Fedora:
# Add HashiCorp repository
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
# Verify installation
terraform --version
Option 2: Manual Installation
Download Binary
- Visit Terraform Downloads
- Select your operating system and architecture
- Download the appropriate binary
macOS/Linux Installation
# Download (replace with latest version URL)
curl -O https://releases.hashicorp.com/terraform/1.6.0/terraform_1.6.0_darwin_amd64.zip
# Unzip
unzip terraform_1.6.0_darwin_amd64.zip
# Move to PATH location
sudo mv terraform /usr/local/bin/
# Verify installation
terraform --version
Windows Installation
- Download the Windows zip file
- Extract
terraform.exe
- Add the directory containing
terraform.exe
to your system PATH - Open new command prompt and verify:
terraform --version
Verify Installation
Check Version
terraform --version
Expected output:
Terraform v1.6.0
on darwin_amd64
Test Basic Command
terraform help
Verify Terraform Commands
# List available commands
terraform
# Check specific command help
terraform init --help
terraform plan --help
terraform apply --help
Version Management
Check Current Version
terraform version
Version Constraints
In Terraform configurations, you can specify version constraints:
terraform {
required_version = ">= 1.0"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
Upgrading Terraform
Using Package Managers
# macOS
brew upgrade hashicorp/tap/terraform
# Windows
choco upgrade terraform
# Ubuntu/Debian
sudo apt-get update && sudo apt-get install terraform
# CentOS/RHEL
sudo yum update terraform
Manual Upgrade
- Download newer version binary
- Replace existing binary
- Verify new version
Development Environment Setup
IDE/Editor Extensions
Visual Studio Code
# Install Terraform extension
# Search for "HashiCorp Terraform" in VS Code extensions
Features:
- Syntax highlighting
- IntelliSense
- Error detection
- Formatting
Other Editors
- Vim: terraform-vim plugin
- Emacs: terraform-mode
- IntelliJ: Terraform plugin
- Sublime Text: Terraform package
Shell Auto-completion
Bash
# Add to ~/.bashrc
terraform -install-autocomplete
Zsh
# Add to ~/.zshrc
terraform -install-autocomplete
PowerShell
# Install module
Install-Module -Name PSReadLine -Force
terraform -install-autocomplete
Environment Configuration
Setting Up Working Directory
# Create project directory
mkdir terraform-projects
cd terraform-projects
# Create first project
mkdir my-first-project
cd my-first-project
Essential Environment Variables
Common Variables
# Set default region (AWS example)
export AWS_DEFAULT_REGION=us-west-2
# Set Terraform log level for debugging
export TF_LOG=INFO
# Set custom plugin directory
export TF_PLUGIN_CACHE_DIR="$HOME/.terraform.d/plugin-cache"
Create Plugin Cache Directory
mkdir -p ~/.terraform.d/plugin-cache
Terraform Configuration Directory
# Create Terraform configuration directory
mkdir -p ~/.terraform.d
# Structure:
# ~/.terraform.d/
# ├── plugin-cache/ # Cached provider plugins
# ├── credentials # Provider credentials (if needed)
# └── terraform.rc # CLI configuration
Troubleshooting Installation
Common Issues
Permission Denied
# If permission denied on macOS/Linux
sudo chown $(whoami) /usr/local/bin/terraform
chmod +x /usr/local/bin/terraform
Command Not Found
# Check if Terraform is in PATH
which terraform
# If not found, add to PATH
echo 'export PATH=$PATH:/usr/local/bin' >> ~/.bashrc
source ~/.bashrc
Windows PATH Issues
- Open System Properties
- Environment Variables
- Add Terraform directory to PATH
- Restart command prompt
Verification Checklist
-
terraform --version
shows correct version -
terraform help
displays command list -
terraform -install-autocomplete
works (optional) - IDE/editor has Terraform syntax highlighting
- Plugin cache directory created
First Configuration Test
Create Test Configuration
# Create test directory
mkdir terraform-test
cd terraform-test
# Create simple configuration
cat << EOF > main.tf
terraform {
required_version = ">= 1.0"
}
# This is a simple local resource for testing
resource "local_file" "test" {
content = "Hello, Terraform!"
filename = "test.txt"
}
EOF
Initialize and Test
# Initialize Terraform (downloads providers)
terraform init
# Validate configuration
terraform validate
# Plan changes
terraform plan
# Apply changes (creates test.txt)
terraform apply
# Clean up
terraform destroy
Next Steps
After successful installation:
- Complete Tutorial 3: Build Infrastructure
- Set up cloud provider credentials
- Create your first real infrastructure
Security Considerations
Binary Verification
# Verify GPG signature (optional but recommended)
curl -O https://releases.hashicorp.com/terraform/1.6.0/terraform_1.6.0_SHA256SUMS
curl -O https://releases.hashicorp.com/terraform/1.6.0/terraform_1.6.0_SHA256SUMS.sig
# Import HashiCorp public key
gpg --import hashicorp.asc
# Verify signature
gpg --verify terraform_1.6.0_SHA256SUMS.sig terraform_1.6.0_SHA256SUMS
# Verify binary checksum
shasum -a 256 -c terraform_1.6.0_SHA256SUMS
Keep Updated
- Subscribe to Terraform release notifications
- Regularly update to latest stable version
- Test updates in development environment first
Key Takeaways
- Choose package manager installation for easier updates
- Verify installation with
terraform --version
- Set up IDE extensions for better development experience
- Configure shell auto-completion for faster workflow
- Create plugin cache directory to speed up initialization