How To Install Terraform on Ubuntu 22.04

Estimated reading: 4 minutes 127 views

This guide will help you install Terraform on Ubuntu 22.04|20.04 |18.04. Terraform is an Infrastructure as code tool that allows you to easily manage cloud resources in a versioned manner. You use Terraform to build, change, and version infrastructure deployed on popular service providers. This tool is not cloud-agnostic and it supports custom in-house solutions.

With Terraform you can manage Cloud Compute, Networking, Load Balancers, DNS, and so on using simple Declarative Programming Language. See the complete list of Terraform Providers. Complex changesets can be applied to your infrastructure with minimal human interaction

Install Terraform on Ubuntu 22.04

Terraform is distributed as a tarball on Github. Check the latest release on Terraform releases page before downloading below.There are two methods from which we can install terraform on Ubuntu22.04

Install Terraform from APT repository

Terraform team offers package repositories for Debian based Linux, which allow you to install Terraform using the apt package management tool or any other APT frontend.

First, install repository addition dependencies:

apt update
apt install  software-properties-common gnupg2 curl

Now import repository GPG key

curl https://apt.releases.hashicorp.com/gpg | gpg --dearmor > hashicorp.gpg
install -o root -g root -m 644 hashicorp.gpg /etc/apt/trusted.gpg.d/

With the key imported now add Hashicorp repository to your Ubuntu system:

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

Expected execution output:

Now install terraform on your Ubuntu Linux system:

apt install terraform

Check the version of terraform installed on your system

$ terraform --version


Verify that the tool works:

$ terraform
Usage: terraform [global options] <subcommand> [args]

The available commands for execution are listed below.
The primary workflow commands are given first, followed by
less common or more advanced commands.

Main commands:
  init          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

All other commands:
  console       Try Terraform expressions at an interactive command prompt
  fmt           Reformat your configuration in the standard style
  force-unlock  Release a stuck lock on the current workspace
  get           Install or upgrade remote Terraform modules
  graph         Generate a Graphviz graph of the steps in an operation
  import        Associate existing infrastructure with a Terraform resource
  login         Obtain and save credentials for a remote host
  logout        Remove locally-stored credentials for a remote host
  output        Show output values from your root module
  providers     Show the providers required for this configuration
  refresh       Update the state to match remote systems
  show          Show the current state or a saved plan
  state         Advanced state management
  taint         Mark a resource instance as not fully functional
  test          Experimental support for module integration testing
  untaint       Remove the 'tainted' state from a resource instance
  version       Show the current Terraform version
  workspace     Workspace management

Global options (use these before the subcommand, if any):
  -chdir=DIR    Switch to a different working directory before executing the
                given subcommand.
  -help         Show this help output, or the help for a specified subcommand.
  -version      An alias for the "version" subcommand.

Using Terraform to Manage Infrastructure

To test terraform installation, we will try to execute below terraform example. But before that let’s understand its important files.

  • main.tf : It is the main configuration file containing resource definition
  • variables.tf : It contains variables declarations.
  • outputs.tf : This contains output from the resources.
  • provider.tf : It contains provider definition.

Example : Using Local provider, create a file nix.txt.

mkdir projects
cd projects

Create Terraform main configuration file.

touch main.tf
$ nano main.tf
resource "local_file" "distros" {
  filename = "/tmp/nix.txt"
  content = "Greencloud provides VPS etc"
}

Save and close the file

Next initialize the terraform, plan and apply.

$ terraform init

$ terraform plan

$ terraform apply

Verify whether the file has been created or not.

root@Greencloud:~# ls -l /tmp/nix.txt
-rwxr-xr-x 1 root root 27 May 20 10:39 /tmp/nix.txt

Destroying Terraform Infrastructure

We have confirmed that our Terraform installation on Ubuntu is working as expected. destroy Terraform-managed infrastructure by running terraform destroy command.

$ terraform destroy

If you don’t want confirmation prompt, use:

terraform destroy -auto-approve

Sumarry

That’s all from this guide, I hope you have found it informative and useful. Kindly do post your feedback and queries in below comments section.