Skip to content

Instantly share code, notes, and snippets.

@danisla
Last active July 15, 2024 13:36
Show Gist options
  • Save danisla/0a394c75bddce204688b21e28fd2fea5 to your computer and use it in GitHub Desktop.
Save danisla/0a394c75bddce204688b21e28fd2fea5 to your computer and use it in GitHub Desktop.
Terraform latest version install script
#!/bin/bash
function terraform-install() {
[[ -f ${HOME}/bin/terraform ]] && echo "`${HOME}/bin/terraform version` already installed at ${HOME}/bin/terraform" && return 0
LATEST_URL=$(curl -sL https://releases.hashicorp.com/terraform/index.json | jq -r '.versions[].builds[].url' | sort -t. -k 1,1n -k 2,2n -k 3,3n -k 4,4n | egrep -v 'rc|beta' | egrep 'linux.*amd64' |tail -1)
curl ${LATEST_URL} > /tmp/terraform.zip
mkdir -p ${HOME}/bin
(cd ${HOME}/bin && unzip /tmp/terraform.zip)
if [[ -z $(grep 'export PATH=${HOME}/bin:${PATH}' ~/.bashrc) ]]; then
echo 'export PATH=${HOME}/bin:${PATH}' >> ~/.bashrc
fi
echo "Installed: `${HOME}/bin/terraform version`"
cat - << EOF
Run the following to reload your PATH with terraform:
source ~/.bashrc
EOF
}
terraform-install
@hk1313
Copy link

hk1313 commented Apr 24, 2020

@danisla, your script is awesome but now latest terraform version is 0.12.24 but still it extract 0.12.9. so you may need to change line five for sort 5 number in version.
"LATEST_URL=$(curl https://releases.hashicorp.com/terraform/index.json | jq -r '.versions[].builds[].url' | sort -t. -k 1,1n -k 2,2n -k 3,3n -k 4,4n -k 5,5n | egrep -v 'rc|beta' | egrep 'linux.*amd64' |tail -1)"

@yanivpaz
Copy link

yanivpaz commented Aug 13, 2021

I replaced with

curl -sL https://releases.hashicorp.com/terraform/index.json | jq -r '.versions[].builds[].url' | egrep -v 'rc|beta|alpha' | egrep 'linux.*amd64'  | tail -1

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