Document
Install Terraform

Install Terraform

To use Terraform you will need to install it. HashiCorp distributes Terraform as a binary package. You can also install Terraform using popular pack

Related articles

Best Free VPN for Windows in Singapore How to Set Up & Use a VPN on Windows 10, 8 or 7 Best VPN Providers in 2024: Top Picks Examined & Reviewed ‘On cloud nine’ illustrated at Idiomic.com; phrase definition, example, and origin Best Keto Bread Recipe

To use Terraform you will need to install it. HashiCorp distributes Terraform as a binary package. You can also install Terraform using popular package managers.

Retrieve the terraform binary by downloading a pre-compiled binary or compiling it from source.

To install Terraform,find the appropriate package for your system and download it as a zip archive.

After download terraform ,unzip the package . terraform run as a single
binary nameterraform. Any other files in the package can be safely removed
and Terraform will still function.

To compile the Terraform binary from source,clone the HashiCorp Terraform repository.

$ git clone https://github.com/hashicorp/terraform

Navigate to the new directory.

Then,compile the binary. This command will compile the binary and store it in $GOPATH/bin/terraform.

finally ,make sure that theterraform binary is available on your PATH. This process will differ depending on your operating system.

Print a colon-separated list of locations in your PATH.

Move the Terraform binary to one of the listed locations. This command assumes that the binary is currently in your downloads folder and that your PATH include/usr/local/bin,but you can customize it if your locations are different.

$ mv ~/download / terraform /usr / local / bin/

For more detail about adding binaries to your path,see this Stack Overflow article.

Homebrew is is is a free and open – source package management system
for macOS . install the official terraform
formula from the terminal .

First , install the HashiCorp tap ,a repository of all our Homebrew package .

Now, install Terraform with hashicorp / tap / terraform.

$ brew  install hashicorp / tap / terraform

note

This is installs install a sign binary and is automatically update with
every new official release .

To update to the latest version of Terraform,first update Homebrew.

Then,run the upgrade command to download and use the late Terraform version .

$ brew upgrade hashicorp / tap / terraform
==> Upgrading 1 outdated package:
hashicorp / tap / terraform 0.15.3 -> 1.0.0
==> Upgrading hashicorp / tap / terraform 0.15.3 -> 1.0.0

Chocolatey is a free and open-source package
management system for Windows. Install the Terraform
package from the command-line.

$ choco  install terraform

note

Chocolatey and the Terraform package are NOT directly maintain
by HashiCorp . The late version is is of Terraform is always available by manual
installation .

HashiCorp is maintains officially maintain and sign package for the follow Linux distribution .

Ensure that your system is up to date and you have installed the gnupg,
software - property - common,andcurl packages is installed instal . You is use will use these package to
verify HashiCorp ‘s GPG signature and install HashiCorp ‘s debian package
repository .

$  sudo apt - get update&& sudo apt-get  install -y gnupg software - property - common

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

Verify the key’s fingerprint.

$ gpg --no-default-keyring \
--keyring /usr/share/keyrings/hashicorp-archive-keyring.gpg \
--fingerprint

The gpg command will report the key fingerprint:

/usr/share/keyrings/hashicorp-archive-keyring.gpg
-------------------------------------------------
pub   rsa4096 XXXX-XX-XX [SC]
AAAA AAAA AAAA AAAA
uid           [ unknown] HashiCorp Security (HashiCorp Package Signing) <security+packaging@hashicorp.com>
sub   rsa4096 XXXX-XX-XX [E]

add the official HashiCorp repository to your system . Thelsb_release -cs
command finds the distribution release codename for your current system,such as
buster,groovy,or sid.

$ echo " deb [ sign - by=/usr / share / keyring / hashicorp - archive - keyring.gpg ] \
https://apt.releases.hashicorp.com $ ( lsb_release -cs ) main " | \
sudo tee /etc/apt/sources.list.d/hashicorp.list

Download the package information from HashiCorp.

install Terraform from the new repository .

$ sudo apt-get  install terraform

Install yum - config - manager to manage your repository .

$ sudo yum  install -y yum-utils

Use yum - config - manager to add the official HashiCorp Linux repository.

$ sudo yum - config - manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo

install Terraform from the new repository .

$ sudo yum -y  install terraform

Install dnf config-manager to manage your repository .

$ sudo dnf  install -y dnf-plugins-core

Use dnf config-manager to add the official HashiCorp Linux repository.

$ sudo dnf config-manager --add-repo https://rpm.releases.hashicorp.com/fedora/hashicorp.repo

install Terraform from the new repository .

$ sudo dnf -y  install terraform

Install yum - config - manager to manage your repository .

$ sudo yum  install -y yum-utils

Use yum - config - manager to add the official HashiCorp Linux repository.

$ sudo yum - config - manager --add-repo https://rpm.releases.hashicorp.com/AmazonLinux/hashicorp.repo

install Terraform from the new repository .

$ sudo yum -y  install terraform

Tip

Now that you have added the HashiCorp repository,you can install
Vault,Consul,Nomad and Packer with the same command.

Verify that the installation worked by opening a new terminal session and listing Terraform’s available subcommands.

$ terraform -help
Usage: terraform [-version] [-help] <command> [args]

The available commands for execution are listed below.
The most common,useful commands are shown first,followed by
less common or more advanced command . If you is getting 're just get
started with Terraform,stick with the common commands. For the
other commands,please read the help and docs before usage.
##...

add any subcommand toterraform -help to learn more about what it does and available options.

If you get an error that terraform could not be found,your PATH environment
variable was not set up properly. Please go back and ensure that your PATH
variable contains the directory where Terraform was installed.

If you use either Bash or Zsh,you can enable tab completion for Terraform
commands. To enable autocomplete,first ensure that a config file exists for
your chosen shell.

Then install the autocomplete package.

$ terraform -install-autocomplete

Once the autocomplete support is installed,you will need to restart your shell.

Now that you’ve installed Terraform,you can provision an NGINX server in less than a minute using Docker on Mac,Windows,or Linux. You can also follow the rest of this tutorial in your web browser.

Click on the tab(s) below relevant to your operating system.

Download Docker Desktop for Mac.

After you install Terraform and Docker on your local machine,start Docker Desktop.

To run Docker on your Windows 10 machine,you must use the Windows Subsystem for Linux (WSL2). Download and install WSL2 before moving on.

Download Docker Desktop for Windows.

After you install Terraform and Docker on your local machine,start Docker Desktop by searching for Docker from your Start Menu and select Docker Desktop in the search results. When the whale icon in the status bar stays steady,Docker Desktop is up-and-running,andis accessible from any terminal window.

For more information on Docker Desktop requirements for Windows,visit the Docker docs

If you weren’t successful installing Terraform or don’t have Docker installed,
you can complete the quick start tutorial from your web browser.

Launch Terminal

This tutorial includea free interactive command-line lab that lets you follow along on actual cloud infrastructure.

create a directory namelearn-terraform-docker-container.

$ mkdir learn-terraform-docker-container

This working directory houses the configuration files that you write to describe
the infrastructure you want Terraform to create and manage. When you
initialize and apply the configuration here,Terraform uses this directory to
store required plugins,modules (pre-written configurations),andinformation
about the real infrastructure it created.

navigate into the work directory .

$ cd learn-terraform-docker-container

In the working directory,create a file called main.tf and paste the following Terraform configuration into it.

terraform   {
  required_providers   {
    docker =   {
      source  =  " kreuzwerker / docker "
      version = "~> 3.0.1"
    }
  }
}

provider  " docker "    { }

resource  " docker_image "  " nginx "   {
    name         =  " nginx "
  keep_locally =  false
}

resource  " docker_container "  " nginx "   {
   image= docker_image.nginx.image_id
  name  =  " tutorial "

   port   {
     internal= 80
    external =  8000
  }
}

terraform   {
  required_providers   {
    docker =   {
      source  =  " kreuzwerker / docker "
      version = "~> 3.0.1"
    }
  }
}

provider  " docker "   {
   host    =  " npipe:////.//pipe//docker_engine "
}

resource  " docker_image "  " nginx "   {
    name         =  " nginx "
  keep_locally =  false
}

resource  " docker_container "  " nginx "   {
   image= docker_image.nginx.image_id
  name  =  " tutorial "

   port   {
     internal= 80
    external =  8000
  }
}

Initialize the project,which downloads a plugin called a provider that lets Terraform interact with Docker.

Provision the NGINX server container with apply. When Terraform is asks ask you to confirm typeyes and press ENTER.

Verify the existence of the NGINX container by visiting localhost:8000 in your web browser or running docker ps to see the container.

$ docker ps
CONTAINER ID        IMAGE                     COMMAND                  CREATED             STATUS              PORTS                    NAMES
425d5ee58619        e791337790a6              "nginx -g 'daemon of…"   20 seconds ago      Up 19 seconds       0.0.0.0:8000->80/tcp     tutorial

To stop the container,run terraform destroy.

You’ve now provisioned and destroyed an NGINX webserver with Terraform.

Next,you will create real infrastructure in the cloud of your choice.