Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Install Terraform & Packer on Linux
#!/usr/bin/env bash
# Script prerequisite > install jq > https://stedolan.github.io
# ********************************************************************************************
# UPDATE: Check out Robert's repo here https://github.com/robertpeteuil/terraform-installer
# Robert's repo is more built out and has more options around the installation process.
# Cheers! -Adron
# ********************************************************************************************
cd ~
# Prerequisites
if [ "$(uname)" == "Darwin" ]; then
brew install jq
# For Linux
elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then
sudo apt-get install --assume-yes jq
fi
# Get URLs for most recent versions
# For OS-X
if [ "$(uname)" == "Darwin" ]; then
terraform_url=$(curl https://releases.hashicorp.com/index.json | jq '{terraform}' | egrep "darwin.*64" | sort --version-sort -r | head -1 | awk -F[\"] '{print $4}')
packer_url=$(curl https://releases.hashicorp.com/index.json | jq '{packer}' | egrep "darwin.*64" | sort --version-sort -r | head -1 | awk -F[\"] '{print $4}')
# For Linux
elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then
terraform_url=$(curl https://releases.hashicorp.com/index.json | jq '{terraform}' | egrep "linux.*amd64" | sort --version-sort -r | head -1 | awk -F[\"] '{print $4}')
packer_url=$(curl https://releases.hashicorp.com/index.json | jq '{packer}' | egrep "linux.*amd64" | sort --version-sort -r | head -1 | awk -F[\"] '{print $4}')
fi
# Create a move into directory.
cd
mkdir packer
mkdir terraform && cd $_
# Download Terraform. URI: https://www.terraform.io/downloads.html
echo "Downloading $terraform_url."
curl -o terraform.zip $terraform_url
# Unzip and install
unzip terraform.zip
# Change directory to Packer
cd ~/packer
# Download Packer. URI: https://www.packer.io/downloads.html
echo "Downloading $packer_url."
curl -o packer.zip $packer_url
# Unzip and install
unzip packer.zip
if [ "$(uname)" == "Darwin" ]; then
echo '
# Terraform & Packer Paths.
export PATH=~/terraform/:~/packer/:$PATH
' >>~/.bash_profile
source ~/.bash_profile
# For Linux
elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then
echo '
# Terraform & Packer Paths.
export PATH=~/terraform/:~/packer/:$PATH
' >>~/.bashrc
source ~/.bashrc
fi
@ryanmaclean

This comment has been minimized.

Copy link

@ryanmaclean ryanmaclean commented Sep 23, 2016

I've incorporated this into a script that grabs the most recent versions - thanks so much! https://github.com/ryanmaclean/awful-bash-scripts/blob/master/install_recent_terraform_packer.sh

@archit24

This comment has been minimized.

Copy link

@archit24 archit24 commented Nov 29, 2016

thanks for this! very helpful

@Adron

This comment has been minimized.

Copy link
Owner Author

@Adron Adron commented May 2, 2017

Glad this helped as a starting point. Updating this one to reflect additions/changes that @ryanmaclean made. 👍 Thanks for making it better! I've also added logic to pick between Darwin and Linux binaries.

@elocnatsirt

This comment has been minimized.

Copy link

@elocnatsirt elocnatsirt commented May 28, 2017

Came across this as I was creating my script. I wrote a version that will grab most Hashicorp programs and install with the options you specify. https://gist.github.com/elocnatsirt/220c83398a67e2f18477dba3c1aeedf9

Hope this helps anybody looking for/to create a similar script.

@jmaslibre

This comment has been minimized.

Copy link

@jmaslibre jmaslibre commented Dec 9, 2017

@Adron, thanks for this useful script. I would like to use it and include a copy in my repository, under which license terms is published? maybe MIT? Apache software license? GPL ? or something similar?

@carl-utter

This comment has been minimized.

Copy link

@carl-utter carl-utter commented Jan 10, 2018

This script only installs terraform version 0.9.9 and the latest is 0.11.2, so it is not getting the latest version of terraform.

@robertpeteuil

This comment has been minimized.

Copy link

@robertpeteuil robertpeteuil commented Feb 2, 2018

Like a few others in this thread, I came across this after creating my own terraform-installation-script.

If anyone is interested - my script will install the latest version or a specific version (if there are breaking changes you want to avoid).

@jmalibre - my script is MIT license if it works for you. I'm open to changes you may need.

@dolzenko

This comment has been minimized.

Copy link

@dolzenko dolzenko commented Feb 19, 2018

Out of everything I have tried @robertpeteuil's looks like the best solution

@Adron

This comment has been minimized.

Copy link
Owner Author

@Adron Adron commented Mar 30, 2018

I don't (as some have noticed) maintain this script anymore, check out @robertpeteuil's script instead. It's here -> https://github.com/robertpeteuil/terraform-installer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment