How to Install Terraform

This articles outlines the steps needed to install Terraform.

Terraform is an infrastructure automation tool developed by Hashicorp. It is the same organization that developed tools like Vagrant, Packer, Consul, Nomad and Vault. Terraform was released as open source tool on July 2014.

The main features of Terraform are below:

  1. It’s written in the Go programming language.
  2. The goal of project is to define Infrastructure As A Code (IaaC) using configuration files. The Terraform configuration files are defined in high level scripting language called as Hashicorp Configuration Language (also known as HCL).
  3. Since terraform is IaaC(Infrastructure As A Code), it allows building, changing, and managing infrastructure in a safe, repeatable and predictable way more effectively.
  4. It has planning step which shows what changes will be made by terraform. This helps avoiding any unnecessary infrastructure change.
  5. It builds resource graph. Resource graph is the used to identify the dependecies between infrastructure components and helps in parallelizing creation and modification on non dependent infrastructure components.
  6. It supports wide range of providers. The providers could be IaaS (e.g. Alibaba Cloud, AWS, GCP, Microsoft Azure, OpenStack), PaaS (e.g. Heroku), or SaaS services(e.g. Terraform Cloud, DNSimple, Cloudflare)
  7. It is client only tool which directly interacts with Providers specified in configuration files to manage infrastructure resources.
  8. It also provides capability to share and reuse the code in the form of Terraform Modules.

In this article, we will see how to:

Prerequisite:
A user account with sudo privileges.

Install Terraform on Ubuntu 20.04

a. Update system and packages:

$ sudo apt-get update

You would be asked to enter sudo password as shown below:
Ubuntu apt-get update
It would take some minutes to update all packages depending on internet speed. Once done, you would see something similar to below:
Ubuntu apt-get update
b. Install wget, unzip and jq packages on Ubuntu

$ sudo apt-get install wget unzip jq  

This will install wget, unzip and jq packages as shown below. We are going to use wget command to download terraform files, unzip package to unzip downloaded terraform file and jq for parsing json on bash
ubuntu wget unzip jq

c. Get latest Terraform version number

$ LATEST_VER=$(curl -s https://checkpoint-api.hashicorp.com/v1/check/terraform | jq -r -M '.current_version')
$ echo $LATEST_VER  

The above commands check the latest available Terraform version using Hashicorp api. This version will be used in subsequent commnds to download the actual terraform file.
ubuntu latest terraform version

d. Download latest Terraform package

$ wget https://releases.hashicorp.com/terraform/${LATEST_VER}/terraform_${LATEST_VER}_linux_amd64.zip  

This command downloads the latest version of terraform and you would see output similar to: ubuntu terraform download

e. Unzip the file.

$ sudo unzip ./terraform_${LATEST_VER}_linux_amd64.zip -d /usr/local/bin/

This command unzips the Terraform file and moves it to ubuntu binaries directroy. ubuntu unzip terraform to bin

f. Check the terraform version using below command:

$ terraform version  

This will provide output similar to: ubuntu terraform version

g. Check terraform commnads using below command

$ terraform --help
Usage: terraform [-version] [-help] <command> [args]

The available commands for execution are listed below.
The most common, useful commands are shown first, followed by
less common or more advanced commands. If you're just getting
started with Terraform, stick with the common commands. For the
other commands, please read the help and docs before usage.

Common commands:
    apply              Builds or changes infrastructure
    console            Interactive console for Terraform interpolations
    destroy            Destroy Terraform-managed infrastructure
    env                Workspace management
    fmt                Rewrites config files to canonical format
    get                Download and install modules for the configuration
    graph              Create a visual graph of Terraform resources
    import             Import existing infrastructure into Terraform
    init               Initialize a Terraform working directory
    login              Obtain and save credentials for a remote host
    logout             Remove locally-stored credentials for a remote host
    output             Read an output from a state file
    plan               Generate and show an execution plan
    providers          Prints a tree of the providers used in the configuration
    refresh            Update local state file against real resources
    show               Inspect Terraform state or plan
    taint              Manually mark a resource for recreation
    untaint            Manually unmark a resource as tainted
    validate           Validates the Terraform files
    version            Prints the Terraform version
    workspace          Workspace management

All other commands:
    0.12upgrade        Rewrites pre-0.12 module source code for v0.12
    debug              Debug output management (experimental)
    force-unlock       Manually unlock the terraform state
    push               Obsolete command for Terraform Enterprise legacy (v1)
    state              Advanced state management

terraform help

Install Terraform on Centos 7/Red Hat(RHEL) 8/Amazon Linux 2

a. Update system and packages:

$ sudo yum update

yum_update

b. Install wget, unzip and jq packages on Ubuntu

$ sudo yum install wget unzip jq  

yum install wget unzip jq

c. Get latest Terraform version number

$ LATEST_VER=$(curl -s https://checkpoint-api.hashicorp.com/v1/check/terraform | jq -r -M '.current_version')
$ echo $LATEST_VER

The above commands check the latest available Terraform version using Hashicorp api. This version will be used in subsequent commnds to download the actual terraform file.
ubuntu latest terraform version

d. Download latest Terraform package

$ wget https://releases.hashicorp.com/terraform/${LATEST_VER}/terraform_${LATEST_VER}_linux_amd64.zip

This command downloads the latest version of terraform and you would see output similar to: ubuntu terraform download

e. Unzip the file.

$ sudo unzip ./terraform_${LATEST_VER}_linux_amd64.zip -d /usr/local/bin/
Archive:  ./terraform_0.12.24_linux_amd64.zip
inflating: /usr/local/bin/terraform

f. Check the version

$ terraform version
Terraform v0.12.24

Install terraform on Windows

This section outlines how to install terraform on windows.
a. Goto https://www.terraform.io/downloads.html.
b. Search for Windows and download 32bit/64bit terraform package depending on your system.
c. Create “C:\Program Files (x86)\Terraform” directory.
d. Extract terraform zip in “C:\Program Files (x86)\Terraform”
e. In order to set up environment variable, goto Control Panel
f. Click System
g. Click on “Advanced System Settings”. Click on “Environment Variable” near the bottom of the window.
h. In the System variables pane, click Path and then click Edit.
i. Click on New. Add the path value as “C:\Program Files (x86)\Terraform”.
j. Click on Ok to save changes.
k. To verify installation, Open command prompt by pressing Windows+R, type “cmd”.
l. Execute “terraform -version” command. It should display the terraform version you downloaded.

updatedupdated2020-08-102020-08-10