Terraform is an open-source Infrastructure as Code (IaC) tool developed by HashiCorp that allows network engineers to programmatically provision the physical resources that are required by an application to keep running.
We will show you how you can install Terraform on Ubuntu by multiple methods. But first, let’s clear some basics on Terraform.
What is Terraform?
Terraform is an Infrastructure as Code (IaC) tool. IaC allows network engineers or operations teams to automatically manage, monitor, and provision resource just by code instead of done manually by a technician.
By using Terraform, users can define their entire infrastructure just by using configuration files and version control in HashiCorp Configuration Language (HCL).
HCL is a simple language that is formed on the base of simple syntax which allows network engineers to provision and reprovision infrastructure across multiple cloud data centers.
Terraform is one of the most popular IaC tools, especially for cloud-based workloads
How Does Terraform Work?
When you apply a command to run and deploy a server, database, or even a load balancer, Terraform will parse the code and translate it to Application Programming Language (API) to call a respected resource provider.
Terraform has two main components:
Terraform core: It handles the planned execution of resources, resource graphs, and configuration files. It is created and compiled in binaries that are written in the Go programming language. In core, each compiled binary will act as Command Line Interface for interacting plugins through Remote Procedure Calls.
Terraform Plugins: They are responsible for specifying resources for requested services and also authenticating the providers to make API calls.
Installing Terraform in Ubuntu 22.04
We will guide you through 3 easy methods by which you can install Terraform in the Ubuntu system. You can choose any one of them as per your preference.
Method 1: Installing Terraform Using Snap
This is the easiest method to install Terraform because Snap comes preconfigured on Ubuntu so it will take the least effort to install Terraform in Ubuntu.
Step 1: Using Snap Command for Installation
Utilize the following command to install Terraform:
sudo snap install terraform –candidate
Step 2: Checking Version of Installation
The reason why we are checking the installed version is we can be assured that Terraform has been installed on our system. To check the version of Terraform, use the following command:
If it shows the version number, you have successfully installed Terraform on your system.
Method 2: Installing Terraform Using Apt
Apt is the official package manager on Debian-based distributions which also includes Ubuntu. Using Apt we can install software conveniently. So let’s start our installation process.
Step 1: Adding HashiCorp Repository
Adding repository avails up to download official packages. This is the crucial step that will allow us to install HashiCorp on our system. Use the following command:
sudo apt-add-repository “deb [arch=$(dpkg –print-architecture)] https://apt.releases.hashicorp.com $(lsb_release -cs) main”
Step 2: Adding HasiCorp GPG keys
Adding GPG keys to our system will allow us to authenticate the installed packages. Use the given command to add GPG keys to your system:
curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add –
Step 3: Updating Added Repositories
Once we are done with adding repository and GPG keys to our system, we are required to update our system to get the latest version of Terraform. This command will update all repositories on the system and will download updates if any. -y is used to allow download files.
sudo apt update && sudo apt upgrade -y
Step 4: Installing Terraform
After adding the repository, GPG keys, and updating the system, its time to install Terraform by the following command:
sudo apt install terraform
Step 5: Checking Terraform Version
The reason why we are performing this step is that we want to make sure that Terraform is installed properly on our system.
If it shows a similar output to this image, you have managed to install Terraform successfully and if it throws any errors, you can repeat the process.
Method 3: Installing Terraform Manually
The major benefits of installing Terraform are:
You can install the 32-bit version
You can choose a version manually
You can select your preferred directory to install Terraform
You can install multiple versions of Terraform
Step 1: Downloading Terraform from Browser
Go to the official download page of Terraform for downloading. Now, select the preferred version from there under Ubuntu/Debian section.
Step 2: Unzipping the Downloaded File
Before unzipping, you have to change your directory to Downloads. Use the following command to change your directory from Home to Downloads:
Now, we are ready to unzip files. Use the given command to unzip the downloaded file.
This will create a separate folder named terraform in our Downloads directory.
Step 3: Setting up “Terraform PATH” Variable
In this step, we just have to move to terraform folder from usr/bin/ directory by the given command.
sudo mv ~/Downloads/terraform /usr/bin/
Once we move the folder, we can check the path where terraform is located by below command
As you can see, it is located at /usr/bin/terraform
Step 4: Checking Current Release of Terraform
After going through all these steps, we have to confirm whether we are on right track or not, and by checking the version of Terraform, if we get output suggesting version, we are on right track and if not, you can cross-check missed step.
Step 5: Installing Terraform “auto-complete” Extension
We can use the extension named auto-complete which will make an entry in “.bashrc” in your home directory where Terraform is installed.
Now, if we use grep with “complete” in our .bashrc, you will see the following output at -C /usr/bin/terraform terraform.
grep “complete” /home/sagar/.bashrc
Note: change your name with sagar.
Step 6: Reading Executable File After Changes
In the previous step we have installed an auto-complete extension but to make it work, we have to use the “source” command as follows:
Uninstallation of Terraform
If you are not satisfied with Terraform or you don’t need it anymore installed on your system, you can use these 3 methods to remove Terraform from your system.
Method 1: Uninstalling Terraform from Ubuntu (For Snap)
This method is only for those who have installed Terraform from Snap using the 1st method.
Step 1: Using Sudo Snap Remove
You can easily remove the given command to remove Terraform:
sudo snap remove terraform
Step 2: Removing Terraform folder
You will still see the folder of Terraform at /home/snap.
To remove Terraform, first, we have to change our current directory to snap by given command:
Now, use the given command to remove Terraform folder:
sudo rm -r terraform
Method 2: Uninstalling Terraform from Ubuntu (For Apt Method)
If you have installed Terraform using the 2nd method, you can easily remove it just by the following command. We have used purge with auto-remove which will remove all the dependencies, configuration files, and data of Terraform.
sudo apt purge –auto-remove terraform
Method 3: Uninstalling Terraform from Ubuntu (Manual process)
If you went through 3rd method, you can use this method for removing Terraform from your system.
You are just required to remove the directory where you have extracted Terraform files. In our shown method, we have moved all extracted files to usr/bin/terraform. So we will remove that folder by the following command:
sudo rm -rf /usr/bin/terraform
Now, let’s try to run Terraform. It should show us an error.
FAQs on installing Terraform on Ubuntu
Here are some of the frequently asked questions while installing Terraform on any Ubuntu version.
How do you check if Terraform is installed?
Open your Terminal and use this command: terraform -v. It will show you the version number if it’s installed on your system and if throws any error, it is not installed.
How do I get Terraform on Ubuntu?
Ubuntu comes preconfigured with snap and it’s the easiest option to install Terraform in Ubuntu. Use this single command and it will download and install Terraform: sudo snap install terraform –candidate.
- How to Use the Sudo Command in Linux
- How to Compress PDF in Ubuntu
- How to Uninstall Python in Ubuntu
This was our simplified approach on how to install Terraform in Ubuntu and other Ubuntu-based Linux distributions. Hope you’ve got enough information about installing and uninstalling Teeraform on Ubuntu 22.04.