How to install Terraform on Linux such as Ubuntu 20.04 LTS server

What is Terraform code software tool? 

Terraform is an open-source uniform configuration language that allows administrators to describe IT resources implementation in a “standardized” manner. It provides a command-line interface workflow to manage various types of cloud services such as Aws, Google Cloud, Vmware, Microsoft Azure, OpenStack, Oracle Cloud, Digital Ocean, and more…

Such type of resource declaration using text files called “Infrastructure as Code” in short “IaC”.  In this system, instead of creating a new instance on the cloud or allocating resources using the web interface of the respective service provide we simply map the desired system architecture in structured text files using Terraform. Which then automatically carries out the changes you have described for you via the provider’s API. For example, you want to install an Nginx server using the Docker service on multiple servers, for that we can create a Terraform file with a code declaring docker and source for installing Nginx along with what ports you want to open and other things if required. Then later we can apply that file on any server using terraform to quickly deploy Nginx in an automated way.

These declarative configuration files in Terraform uses HashiCorp Configuration Language (HCL) that allows for concise descriptions of resources using blocks, arguments, and expressions. Whereas, the technical details, which can differ significantly from provider to provider, have already been “stored” centrally in Terraform.

This abstraction layer allows a uniform process to be transferred to multiple local and cloud-based environments. Just a few lines in a Terraform configuration file are enough to build a complete cloud infrastructure.

 

Advantages:

  • Clean code
  • Open-source
  • Supported by practically all major cloud providers.
  • Automatic JSON code formatting
  • Innovative toolset
  • Enables cross-provider multi-cloud solutions with just one code.
  • Agility and efficiency
  • Download and install community or partner modules
  • Central overview of resources and infrastructure.
  • Quick response to changing company requirements.
  • Integrated separate planning, testing, and execution of your code.
  • Dependency graphing
  • Shorter release cycles
  • Comprehensive documentation
  • And more…

Commands to Install Terraform on Linux Servers

Major Linux server platforms which are commonly available on Cloud servers are RHEL/CentOS, Debian/Ubuntu &  if you are using AWS then Amazon Linux is also an option to use. For these server Linux platforms, the users can install Terraform directly by integrating its official repository whereas if you want to use it on some other Linux, then its compressed zipped file is available to download from the official website.

Using Pre-compiled binary

If you don’t want to add Terraform repo to install it or using Solaris, FreeBSD, OpenBSD, then we can directly download its binary from the official website and then extract the compressed file to get it. Follow the below steps…

  1. Download Terraform Binary, here is the link. Get the latest file as per your operating system.
  2. Extract the file-  unzip terraform_*_linux_amd64.zip
  3. Print your system path- echo $PATH
  4. The output will including /usr/local/bin, thus move the extracted file here…
  5. Move the extracted file to the path- sudo mv terraform /usr/local/bin/
  6. Check it – terraform -help

 

Install on Debian or Ubuntu Server using APT

1. Add HashiCorp GPG key

sudo apt install curl
curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -

 

2. Add Repository for Terraform

To get the latest packages to install Terraform using APT package manager add the following repo.

sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"

 

3. Run system update command

sudo apt update

 

4. Install Terraform

sudo apt-get install terraform

##############################################################

Install on RHEL/CentOS/AlmaLinux using dnf or yum

1. Install Yum-config-Manager

sudo yum install -y yum-utils

 

2. Add hashicorp repo

sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo

For Amazon Linux use this one to add the repo

sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/AmazonLinux/hashicorp.repo

 

3. Command to get Terraform

sudo yum -y install terraform

 

Fedora

sudo dnf install -y dnf-plugins-core
sudo dnf config-manager --add-repo https://rpm.releases.hashicorp.com/fedora/hashicorp.repo
sudo dnf -y install terraform

 

Add tab completion support

To get autocomplete syntax support while writing codes using Terraform, we can enable it using the below command

terraform -install-autocomplete

Other main commands are:

init            - To Prepare your working directory for other commands
validate        - Check whether the configuration is valid
plan            - Show changes required by the current configuration
apply           - Create or update infrastructure
destroy         - Destroy previously-created infrastructure

 

These were the commands to quickly install Terraform, if you want to learn about its command then see the official documentation. 

 

 

Comments (2)

  1. ITO May 3, 2021
    • Heyan May 3, 2021

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.