In this post, we will learn "How to install terraform on Ubuntu". Like always, I have also included a post on how to uninstall terraform. I will show you two methods of installation and uninstallation of the terraform tool. Both are simple and easy.
Let me give you a brief introduction of Terraform, before we deep dive into installation -
Table of Contents
There are many tools to implement infrastructure as code like Ansible, CloudFormation, Azure Resource Manager, Chef, Google cloud deployment manager and more. Terraform is one of the powerful tools (by HashiCorp) that belong to this list. It helps to build, change and manage your infrastructure easily with versioning manifest. Every cloud vendor has its own IaC tool, but Terraform is cloud-agnostic. So you can use a single tool to manage your multi-cloud environment. Infrastructure as Code (IaC) is a term used when you codify your whole infrastructure for automation with DevOps.
How to install Terraform on Ubuntu - 2 methods
There are two easy ways to install terraform on Ubuntu 20.04. Let's explore both options one by one. You can select any one of them as per your preference
Method1- Install Terraform 0.14 using apt
If you have ever worked on any Debian-based distro like Ubuntu. You would know, packages are installed using APT. So I will show you the conventional method of "Terraform installation" first.
Step1- Register HashiCorp GPG keys
To check the authenticity of packages, you must add HashiCorp signed gpg keys to your system. You will see the "OK" message if it is added successfully. Copy this command except for the $ sign and paste it to your Ubuntu system.
$ curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
*** If you are getting "curl: command not found error". Please install it first using below-mentioned command.
$ sudo apt install curl
Step2- Add the HashiCorp package repository
After adding the gpg key successfully. You need to add the HashiCorp repository to download and install terraform packages using apt.
$ sudo apt-add-repository "deb [arch=$(dpkg --print-architecture)] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
Step3- Update "Ubuntu" packages list
Always update the packages list, once you install and add any repository in Ubuntu.
$ sudo apt update
Step4- Install Terraform on Ubuntu
It will take around 82.7MB of space on your disk to install Terraform on Ubuntu 20.04.
$ sudo apt install terraform
Step5 - Check Terraform version
Run the "terraform -v" command to get the version. For example, Terraform v0.14.7 has been installed in our case.
Also, you can run the "which" command to check the "PATH" of terraform command. It is "/usr/bin/terraform" as shown in image.
# Check version of Terraform $ terraform -v # Check PATH of Terraform $ which
Method2 - Install Terraform manually
In this method, you can download Terraform zip file and extract it on your system. Below mentioned are few pros and cons of this method -
- The HashiCorp "apt" servers host only 64-bit (amd64) architecture packages. You can use the manual zip method to install terraform on 32-bit or non-amd64 bit architecture
- You can install the latest or specific version of Terraform on Ubuntu
- You can select the customized directory to install Terraform
- You get the flexibility to install more than one version of Terraform in the Linux system. While using the APT repository, you are limited to a single version.
- PATH variable needs to set up after installation
- Package upgrade requires manual intervention
Let's deep dive into the installation of Terraform using the zip file.
Step1- Download Terraform
Visit the www.terraform.io website and download terraform package for Linux. Click on your system architecture and terraform zip file will download to your system.
Alternatively, run "wget" command to download terraform.
$ wget "Link to terraform zip file"
For example - Install a terraform-specific version on Ubuntu. I will download the "terraform 0.14.7 64-bit" version.
$ wget https://releases.hashicorp.com/terraform/0.14.7/terraform_0.14.7_linux_amd64.zip
If you will take a close look at the image. I have downloaded "terraform_0.14.7_linux_amd64.zip" file in "~/Desktop" or "/home/dev/Desktop" location.
Step2 - Extract terraform zip file
Go to your download directory and run unzip command to inflate your Terraform zip file.
$ unzip "terraform-zip-file"
For example - when I ran unzip command on my "terraform 0.14.7 64-bit file". It extracted the "terraform" command in ~/Desktop location.
$ unzip terraform_0.14.7_linux_amd64.zip
Step3- Setup the "Terraform PATH" variable.
You can simply move this extracted terraform file to the "/usr/bin/" directory. This will set up the Terraform PATH without much hassles.
For example, "~/Desktop/terraform" is the path of terraform in my case. You need to replace it with your own in the below-mentioned command. Now run the "which" command to check the path of "terraform" as shown in the image.
$ sudo mv ~/Desktop/terraform /usr/bin/ # Check Path of Terraform $ which
Step4- Check Terraform version
$ terraform -v
Congrats!! you have learned both ways to install Terraform in Ubuntu Linux.
Step5- Install the "auto-complete" terraform extension
You can take benefit of the auto-complete feature of terraform, in case you use Bash or Zsh. The command "terraform -install-autocomplete" will make an entry in your ".bashrc" in your home directory as shown in the image.
If you will grep word "complete" in your ".bashrc". You can see statement "complete -C /usr/bin/terraform terraform" in output.
$ terraform -install-autocomplete
Step6- Run the "source" command
Now run the "source" command to read and execute the ".bashrc" file after changes. Otherwise, autocomplete will not work. For example, you can see that "Terraform auto-complete" is working while pressing the tab twice on my keyboard.
Check out this post on How to create an Azure Virtual machine using Custom data in Terraform
How to uninstall Terraform in Ubuntu
To uninstall terraform, you have again two options. Actually, it depends on the steps you followed to install terraform.
Method #1- To uninstall terraform from Ubuntu using apt
- Run apt remove command to uninstall terraform on Ubuntu. Press "Y" > then Enter when asked.
$ sudo apt remove terraform
Once terraform has been removed. Run terraform -v command. It must result in a "No such file for directory" error. It confirms that you have successfully uninstalled terraform in Ubuntu.
Method #2- To remove terraform manually
If you have completed the installation of "terraform" manually by extracting the zip file. You need to delete the "terraform" command file manually.
$ sudo rm -rf "path to terraform file"
For example, I have extracted and moved the terraform file from ~/Desktop to /usr/bin/ directory. So I will just delete it from the /usr/bin directory using the "rm" command. After that, I will try to run the "terraform" command, it will result in "No such file or directory".
$ sudo rm -rf /usr/bin/terraform
Video Tutorial -
If you are facing any difficulty with text instructions. This video will help you with both methods to install and uninstall terraform in Ubuntu 20.04 effortlessly.
I hope you are able to install Terraform in your Ubuntu Linux system. Let me know, which method you like out of the two. You can reply with your comments for any feedback, queries or suggestion.
See you soon in the next post. Till that time keep learning.