How to Install Terraform on CentOS
In this article
Terraform is HashiCorp's infrastructure-as-code tool that lets you define and provision cloud and on-premises resources using declarative configuration files. This guide walks you through installing Terraform on CentOS (7, 8, or 9) using the official HashiCorp repository, which is the recommended method because it provides automatic updates and verified binaries. You'll need a CentOS system with sudo access and an internet connection. No prior Terraform experience is required.
What You'll Need
- A CentOS machine (7, 8, or 9) with internet access
- A user account with sudo privileges
- Basic familiarity with the command line
Step-by-Step
- Update your system (recommended). Open a terminal and run:
On CentOS 8+ you can usesudo yum update -ydnfinstead; both work in this guide. - Install required dependencies. Terraform needs
wgetandunzipto download and extract the binary if you choose the manual method. For the repository method, you'll needyum-utilsordnf-plugins-coreto manage the repo. Install them with:sudo yum install -y yum-utils wget unzip - Add the HashiCorp repository. The official repo is the cleanest way to install Terraform. Run:
Ifsudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repoyum-config-manageris not found, install it withsudo yum install -y yum-utilsfirst. - Verify the repository is enabled. Check with:
You should see asudo yum repolisthashicorpentry listed. - Install Terraform. Now install the package:
This installs the latest stable version.sudo yum install -y terraform - Verify the installation. Check the version to confirm everything works:
You should see output liketerraform versionTerraform v1.x.x. - Enable tab completion (optional but handy). Add the completion script to your shell profile:
Then restart your shell or runterraform -install-autocompletesource ~/.bashrc.
If you prefer to install manually (e.g., to pin a specific version), download the binary from terraform.io/downloads, unzip it, and move it to /usr/local/bin. For example:
wget https://releases.hashicorp.com/terraform/1.5.7/terraform_1.5.7_linux_amd64.zip
unzip terraform_1.5.7_linux_amd64.zip
sudo mv terraform /usr/local/bin/
Common Mistakes
- Using the wrong repo URL. The official repo is for RHEL, but it works on CentOS. Do not use the Debian/Ubuntu repo on CentOS.
- Forgetting to enable the repo. Some versions of CentOS may not automatically enable third-party repos. Run
sudo yum-config-manager --enable hashicorpif needed. - Installing from EPEL. EPEL may offer an older Terraform version. Stick with the HashiCorp repo for the latest.
- Not checking PATH. If you manually install to a custom directory, ensure it's in your PATH or you'll get "command not found".
Tips
- Use
sudo yum update terraformto upgrade Terraform later; the repo handles updates. - Set up a separate directory for each Terraform project to keep state files and configurations organized.
- Use
terraform initin a new project to download provider plugins. - Consider using a version manager like
tfenvif you work on multiple projects with different Terraform versions.
Troubleshooting
- "yum-config-manager: command not found": Install yum-utils:
sudo yum install -y yum-utils. - "No package terraform available": The repo might not be enabled. Run
sudo yum-config-manager --enable hashicorpand try again. - GPG key errors: The repo uses a GPG key. If you get a key import prompt, accept it. If it fails, re-import the key manually:
sudo rpm --import https://rpm.releases.hashicorp.com/gpg - "terraform: command not found" after manual install: Check that the binary is in your PATH. Run
echo $PATHand move the binary to a directory listed, like/usr/local/bin. - Permission denied when running terraform: Ensure the binary has execute permissions:
sudo chmod +x /usr/local/bin/terraform.
Frequently Asked Questions
Can I install Terraform on CentOS 6?
CentOS 6 is end-of-life and not officially supported by HashiCorp. It's strongly recommended to upgrade to a supported version.
Is the HashiCorp repo safe to use?
Yes, it's the official repository maintained by HashiCorp. Packages are signed with their GPG key.
How do I uninstall Terraform?
Run sudo yum remove terraform. This removes the binary but leaves your configuration files and state files untouched.
Can I install multiple versions of Terraform?
Yes, you can use a tool like tfenv or manually download binaries to different directories and switch via symlinks. The repo method installs only one version.
Do I need to install anything else to use Terraform with AWS/Azure/GCP?
No, Terraform downloads the necessary provider plugins when you run terraform init. You just need credentials for your cloud provider.
Why is my installed version older than the latest?
The repo may lag slightly. You can always download the latest binary manually from the official site.