Are you overwhelmed with the number of cloud services and resources you have to manage? Do you wonder what tool can help with these chores? Wonder no more and dive right in! This tutorial will teach how to install Terraform!
Terraform is the most popular automation tool to build, change and manage your cloud infrastructure effectively and quickly. So let’s get started!
Prerequisites
This how-to will be a step-by-step tutorial on Terraform. If you’d like to follow along, be sure you have the following:
- A Windows 10+ device.
- A device with Ubuntu 18.04 or later. The examples in this tutorial will use Ubuntu 18.04.
Installing Terraform on a Windows Device
To get started read below to learn how to install Terraform on a Windows device.
1. Open your favorite browser and download the appropriate version of Terraform from HashiCorp’s Terraform releases page. The examples use version 1.0.6.
At the moment of the writing, the latest Terraform version is 1.0.6. The examples will use this version.
2. Create a tools folder in your C:\ drive.
3. Extract the downloaded archive file and copy the terraform.exe into the previously created C:\tools folder.
4. Next, open your Start Menu and type in environment system, and the first search result that comes up should be Edit the System Environment Variables command. Click on the search result and will see the System Properties window as shown below.
5. Now find the Path environment variable and click Edit to change it.
6. Click New and add the folder path where you extracted terraform.exe, which is C:\tools\terraform.exe, and click OK.
The screenshot above shows a sub-directory path with the version included, this is optional.
7. Open a Command Prompt or PowerShell to check if Terraform is correctly added to the environmental variable PATH. Run the terraform
command and you will see an output similar to that shown below.
8. As a good practice, you should also consider running the terraform --version
to confirm the version of Terraform installed. You should see 1.0.6
if you are following along.
Installing Terraform with Package Repositories on Ubuntu
Now learn how to install Terraform on Linux, specifically on Ubuntu. You will install Terraform via package repositories for easier integration with configuration management tools with the apt install
command.
1. SSH into an Ubuntu machine with your favorite SSH client.
2. Create a directory to hold your Terraform installation files. Although you can install Terraform in any directory, it is recommended to install software under the opt directory. Create the terraform directory with the mkdir
command. Navigate into that directory with the cd
command, as shown below.
# Creating the directory terraform under opt and changing to this directory.
mkdir /opt/terraform
cd /opt/terraform
3. Next, configure your system to trust the HashiCorp key for package authentication by running the curl
command below. After you run the command you will see an OK
message. The connection between your Ubuntu device and the Hashicorp site is now secure.
Although this tutorial uses the root user, it is typically best practice to use a less-privileged account member of the sudoers group.
curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
4. After registering the key, add the official HashiCorp repository to your system by running the apt-add-repository
command. The Hashicorp repository contains the Terraform installation package.
sudo apt-add-repository "deb [arch=$(dpkg --print-architecture)] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
5. Install Terraform from the newly added Hashicorp repository using the apt install
command.
sudo apt install terraform
6. Check your Terraform installation by running the terraform
command. You should see an output similar to the one below.
Installing Terraform with Zip Files on Any Linux Device
Not every Linux distro has access to the apt tools. As an alternative, install Terraform from zip files. Let’s check how to accomplish that!
1. Create the /opt/terraform directory and navigate to it with the mkdir
and cd
commands, as shown below.
# Creating the directory terraform under opt and changing to this directory.
mkdir /opt/terraform
cd /opt/terraform
2. Download the appropriate version of Terraform from HashiCorp’s Terraform releases page. The example below downloads version 1.0.6 with wget
to the current directory.
wget https://releases.hashicorp.com/terraform/1.0.6/terraform_1.0.6_linux_amd64.zip
3. After a successful download, extract the Terraform archive to the current directory with the unzip
command.
unzip terraform_1.0.6_linux_amd64.zip
Run the ls
command to confirm that the terraform binary file is present.
4. Now, move the terraform binary to the executable directory /usr/local/bin. The executable directory (/usr/local/bin
) allows you to run the executable files from anywhere in the Linux system.
sudo mv terraform /usr/local/bin
Finally, verify if terraform has been installed successfully by running the terraform --version
command.
Conclusion
In this tutorial, you learned how to install Terraform on both Ubuntu and Windows devices. Now that you have Terraform downloaded and set up, what do you plan to manage with it?