Terraform by HashiCorp is the industry-standard tool for provisioning and managing infrastructure as code (IaC) across cloud providers, on-premises platforms, and SaaS APIs. On RHEL 8 you can install Terraform directly from the official HashiCorp RPM repository, keeping it up to date through the standard dnf package manager. This tutorial walks through adding the HashiCorp repo, installing Terraform, writing your first configuration file, and running the core Terraform workflow commands so your team can begin managing infrastructure declaratively.
Prerequisites
- RHEL 8 server with a non-root sudo user
dnf-plugins-coreinstalled (providesdnf config-manager)- Internet access to reach
rpm.releases.hashicorp.com - A cloud provider account (AWS, Azure, or GCP) if you want to provision real resources
- Basic familiarity with the command line and text editors
Step 1 — Add the HashiCorp Repository
HashiCorp publishes signed RPM packages in their own repository. Adding it lets dnf manage Terraform upgrades automatically.
# Install the dnf plugins package if not already present
sudo dnf install -y dnf-plugins-core
# Add the HashiCorp repository
sudo dnf config-manager
--add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
# Verify the repository was added
dnf repolist | grep hashicorp
Step 2 — Install Terraform
With the repository configured, a single dnf command installs the latest stable Terraform release.
sudo dnf install -y terraform
# Confirm the installation
terraform version
# Expected output (version will vary):
# Terraform v1.8.x
# on linux_amd64
To install a specific version, use sudo dnf install -y terraform-1.7.5. You can list all available versions with dnf --showduplicates list terraform.
Step 3 — Write Your First Configuration File
Every Terraform project lives in a directory. The entry point is typically main.tf, which declares a provider and at least one resource. The example below uses the local provider so no cloud credentials are needed.
mkdir ~/terraform-demo && cd ~/terraform-demo
cat > main.tf < 2.5"
}
}
required_version = ">= 1.5.0"
}
provider "local" {}
resource "local_file" "hello" {
filename = "${path.module}/hello.txt"
content = "Hello from Terraform on RHEL 8!n"
}
output "file_path" {
value = local_file.hello.filename
}
EOF
Step 4 — Initialise, Plan, and Apply
The Terraform workflow follows three main commands: init downloads provider plugins, plan previews changes, and apply executes them.
# Download provider plugins into .terraform/
terraform init
# Preview what will be created/changed/destroyed
terraform plan
# Apply the configuration (type 'yes' when prompted)
terraform apply
# Skip the interactive prompt
terraform apply -auto-approve
# Verify the output
cat hello.txt
terraform output
Step 5 — Understand the State File and Destroy Resources
Terraform records the current state of managed resources in terraform.tfstate. This file must be preserved (or stored remotely) between runs. Use terraform destroy to tear down everything managed by the current configuration.
# Inspect the state file (readable JSON)
cat terraform.tfstate
# List resources tracked in state
terraform state list
# Show details of a specific resource
terraform state show local_file.hello
# Remove all resources created by this configuration
terraform destroy -auto-approve
Step 6 — Add a .gitignore for Terraform Projects
Terraform creates local files that should never be committed to version control, especially the state file which can contain sensitive data.
cat > .gitignore <<'EOF'
# Terraform state files — may contain secrets
*.tfstate
*.tfstate.*
*.tfstate.backup
# Local .terraform directory (provider binaries)
.terraform/
.terraform.lock.hcl
# Plan output files
*.tfplan
# Variable files that may contain secrets
*.tfvars
!example.tfvars
# Crash log files
crash.log
EOF
git init
git add main.tf .gitignore
git commit -m "Initial Terraform configuration"
Conclusion
You have added the HashiCorp RPM repository to RHEL 8, installed Terraform via dnf, written a provider and resource configuration, executed the full init → plan → apply → destroy lifecycle, and configured a .gitignore to protect sensitive state files. You now have a solid foundation for writing real-world infrastructure-as-code with Terraform on RHEL 8.
Next steps: Use Terraform to Provision Infrastructure on RHEL 8, Manage Terraform State with an S3 Backend, and Integrate Terraform with a Jenkins CI/CD Pipeline on RHEL 8.