Methods to Install Terraform on Ubuntu

Updated: February 3, 2024 By: Guest Contributor Post a comment

Introduction

Installing Terraform on Ubuntu is a simple process that enables you to use this powerful tool for infrastructure automation. Terraform, developed by HashiCorp, is an open-source infrastructure as code software tool that allows you to define and provision a datacenter infrastructure using a high-level configuration language known as HashiCorp Configuration Language (HCL), or optionally JSON.

This tutorial will guide you through several methods to install Terraform on Ubuntu, from the basic to more advanced techniques. We’ll cover the installation process through the official Terraform website, the convenience of using package managers, and wrapping up with how to manage multiple Terraform versions on your system.

Let’s dive in!

Method 1: Installing Terraform from the Official Website

This method involves downloading the Terraform binary directly from the official website. It’s straightforward and ensures you’re getting the tool directly from the source.

  1. First, update your package list to ensure your Ubuntu system is up to date.
    sudo apt-get update
    sudo apt-get upgrade -y
    
  2. Next, go to the Terraform downloads page and find the appropriate version for your system. Right-click on the link for the Linux version and copy the link address for the wget command.
  3. Use wget to download Terraform. Replace the URL in the command below with the one you got from the download page.
    wget https://releases.hashicorp.com/terraform/0.12.0/terraform_0.12.0_linux_amd64.zip
    
  4. Unzip the downloaded file.
    unzip terraform_0.12.0_linux_amd64.zip
    
  5. Move the Terraform binary to a directory in your PATH.
    sudo mv terraform /usr/local/bin/
    
  6. Verify the installation by checking Terraform’s version.
    terraform version
    

    You should see the installed version of Terraform output.

Method 2: Using the HashiCorp Repository

For users aiming for simplicity and the ability to manage Terraform versions through APT, HashiCorp provides a repository. Here’s how to add it and install Terraform.

  1. First, install the GPG key.
    curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
    
  2. Add the HashiCorp Linux repository.
    sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
    
  3. Update your package list.
    sudo apt-get update
    
  4. Install Terraform.
    sudo apt-get install terraform
    
  5. Verify the installation.
    terraform version
    

    You’ll see the installed version of Terraform displayed.

Method 3: Using tfenv for Terraform Version Management

tfenv is a Terraform version manager inspired by rbenv. It allows you to manage multiple Terraform versions easily. Here’s how to install and use it.

  1. First, install tfenv.
    git clone https://github.com/tfutils/tfenv.git ~/.tfenv
    
  2. Add tfenv to your PATH by adding the following line to your shell profile (e.g., .bashrc or .zshrc).
    export PATH="$HOME/.tfenv/bin:$PATH"
    
  3. Reload your shell configuration.
    source ~/.bashrc
    
  4. Install a specific version of Terraform using tfenv.
    tfenv install 0.12.0
    
  5. Switch to your installed version.
    tfenv use 0.12.0
    
  6. Verify your Terraform version.
    terraform version
    

    The specified version should now be active.

Advanced Tips

Once you have Terraform installed, you might want to automate its installation in future setups or scripts. You can use a simple bash script that detects your Ubuntu version, downloads the correct Terraform binary, and installs it automatically. Such scripts are especially useful in automated environment setups like Dockerfile or provisioning scripts with tools like Ansible, Chef, or Puppet.

Conclusion

Whether you’re a beginner or an experienced DevOps professional, setting up Terraform on Ubuntu is a critical first step in automating your infrastructure. By following the methods outlined in this tutorial, you can quickly and easily install Terraform in a way that suits your needs. The flexibility of choosing between direct downloads, package managers, or version managers like tfenv ensures that you have the right tool for your workflow. Happy automating!