G
GuideDevOps
Lesson 2 of 14

Installing Terraform

Part of the Terraform tutorial series.

What is the Terraform CLI?

Terraform is beautifully simple on the surface. It is distributed as a single, standalone binary file.

You do not need to install complex language runtimes (like Python, Node.js, or Java). You just download the executable, put it in your system's PATH, and you are ready to manage infrastructure.


Installing on MacOS

The easiest way to install Terraform on a Mac is using Homebrew.

  1. First, install the official HashiCorp tap (repository):

    brew tap hashicorp/tap
  2. Then, install the terraform package:

    brew install hashicorp/tap/terraform
  3. To update Terraform in the future, simply run:

    brew upgrade hashicorp/tap/terraform

Installing on Windows

On Windows, the easiest method is using the Chocolatey package manager.

  1. Open a terminal (PowerShell or Command Prompt) as Administrator.
  2. Run the Chocolatey install command:
    choco install terraform

Alternative: If you prefer Winget, you can run:

winget install Hashicorp.Terraform

Installing on Linux (Ubuntu/Debian)

For Linux, you add the official HashiCorp repository to your system's package manager.

  1. Install prerequisite packages:

    sudo apt-get update && sudo apt-get install -y gnupg software-properties-common
  2. Install the HashiCorp GPG key:

    wget -O- https://apt.releases.hashicorp.com/gpg | \
      gpg --dearmor | \
      sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg > /dev/null
  3. Add the official HashiCorp Linux repository:

    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
  4. Update and install:

    sudo apt update
    sudo apt-get install terraform

Verifying the Installation

Regardless of your operating system, verify the installation by opening a new terminal window and running:

terraform -version

You should see output similar to this:

Terraform v1.7.0
on darwin_amd64

If your terminal says command not found: terraform, ensure that the directory containing the Terraform executable is included in your system's PATH environment variable.


Setting Up Your IDE (Crucial!)

While you could write Terraform (.tf) files in Notepad, you absolutely shouldn't. Using a proper IDE will catch syntax errors instantly, provide auto-completion for cloud resources, and automatically format your code.

We highly recommend Visual Studio Code (VS Code).

The Official HashiCorp Extension

  1. Open VS Code.
  2. Go to the Extensions tab (Ctrl+Shift+X or Cmd+Shift+X).
  3. Search for "HashiCorp Terraform".
  4. Install the official extension published by HashiCorp.

What the extension provides:

  • Syntax Highlighting: Makes HCL code beautiful and readable.
  • IntelliSense/Auto-completion: Start typing aws_ and it will suggest aws_instance, aws_vpc, etc., along with all their required arguments!
  • Linting & Validation: Red squiggly lines instantly show you where you made a typo or missed a required parameter.
  • Format on Save: It can automatically run terraform fmt every time you save, ensuring your code always meets team style standards.

Enable Tab Autocompletion in Terminal

If you use Bash or Zsh, Terraform can magically auto-complete its own CLI commands (e.g., typing terraform a and pressing Tab will complete it to terraform apply).

To install the autocomplete package:

terraform -install-autocomplete

You will need to restart your terminal for the changes to take effect.