Terraform - Install
- Installation options:
- Manual:
- pre-compiled binary
- compile from source
- Homebrew on Mac OS
- Chocolatey on Windows
- Linux
- Ubuntu/Debian
- CentOS/RHEL
- Fedora
- Amazon Linux
- Manual:
Pre-compiled Binary
- This is for Linux / MacOS
Download the version for your system from here: https://developer.hashicorp.com/terraform/downloads
Uncompress
unzip terraform_1.5.1_linux_amd64.zip
Run it from current location:
./terraform
- Place it on your path ( see below )
Compile from Source
- This is for Linux / MacOS
This needs golang to be installed and setup first. See our Install Golang page.
Clone the Terraform repo:
git clone https://github.com/hashicorp/terraform.git
cd terraform
Build it:
go install
It will be placed here:
$GOPATH/bin/terraform
- Place it on your path ( see below )
Place it on your Path:
Check your path, and place it into one of the listed directories:
echo $PATH
Alternatively, copy to a directory of your choice (ex. home dir ) and add that directory to your path:
mv terraform ~
echo "export PATH=$PATH:~" >> ~/.bashrc
source ~/.bashrc
Verify:
terraform -v
Homebrew on Mac OS
Install the HashiCorp repo ( tap ):
brew tap hashicorp/tap
Install terraform:
brew install hashicorp/tap/terraform
Verify:
terraform -v
Upgrade Homebrew and Terraform:
brew update
brew upgrade hashicorp/tap/terraform
Chocolatey on Windows
- NOTE - Terraform on Chocolatey is endorsed but not directly maintained by HashiCorp.
choco install terraform
Verify:
terraform -v
Linux Packages
Ubuntu/Debian
Update repo and make sure these are installed first:
sudo apt-get update && sudo apt-get install -y gnupg software-properties-common curl
Install HashiCorp key:
wget -O- https://apt.releases.hashicorp.com/gpg | \
gpg --dearmor | \
sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg
Verify:
gpg --no-default-keyring \
--keyring /usr/share/keyrings/hashicorp-archive-keyring.gpg \
--fingerprint
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
Update repo info:
sudo apt update
Actually install terraform:
sudo apt-get install terraform
CentOS/RHEL
Install yum-config-manager:
sudo yum install -y yum-utils
Install HashiCorp repo:
sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
Install Terraform:
sudo yum -y install terraform
Fedora
Install dnf config-manager
sudo dnf install -y dnf-plugins-core
Install HashiCorp repo:
sudo dnf config-manager --add-repo https://rpm.releases.hashicorp.com/fedora/hashicorp.repo
Install Terraform:
sudo dnf -y install terraform
Amazon Linux
Install yum-config-manager:
sudo yum install -y yum-utils
Install HashiCorp repo:
sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/AmazonLinux/hashicorp.repo
Install Terraform:
sudo yum -y install terraform
Enable tab completion
touch ~/.bashrc # for bash
touch ~/.zshrc # for zsh
terraform -install-autocomplete
Terraform First Test / Demo
Create a directory for the configuration.
mkdir test1
cd test1
Define resources and other components here:
main.tfterraform { 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" ports { internal = 80 external = 8000 } }
Change your profider if you’re running Windows:
provider "docker" {
host = "npipe:////.//pipe//docker_engine"
}
Initialize, plan, and apply ( more on these in the next section ):
terraform init
terraform plan
terraform apply
You should be able to see a docker container running:
docker ps
You should be able to reach default NGINX web page here:
localhost:8000 |
Destroy your infrastructure with this:
terraform destroy
Help commands:
terraform -help
terraform -help plan