How to Install Terraform on Ubuntu in 7 Steps [With Examples]

April 9th, 2024
How to Install Terraform on Ubuntu in 7 Steps [With Examples]

With the growing popularity of cloud-based infrastructure, the need to create and maintain complex platforms has also increased. With the DevOps movement, having an Infrastructure as Code (IaC) tool fits seamlessly into the DevOps workflow. Enter Terraform, one of the most popular infrastructures as Code tools. In this tutorial, I will show you how to install Terraform on Ubuntu so you can start using this tool to your advantage.

What is Terraform?

Terraform is a widely used Infrastructure as Code tool developed by HashiCorp. With Terraform, we can define cloud and on-premises resources in human-readable configuration files. It works with all the major cloud providers like AWS, with specialized cloud services for developers like Cherry Servers, and Docker infrastructure.

Deploy, manage, and orchestrate your cloud infrastructure across one or more cloud providers with Cherry Servers Terraform module

What is Terraform used for?

Terraform is used for creating and managing resources on cloud platforms and other services. This is done via providers created by HashiCorp and the Terraform community. Those providers allow us to manage low-level cloud components such as storage and networking resources and high-level components such as DNS.


To follow along this tutorial, you will need:

How to install Terraform on Ubuntu: Step-by-step process

The below steps will cover how to install Terraform on Ubuntu. We will also show how to declare a basic infrastructure by deploying Nginx on Docker using Terraform. We will then proceed to deploy the infrastructure and destroy it.

Step 1: Configure packages

We will install the pre-build versions of Terraform using apt package manager.

We will first start by downloading and saving the hashicorp PGP keys:

wget -O- | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg

We can now add an entry to the system's list of package providers:

echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list

Step 2: Install Terraform

We can now update our package list and install Terraform:

sudo apt update && sudo apt install terraform

Run terraform -version to test if Terraform has been correctly installed. This will show you the Terraform version installed:

terraform -version

Step 3: Declare infrastructure

Now that Terraform is installed, we can proceed to declare our first infrastructure.

Let’s first create a folder terraform in our home directory and move to that folder:

cd ~
mkdir terraform
cd terraform

Now, with your favorite text editor Vim, create the file We will do it with Vim:


Copy and paste the below information in the file.

terraform {
  required_providers {
    docker = {
      source = "kreuzwerker/docker"
      version = "~> 3.0.1"  

provider "docker" {}

resource "docker_image" "nginx" {
  name         = "nginx:latest"
  keep_locally = false

resource "docker_container" "nginx" {
  image = docker_image.nginx.image_id
  name  = "Cherry_servers_tutorial"
  ports {
    internal = 80
    external = 8000

This will tell Terraform to use the Docker provider and create a resource nginx from the latest nginx docker image. Nginx will then be deployed and accessible on port 80 as indicated with the internal parameter in the ports section.

Step 4: Validate the configuration

Terraform configurations can quickly get complex and validating them helps maintain a stable infrastructure.

We will now validate our configuration with the following command:

terraform validate

The command should indicate a valid configuration:

terraform validate

Step 5: Initialize the directory

We now need to initialize the configuration directory. The initialization will download and install the providers defined in the configuration.

We initialize the Terraform configuration directory with the following command:

terraform init

We should now see a similar output to this:

terraform init

Step 6: Build infrastructure

We now apply our configuration to build our infrastructure:

terraform apply

Terraform will ask for a confirmation to perform the actions declared in the configuration file.

Action confirmation

Type yes to continue.

Upon confirmation, Terraform will download the Nginx docker image and build the infrastructure:

docker container created

We can see the container running with the following command:

 docker ps |grep Cherry

 docker ps |grep Cherry

Step 7: Destroy infrastructure

We can also destroy the infrastructure. This will delete both the image and the Docker container that has been deployed during the build.

To destroy the infrastructure, use the following command:

terraform destroy

Type yes to continue.

terraform destroy


This tutorial covered how to install Terraform on Ubuntu, what Terraform is, and how it solves the provisioning of infrastructure management problems in the DevOps world. We also covered the basics of Terraform, such as creating and destroying an infrastructure. You can find more about Terraform in the official Terraform documentation.

With over 20 years in IT, Didier has been creating technical documentation for companies, catering to both technical and non-technical audiences. Didier is an expert in Linux system administration, DevOps, cloud computing, cybersecurity, IT consulting, management consulting, technical writing, Diataxis framework, Doc-as-Code, UX Writing, Jamstack, MkDocs, Docker, containers, open-source, SDLC, and Python programming. His hands-on technical expertise, coupled with his strong communication skills, enables him to bridge the gap between developers and end-users. Didier creates user guides, API References, end-user documentation, how-tos, and tutorials. He is an expert in authoring using modern technologies such as Markdown, Mermaid, and static-site generators. Didier also utilizes frameworks and methodologies such as Diaxiatis and Doc-as-code, applying structured writing techniques. Currently, Didier works as a freelance technical writer and documentation consultant, assisting organizations in creating comprehensive and easy-to-understand documentation for their software and web applications. In his previous roles, Didier worked as a system and applications engineer and implemented style guides, tone and voice best practices, and documentation processes that streamline their release cycles. Didier resides in Vacoas, Mauritius.

Cloud VPS - Cheaper Each Month

Start with $9.99 and pay $0.5 less until your price reaches $6 / month.

We use cookies to ensure seamless user experience for our website. Required cookies - technical, functional and analytical - are set automatically. Please accept the use of targeted cookies to ensure the best marketing experience for your user journey. You may revoke your consent at any time through our Cookie Policy.
build: 372cf894.659