Skip to content

Instantly share code, notes, and snippets.

@zulhfreelancer
Last active July 14, 2021 15:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zulhfreelancer/be012fbdca82513904e95cdd1f3d2ed3 to your computer and use it in GitHub Desktop.
Save zulhfreelancer/be012fbdca82513904e95cdd1f3d2ed3 to your computer and use it in GitHub Desktop.
How to install an older or specific version of Homebrew tap package?

Problem

When using Homebrew Tap, it's quite hard to install an older or specific version of Homebrew package.

It took me awhile to run some tests before I found the solution (see below).

If you are facing the same problem, you can use my script below to solve the problem.

Solution

  1. Download the script below to your local machine
  2. Run $ chmod +x /path/to/install-specific-version-homebrew-tap-package.sh
  3. Update all lines that start with export keywords
  4. Run /path/to/install-specific-version-homebrew-tap-package.sh to execute the script
  5. Profit

Caveat

This solution may break your brew update command. If you are not able to perform brew update correctly, you can untap all tapped formulae using this command:

$ brew tap | xargs -I % sh -c 'brew untap --force %'
#!/bin/bash
set -e
export TARGET_PACKAGE_NAME=terraform
export TARGET_PACKAGE_VERSION=0.15.0
export TARGET_PACKAGE_REPO_OWNER=hashicorp
export TARGET_PACKAGE_REPO_NAME=homebrew-tap
DIRECTORY_PREFIX=$(date +%s)
TARGET_PACKAGE_VERSION_WITHOUT_DOT=$(echo $TARGET_PACKAGE_VERSION | sed 's/\.//g')
FULL_TARGET_REPO_PATH=$TARGET_PACKAGE_REPO_OWNER/$TARGET_PACKAGE_REPO_NAME
FULL_TARGET_PACKAGE_PATH=$FULL_TARGET_REPO_PATH/$TARGET_PACKAGE_NAME
TAP_SOURCE_LOCAL_DIRECTORY_PATH=/usr/local/Homebrew/Library/Taps/$FULL_TARGET_REPO_PATH
LOCAL_DIRECTORY_PATH=$USER/$DIRECTORY_PREFIX-$TARGET_PACKAGE_NAME-$TARGET_PACKAGE_VERSION_WITHOUT_DOT
LOCAL_PACKAGE_PATH=$TARGET_PACKAGE_NAME@$TARGET_PACKAGE_VERSION
echo "======"
echo "Setupz"
echo "======"
echo "TARGET_PACKAGE_NAME : $TARGET_PACKAGE_NAME"
echo "TARGET_PACKAGE_VERSION : $TARGET_PACKAGE_VERSION"
echo "TARGET_PACKAGE_REPO_OWNER : $TARGET_PACKAGE_REPO_OWNER"
echo "TARGET_PACKAGE_REPO_NAME : $TARGET_PACKAGE_REPO_NAME"
echo "DIRECTORY_PREFIX : $DIRECTORY_PREFIX"
echo "TARGET_PACKAGE_VERSION_WITHOUT_DOT : $TARGET_PACKAGE_VERSION_WITHOUT_DOT"
echo "FULL_TARGET_REPO_PATH : $FULL_TARGET_REPO_PATH"
echo "FULL_TARGET_PACKAGE_PATH : $FULL_TARGET_PACKAGE_PATH"
ECHO "TAP_SOURCE_LOCAL_DIRECTORY_PATH : $TAP_SOURCE_LOCAL_DIRECTORY_PATH"
echo "LOCAL_DIRECTORY_PATH : $LOCAL_DIRECTORY_PATH"
echo "LOCAL_PACKAGE_PATH : $LOCAL_PACKAGE_PATH"
echo
echo "======"
echo "Step 1"
echo "======"
if [ -d $TAP_SOURCE_LOCAL_DIRECTORY_PATH ]
then
echo "Already tapped $FULL_TARGET_REPO_PATH (source)."
echo "It's located at $TAP_SOURCE_LOCAL_DIRECTORY_PATH. Skipping this step."
else
brew tap $FULL_TARGET_REPO_PATH
fi
echo
echo "======"
echo "Step 2"
echo "======"
brew tap-new $LOCAL_DIRECTORY_PATH
echo
echo "======"
echo "Step 3"
echo "======"
brew extract --version $TARGET_PACKAGE_VERSION $FULL_TARGET_PACKAGE_PATH $LOCAL_DIRECTORY_PATH
echo
echo "======"
echo "Step 4"
echo "======"
brew install $LOCAL_DIRECTORY_PATH/$LOCAL_PACKAGE_PATH
echo
echo "======"
echo "Step 5"
echo "======"
brew list | grep $LOCAL_PACKAGE_PATH
echo
echo "======"
echo "Finish"
echo "======"
# Here is how I installed terraform v0.15.0 (quite an old build)
$ ./install-old-homebrew-package.sh
======
Setupz
======
TARGET_PACKAGE_NAME : terraform
TARGET_PACKAGE_VERSION : 0.15.0
TARGET_PACKAGE_REPO_OWNER : hashicorp
TARGET_PACKAGE_REPO_NAME : homebrew-tap
DIRECTORY_PREFIX : 1622234000
TARGET_PACKAGE_VERSION_WITHOUT_DOT : 0150
FULL_TARGET_REPO_PATH : hashicorp/homebrew-tap
FULL_TARGET_PACKAGE_PATH : hashicorp/homebrew-tap/terraform
TAP_SOURCE_LOCAL_DIRECTORY_PATH : /usr/local/Homebrew/Library/Taps/hashicorp/homebrew-tap
LOCAL_DIRECTORY_PATH : zulh/1622234000-terraform-0150
LOCAL_PACKAGE_PATH : terraform@0.15.0
======
Step 1
======
Already tapped hashicorp/homebrew-tap (source).
It's located at /usr/local/Homebrew/Library/Taps/hashicorp/homebrew-tap. Skipping this step.
======
Step 2
======
Initialized empty Git repository in /usr/local/Homebrew/Library/Taps/zulh/homebrew-1622234000-terraform-0150/.git/
[master (root-commit) 13824ff] Create zulh/1622234000-terraform-0150 tap
3 files changed, 88 insertions(+)
create mode 100644 .github/workflows/publish.yml
create mode 100644 .github/workflows/tests.yml
create mode 100644 README.md
==> Created zulh/1622234000-terraform-0150
/usr/local/Homebrew/Library/Taps/zulh/homebrew-1622234000-terraform-0150
When a pull request making changes to a formula (or formulae) becomes green
(all checks passed), then you can publish the built bottles.
To do so, label your PR as `pr-pull` and the workflow will be triggered.
======
Step 3
======
==> Searching repository history
==> Writing formula for terraform from revision 4eacb08 to:
/usr/local/Homebrew/Library/Taps/zulh/homebrew-1622234000-terraform-0150/Formula/terraform@0.15.0.rb
======
Step 4
======
==> Installing terraform@0.15.0 from zulh/1622234000-terraform-0150
==> Downloading https://releases.hashicorp.com/terraform/0.15.0/terraform_0.15.0_darwin_amd64.zip
Already downloaded: /Users/zulh/Library/Caches/Homebrew/downloads/cfc493c49fff1c97752b8de26ef34ec1eff5f9fde7ea7ba8c371ec6d141307b7--terraform_0.15.0_darwin_amd64.zip
🍺 /usr/local/Cellar/terraform@0.15.0/0.15.0: 3 files, 76.4MB, built in 3 seconds
======
Step 5
======
terraform@0.15.0
======
Finish
======
$ terraform -v
Terraform v0.15.0
on darwin_amd64
Your version of Terraform is out of date! The latest version
is 0.15.4. You can update by downloading from https://www.terraform.io/downloads.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment