Terraform on Ubuntu
Setting Up Terraform, Packer, and Recommended Add-ons on Ubuntu 24.04
This guide will walk you through installing Terraform, Packer, and recommended add-ons on a new Ubuntu 24.04 system.
Prerequisites
First, update your system:
sudo apt updatesudo apt upgrade -y
Install required dependencies:
sudo apt install -y curl wget unzip software-properties-common gnupg lsb-release apt-transport-https ca-certificates
Installing Terraform
Add HashiCorp GPG Key and Repository
wget -O- https://apt.releases.hashicorp.com/gpg | gpg --dearmor | sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg >/dev/nullecho "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.listsudo apt update
Install Terraform
sudo apt install terraform -y
Verify Installation
terraform --version
Installing Packer
Packer is also available from the HashiCorp repository we just added:
sudo apt install packer -y
Verify Installation
packer --version
Recommended Add-ons
1. Terraform-docs
Terraform-docs generates documentation from Terraform modules:
TFDOCS_VERSION=v0.16.0curl -Lo ./terraform-docs.tar.gz https://github.com/terraform-docs/terraform-docs/releases/download/${TFDOCS_VERSION}/terraform-docs-${TFDOCS_VERSION}-linux-amd64.tar.gztar -xzf terraform-docs.tar.gzchmod +x terraform-docssudo mv terraform-docs /usr/local/bin/
Verify:
terraform-docs --version
2. TFLint
TFLint is a Terraform linter that helps identify potential issues:
curl -s https://raw.githubusercontent.com/terraform-linters/tflint/master/install_linux.sh | bash
Verify:
tflint --version
3. Terragrunt
Terragrunt is a thin wrapper for Terraform that provides extra tools for keeping configurations DRY:
TERRAGRUNT_VERSION=v0.45.0wget https://github.com/gruntwork-io/terragrunt/releases/download/${TERRAGRUNT_VERSION}/terragrunt_linux_amd64chmod +x terragrunt_linux_amd64sudo mv terragrunt_linux_amd64 /usr/local/bin/terragrunt
Verify:
terragrunt --version
4. AWS CLI (Optional, for AWS users)
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"unzip awscliv2.zipsudo ./aws/install
Verify:
aws --version
5. Azure CLI (Optional, for Azure users)
curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash
Verify:
az --version
6. Google Cloud SDK (Optional, for GCP users)
echo "deb [signed-by=/usr/share/keyrings/cloud.google.gpg] https://packages.cloud.google.com/apt cloud-sdk main" | sudo tee -a /etc/apt/sources.list.d/google-cloud-sdk.listcurl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key --keyring /usr/share/keyrings/cloud.google.gpg add -sudo apt update && sudo apt install google-cloud-sdk -y
Verify:
gcloud --version
Setting Up Shell Completion
Terraform Completion
For Bash:
terraform -install-autocomplete
For ZSH, add to your ~/.zshrc:
autoload -U +X bashcompinit && bashcompinitcomplete -o nospace -C /usr/bin/terraform terraform
Packer Completion
For Bash users, add to your ~/.bashrc:
complete -C /usr/bin/packer packer
For ZSH users, add to your ~/.zshrc:
autoload -U +X bashcompinit && bashcompinitcomplete -o nospace -C /usr/bin/packer packer
VSCode Integration (Optional)
Install Visual Studio Code:
sudo apt install software-properties-common apt-transport-https wget -ywget -q https://packages.microsoft.com/keys/microsoft.asc -O- | sudo apt-key add -sudo add-apt-repository "deb [arch=amd64] https://packages.microsoft.com/repos/vscode stable main"sudo apt updatesudo apt install code -y
Recommended VSCode extensions for Terraform:
- HashiCorp Terraform
- Terraform Docs Snippets
- Terragrunt
Install these extensions from the command line:
code --install-extension hashicorp.terraformcode --install-extension mindginative.terraform-snippetscode --install-extension 4ops.terraform
Final Steps
Remember to reload your shell configuration after making changes:
source ~/.bashrc # for Bash# ORsource ~/.zshrc # for ZSH
Now your Ubuntu 24.04 system is ready for infrastructure as code development with Terraform and Packer!
Complete script
#!/bin/bash# Script to set up Terraform, Packer, and recommended add-ons on Ubuntu 24.04# Optimized for ZSH users
# Exit on errorset -e
echo "=== Starting Terraform, Packer, and tools installation ==="
# Update systemecho "=== Updating system packages ==="sudo apt updatesudo apt upgrade -y
# Install dependenciesecho "=== Installing dependencies ==="sudo apt install -y curl wget unzip software-properties-common gnupg lsb-release apt-transport-https ca-certificates jq git
# Add HashiCorp GPG key and repositoryecho "=== Adding HashiCorp repository ==="wget -O- https://apt.releases.hashicorp.com/gpg | gpg --dearmor | sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg >/dev/nullecho "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.listsudo apt update
# Install Terraform and Packerecho "=== Installing Terraform and Packer ==="sudo apt install -y terraform packer
# Verify installationsterraform --versionpacker --version
# Install terraform-docsecho "=== Installing terraform-docs ==="TFDOCS_VERSION=v0.16.0curl -Lo ./terraform-docs.tar.gz https://github.com/terraform-docs/terraform-docs/releases/download/${TFDOCS_VERSION}/terraform-docs-${TFDOCS_VERSION}-linux-amd64.tar.gztar -xzf terraform-docs.tar.gzchmod +x terraform-docssudo mv terraform-docs /usr/local/bin/rm terraform-docs.tar.gzterraform-docs --version
# Install TFLintecho "=== Installing TFLint ==="curl -s https://raw.githubusercontent.com/terraform-linters/tflint/master/install_linux.sh | bashtflint --version
# Install Terragruntecho "=== Installing Terragrunt ==="TERRAGRUNT_VERSION=v0.45.0wget https://github.com/gruntwork-io/terragrunt/releases/download/${TERRAGRUNT_VERSION}/terragrunt_linux_amd64chmod +x terragrunt_linux_amd64sudo mv terragrunt_linux_amd64 /usr/local/bin/terragruntterragrunt --version
# Prompt for cloud provider CLI installationsecho "=== Cloud Provider CLI installations ==="read -p "Install AWS CLI? (y/n): " install_awsif [[ $install_aws == "y" || $install_aws == "Y" ]]; then echo "Installing AWS CLI..." curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" unzip awscliv2.zip sudo ./aws/install rm -rf aws awscliv2.zip aws --versionfi
read -p "Install Azure CLI? (y/n): " install_azureif [[ $install_azure == "y" || $install_azure == "Y" ]]; then echo "Installing Azure CLI..." curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash az --versionfi
read -p "Install Google Cloud SDK? (y/n): " install_gcpif [[ $install_gcp == "y" || $install_gcp == "Y" ]]; then echo "Installing Google Cloud SDK..." echo "deb [signed-by=/usr/share/keyrings/cloud.google.gpg] https://packages.cloud.google.com/apt cloud-sdk main" | sudo tee -a /etc/apt/sources.list.d/google-cloud-sdk.list curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key --keyring /usr/share/keyrings/cloud.google.gpg add - sudo apt update && sudo apt install google-cloud-sdk -y gcloud --versionfi
# Set up shell completions for ZSHecho "=== Setting up ZSH completions ==="if [ -f ~/.zshrc ]; then # Check if completions are already configured if ! grep -q "terraform completion" ~/.zshrc; then cat << 'EOF' >> ~/.zshrc
# HashiCorp tool completionsautoload -U +X bashcompinit && bashcompinitcomplete -o nospace -C /usr/bin/terraform terraformcomplete -o nospace -C /usr/bin/packer packercomplete -o nospace -C /usr/local/bin/terragrunt terragrunt
# Add aliases for common Terraform commandsalias tf='terraform'alias tfi='terraform init'alias tfp='terraform plan'alias tfa='terraform apply'alias tfd='terraform destroy'alias tfo='terraform output'alias tfs='terraform state'alias tfv='terraform validate'alias tfmt='terraform fmt'
# Add aliases for common Packer commandsalias pk='packer'alias pkb='packer build'alias pkv='packer validate'alias pkf='packer fmt'
# Add aliases for Terragruntalias tg='terragrunt'alias tgi='terragrunt init'alias tgp='terragrunt plan'alias tga='terragrunt apply'alias tgd='terragrunt destroy'EOF echo "Added tool completions and aliases to ~/.zshrc" else echo "ZSH completions already configured" fielse echo "ZSH configuration file (~/.zshrc) not found"fi
# Install VSCode and extensions (optional)read -p "Install Visual Studio Code and Terraform extensions? (y/n): " install_vscodeif [[ $install_vscode == "y" || $install_vscode == "Y" ]]; then echo "Installing VSCode and extensions..." sudo apt install software-properties-common apt-transport-https wget -y wget -q https://packages.microsoft.com/keys/microsoft.asc -O- | sudo apt-key add - sudo add-apt-repository "deb [arch=amd64] https://packages.microsoft.com/repos/vscode stable main" sudo apt update sudo apt install code -y code --install-extension hashicorp.terraform code --install-extension mindginative.terraform-snippets code --install-extension 4ops.terraform code --install-extension ms-azuretools.vscode-azureterraform echo "VSCode and Terraform extensions installed"fi
# Create sample Terraform configurationread -p "Create a sample Terraform project structure? (y/n): " create_sampleif [[ $create_sample == "y" || $create_sample == "Y" ]]; then echo "Creating sample Terraform project structure..." mkdir -p ~/terraform-sample/{modules,environments/{dev,prod},scripts}
# Create main.tf cat << 'EOF' > ~/terraform-sample/environments/dev/main.tfterraform { required_version = ">= 1.0.0"
required_providers { aws = { source = "hashicorp/aws" version = "~> 4.0" } }
# Uncomment to enable backend configuration # backend "s3" { # bucket = "my-terraform-state" # key = "dev/terraform.tfstate" # region = "us-west-2" # dynamodb_table = "terraform-lock" # }}
provider "aws" { region = var.region}
module "example" { source = "../../modules/example"
environment = var.environment}EOF
# Create variables.tf cat << 'EOF' > ~/terraform-sample/environments/dev/variables.tfvariable "region" { description = "AWS region" type = string default = "us-west-2"}
variable "environment" { description = "Environment name" type = string default = "dev"}EOF
# Create outputs.tf cat << 'EOF' > ~/terraform-sample/environments/dev/outputs.tfoutput "example_output" { description = "Example output from the module" value = module.example.example_output}EOF
# Create example module mkdir -p ~/terraform-sample/modules/example
cat << 'EOF' > ~/terraform-sample/modules/example/main.tfresource "aws_s3_bucket" "example" { bucket = "example-${var.environment}-${random_string.suffix.result}"
tags = { Name = "Example Bucket" Environment = var.environment }}
resource "random_string" "suffix" { length = 8 special = false upper = false}EOF
cat << 'EOF' > ~/terraform-sample/modules/example/variables.tfvariable "environment" { description = "Environment name" type = string}EOF
cat << 'EOF' > ~/terraform-sample/modules/example/outputs.tfoutput "example_output" { description = "S3 bucket name" value = aws_s3_bucket.example.bucket}EOF
echo "Sample Terraform project created at ~/terraform-sample"fi
# Final instructionsecho "=== Installation Complete ==="echo "To activate the changes in your current shell, run:"echo " source ~/.zshrc"echo ""echo "Terraform and Packer are now installed and configured on your system."echo "Happy infrastructure-as-code development!"