How to Install Terraform on Windows and Linux and Set the
Environment Variable
Terraform is a widely used Infrastructure as Code (IaC) tool
that simplifies the process of managing and provisioning infrastructure.
Whether you are using Windows or Linux, installing Terraform and setting up the
environment variable is straightforward. This guide will walk you through how
to install Terraform on Windows and Linux and set the environment variable
step by step. Let’s dive in!
What is Terraform?
Before we start with the installation, it’s essential to
understand Terraform's role. Terraform allows you to define infrastructure in
configuration files, making it easy to automate deployments. Its cross-platform
nature makes it a favourite among DevOps engineers and IT professionals.
For more details : What is Terraform? A Comprehensive Guide to Pros and Cons in Infrastructure Automation
How to Install Terraform on Windows
Step 1: Download Terraform
- Visit
the official Terraform
downloads page.
- Download
the Windows version of Terraform for your architecture (32-bit or
64-bit).
Step 2: Extract the Terraform Binary
- Once
downloaded, extract the ZIP file.
- Place
the terraform.exe file in a directory of your choice (e.g., C:\Terraform).
Step 3: Set the Environment Variable on Windows
Setting the environment variable ensures you can run the terraform
command from any terminal without specifying the full path.
- Open
the System Settings:
- Right-click
on This PC or My Computer and select Properties.
- Click
on Advanced system settings in the left sidebar.
- Access
Environment Variables:
- In the System Properties window, click on the Environment Variables button.
- Edit
the Path Variable:
- Under
System Variables, scroll to find the Path variable and select it.
- Click Edit.
- Add
Terraform’s Path:
- Click
New and enter the path where terraform.exe is located (e.g., C:\Terraform).
- Click OK to save changes.
- Apply
Changes:
- Close
all dialog boxes by clicking OK.
Step 4: Verify Installation
- Open
a Command Prompt or PowerShell window.
- Run
the following command:
- terraform -version
- If
installed correctly, it will display the Terraform version.
How to Install Terraform on Linux
Step 1: Download Terraform
- Visit
the official Terraform
downloads page.
- Download
the Linux version of Terraform for your architecture.
Step 2: Extract the Terraform Binary
- Open
a terminal.
- Navigate
to the directory where the file was downloaded (e.g., ~/Downloads).
- Extract
the binary using the following command:
- unzip
terraform_<version>_linux_amd64.zip
- Move
the extracted binary to /usr/local/bin:
- sudo mv terraform
/usr/local/bin/
Step 3: Set the Environment Variable on Linux
To ensure Terraform is accessible globally, you need to set
the environment variable.
- Update
the PATH Variable:
- Open
your shell configuration file (e.g., .bashrc, .bash_profile, or .zshrc)
using a text editor:
- nano ~/.bashrc
- Add
the following line to include Terraform’s path:
- export PATH=$PATH:/usr/local/bin
- Apply
Changes:
- Save
and close the file.
- Apply
the changes using the command:
- source ~/.bashrc
Step 4: Verify Installation
- Type
the following command in your terminal:
- terraform -version
- If
installed correctly, it will display the Terraform version.
Detailed Explanation of Environment Variables
Environment variables are critical for making applications
and tools like Terraform accessible globally. Here’s a detailed breakdown:
On Windows:
- Why
Set the PATH Variable? The PATH environment variable specifies
directories where executable programs are located. By adding Terraform’s
directory to the PATH, you ensure that Windows can locate the terraform.exe
file without specifying its full path.
- Common
Issues:
- Command
Not Found: This happens if the PATH variable was not set correctly.
Double-check the directory path.
- Permissions
Issue: Ensure you have administrative rights to modify the PATH
variable.
On Linux:
- Why
Set the PATH Variable? On Linux, the PATH variable works similarly.
Adding /usr/local/bin ensures that the terminal can locate the Terraform
binary globally.
- Common
Issues:
- Changes
Not Persisting: Ensure you edit the correct shell configuration file
for your terminal.
- Incorrect
Binary Placement: Always move the binary to a directory included in
the PATH (e.g., /usr/local/bin).
Conclusion
In this guide, we covered how to install Terraform on
Windows and Linux and set the environment variable. By following these
steps, you can quickly get Terraform up and running, ready to manage your
infrastructure efficiently. Setting the environment variable is a crucial step
to ensure seamless usage of Terraform across your system. With Terraform
properly installed and configured, you are now equipped to leverage its
powerful IaC capabilities for your projects.
0 Comments