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 package managers.
Install Terraform
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 downloading Terraform, unzip the package. Terraform runs as a single
binary named terraform
. Any other files in the package can be safely removed
and Terraform will still function.
Finally, make sure that the terraform
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
includes /usr/local/bin
, but you can customize it if your locations are different.
For more detail about adding binaries to your path, see this Stack Overflow article.
Verify the installation
Verify that the installation worked by opening a new terminal session and listing Terraform's available subcommands.
Add any subcommand to terraform -help
to learn more about what it does and available options.
Troubleshoot
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.
Enable tab completion
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.
Once the autocomplete support is installed, you will need to restart your shell.
Quick start tutorial
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.
Create a directory named 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), and information about the real infrastructure it created.
Navigate into the working directory.
In the working directory, create a file called main.tf
and paste the following Terraform configuration into it.
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 asks you to confirm type yes
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.
To stop the container, run terraform destroy
.
You've now provisioned and destroyed an NGINX webserver with Terraform.
Next Steps
Next, you will create real infrastructure in the cloud of your choice.