Open In App

How To Install Terraform In Ubuntu

Last Updated : 06 May, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Terraform is an open-source infrastructure as code (IaC) tool that enables users to define and provision infrastructure using a declarative configuration language. It simplifies the process of managing and scaling infrastructure by allowing you to describe your infrastructure necessities in a configuration file. In this article, you will get to know about Terraform, and why we need Ubuntu to install Terraform, we will also discuss major issues that you will face during installation and methods to resolve them.

What is Terraform?

Terraform is an open-source infrastructure as Code ( IaC ) tool that is developed by HashiCorp. It facilitates the users to define and provision the infrastructure using declarative configuration files. Its components such as servers, databases, and networks managed as code allowing version control and automation of the entire infrastructure lifecycle.

Why Use Ubuntu to install Terraform?

Terraform can be set up on various operating systems, like Windows, macOS, and different Linux distributions. Choosing Ubuntu to install Terraform is not a basic requirement, However, it has certain key features which differentiate it from other Linux distributions. The benefits of Ubuntu are illustrated below:

  • Package Management: Ubuntu uses the Debian package management system, which simplifies the installation process and makes it easier to install, update, and manage software packages with the use of the APT (Advanced Package Tool) package manager. This simplifies the technique of installing dependencies and managing software installations.
  • Community Support: Ubuntu provides us a large network of users and developers. This means that in case if you get stuck on any problems or have questions even on installing or using Terraform in Ubuntu, you’re likely to find a lot of resources, forums, and community discussions that were already created by pro developers to assist you and troubleshoot the problem and find solutions.
  • Ease of Use: Ubuntu is used for its user-friendly interface and ease of use. It is generally recommended for beginners who are new to the industry or with a Linux environment. The clear command structure and clean documentation are available for users who have different levels of experience.
  • Compatibility: Many tutorials, guides, and documentation those are online are frequently written with Ubuntu because of its popularity among developers. Following these sources may be more helpful while using Ubuntu.
  • Cloud Compatibility: Many cloud providers, including AWS, Azure, and Google Cloud, provide reliable Ubuntu images. If you are using cloud systems, initiating with an Ubuntu-based image can offer a regular environment between your local development and the cloud.

How to Manually Install Terraform in Ubuntu? A Step-By-Step Guide

The following are the steps to install the terraform in ubuntu:

Step 1: Search for the latest version

  • Go to any browser and search for the latest version of Terraform.

search-for-latest-version

  • Choose the official website HashiCorp AI to install Terraform.

Choose-the-official-website-Hash

  • Go into the Linux section then select Ubuntu/Debian and copy all three commands.

select-ubuntu

Step 2: Go into Command Prompt

  • Firstly we will create a new directory and then use this using following command:
mkdir terraform

create-a-new-directory

  • Navigate to the created terraform directory with the following command:
cd terraform/

use-directory

Step 3: Install Terraform

  • You can install terraform in ubuntu by pasting all three commands one by one which you have copied earlier in step-1.
wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg

Download the software with wget

  • Try on running the following command, the output looks as shown in the following screenshot:
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list

Printing the release information

  • Update and install the terraform with running the following command:
sudo apt update && sudo apt install terraform

Install the terraform

Step 4: Verifying the Terraform Installation

You can check the installed version by using this command

terraform --version

check-terraform-version

  • Congratulation you have successfully installed terraform in ubuntu

How to Configure Terraform In Ubuntu? A Step-By-Step Guide

The following are the steps for configuring the terraform in ubuntu:

Step 1: Download Terraform

  • Download the terraform binary suitable for the system from the site https://www.terraform.io/downloads.html

Step 2: Extract the Binary

  • Extract the downloaded terraform binary to the directory such as /usr/local/bin to make it executable for the system-wide.

Step 3: Set Permissions

  • Use the following command to set the permissions for making the terraform binary executable:
 chmod +x /usr/local/bin/terraform

Step 4: Verify Terraform Installation

Now, verify the installation of terraform using the following command:

terraform version

Step 5: Setup Environment Variables ( Optional )

  • Try on adding the terraform binary directory to your system’s PATH environment variable to run Terraform commands without specifying its absolute path to the directory:
echo 'export PATH=$PATH:/usr/local/bin' >> ~/.bashrc
source ~/.bashrc

How to Create Terraform Configuration Files? A Step-By-Step Guide

The following are the steps for configuring Terraform files:

Step 1: Create A Directory

  • Create a directory for organizing the terraform files with the following command:
mkdir terraform-project
cd terraform-project

Step 2: Initailize Terraform

  • Initialize the terraform in your project directory with the following command:
terraform init

Step 3: Create A Configuration File

Create a new Terraform configuration file with .tf extension, such as main.tf with following command:

touch main.tf

Step 4: Write Configuration

  • Write the terrform configuration in the main.tf using the Hashicorp configuration language ( HCP ) syntax. It define the resources like AWS EC2 instances, Google Cloud Storage.

Step 5: Validate the Confguration

  • Once, validate the syntax of the terraform configuration file with the following command:
terraform validate

Step 6: Plan Infrastructure

  • To see the preview of the changes use the following command:
terraform plan

Step 7: Apply Changes

  • Apply the changes that defined in your Terrafrom configuration to create, update or delete the resources.
terraform apply

Step 8: Review Changes

After apply the changes with the above step, review the changes once, provide the configuration with typing yes as prompted.

Step 9: Verify Resources

  • Once applied it, verify that terrafom has created or updated, use the following command to know:
terraform show

How To Update Terraform Version In Ubuntu? A Step-By-Step Guide

The following are the steps for updating terraform version in ubuntu:

Step 1: Download the New Version

  • Download the latest version of terraform the following link: https://www.terraform.io/downloads.html

Step 2: Extract the Binary

  • Extract the downloaded terraform binary to a directory where other executable binaries such as /usr/local/bin
sudo mv terraform /usr/local/bin/

Step 3: Move the Binary to Bin Directory

  • Now, move the terraform binary file to the directory such as /usr/local/bin
sudo mv terraform /usr/local/bin

Step 4: Update Permissions

  • Provide the executable permissions to the Terraform with the following command:
sudo chmod +x /usr/local/bin/terraform

Step 5: Verify the Update

  • Confirm the terraform version update with the following command:
terraform version

Troubleshooting Common Issues of Terraform

1. Insufficient Permissions

  • Issue: Users may face permission problems while attempting to move the Terraform binary to ( /usr/local/bin/).
  • Solution: Ensure you have got the essential permissions to carry out the move operation. Use sudo before the mv command:
sudo mv terraform /usr/local/bin/

2. Dependency Issues

  • Issue: Terraform might also have dependencies that are not installed on the device.
  • Solution: Check the Terraform documentation for particular dependencies and install them using the package manager. For example, on Ubuntu, you can install the desired dependencies with:
sudo apt-get install -y unzip

3. Incorrect PATH Configuration

  • Issue: Users may additionally face problems if the usr/local/bin/listing is not in their system PATH.
  • Solution: Ensure that the you have usr/local/bin/ listing is in the system PATH. You can add it by modifying the ~/.bashrc or ~/.bash_profile file:
echo 'export PATH=$PATH:/usr/local/bin/' >> ~/.bashrc
source ~/.bashrc

4. Outdated Terraform Version

  • Issue: Users may face compatibility issues or miss out on new functions if using an outdated Terraform version.
  • Solution: Regularly check for updates using the terraform model check-upgrade command. Upgrade to the latest version as per the requirement.
terraform version -check-upgrade

Conclusion

You can successfully install terraform in the ubuntu by following above given Steps. This tutorial will not help in installation but also will provide you information about brief knowledge of terraform and issues which you might can face during installation.

Installation of Terraform In Ubuntu – FAQs

What is Terraform?

Terraform is an open-source Infrastructure as Code (IaC) tool designed to outline, provision, and control infrastructure throughout numerous cloud servers and on-premise environments.

Can I Use Terraform With Multiple Cloud Providers?

Yes, Terraform is cloud-agnostic and it helps more than one cloud provider. You can manage resources on AWS, Azure, Google Cloud, etc. within the same Terraform configuration.

Can I Use Terraform to ,anage Already Existing Infrastructure?

Yes, Terraform can be used to manage each current and new infrastructure. You can import present resources into Terraform and manage them using the Terraform configuration.

What Is The Difference Between `terraform init`, `terraform plan`, and `terraform apply`?

Terraform init: Initializes a new or current Terraform configuration.

Terraform plan: Generates an execution plan, showing what actions Terraform will take.

Terraform apply: Applies the adjustments described within the Terraform configuration to the actual infrastructure.

What is HCP Terraform?

HCP Terraform is service within the Hasicorp Cloud Platform (HCP) provides the infrastructure as Code using Terraform.

What is Configuration Language?

Configuration is a language refers to the syntax that is used in terraform files for defining and managing the infrastructure resources.

How do I know if Terraform is installed?

On using the following command we can verify the installation of terraform:

terraform --version

How do I Install Terraform in Ubuntu Terminal?

For this firstly download the terraform software and then the following command to install the terraform in ubuntu:

sudo apt-get install terraform

How do I run a Terraform script in Ubuntu?

For running a terraform script, navigate to the directory where the configuration files are defined:

terraform init
terrform apply


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads