Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Install specific version of a Homebrew formula
# Install specific version of a Homebrew formula
# Usage: formula_name desired_version
# Notes:
# - this will unshallow your brew repo copy. It might take some time the first time
# you call this script
# - it will uninstall (instead of unlink) all your other versions of the formula.
# Proper version management is out of scope of this script. Feel free to improve.
# - my "git log" uses less by default and when that happens it breaks the script
# Therefore we have the "--max-count=20" parameter. This might fail to find proper
# version if the one you wish to install is outside of this count.
# Author: Stanimir Karoserov ( )
if [ "$#" -ne 2 ]; then
echo " - installs specific version of a brew formula"
echo "syntax: formula_name desired_version"
echo "e.g.: swiftformat 0.39.1"
exit 1
git -C "$(brew --repo homebrew/core)" fetch --unshallow || echo "Homebrew repo already unshallowed"
commit=$(brew log --max-count=20 --oneline $1|grep $2| head -n1| cut -d ' ' -f1)
formula=$(brew log --max-count=20 --oneline $1|grep $2| head -n1| cut -d ':' -f1|cut -d ' ' -f2)
if [ -z ${commit} ] || [ -z ${formula} ]; then
echo "No version matching '$2' for '$1'"
exit 1
cd /usr/local/bin
if [[ -e $formula ]]; then
brew uninstall --force $1
brew install$commit/Formula/$formula.rb
echo "$1 $2 installed."

This comment has been minimized.

Copy link

@ox0spy ox0spy commented Mar 7, 2019

Thank you for sharing your script, it's useful!

commit=$(brew log --max-count=20 --oneline $1|grep $2| head -n1| cut -d ' ' -f1)

grep $2 will use regex, so grep 3.1 will match 3*1, e.g.: 301, 311, it will also match git commit, not just formula version.


  • using grep -F, -F, --fixed-strings, Interpret PATTERNS as fixed strings, not regular expressions.
  • grep -F " $2 "

the difference:

$ brew log --max-count=100 --oneline gradle | grep -F 3.1
6f91436288 gradle 4.3.1 (#20420)
7a6a40e0ac gradle 3.1 (#4982)
$ brew log --max-count=100 --oneline gradle | grep -F " 3.1 "
7a6a40e0ac gradle 3.1 (#4982)

I fork and updated it.


This comment has been minimized.

Copy link

@voiski voiski commented May 27, 2019

Nice to have it shared here, it gave me directions to my problem. Thank you @ox0spy for the grep tip.

I also want to mention this comment that helped me to figure out how to not uninstall the formula. My final solution will just upgrade the existing one.

I also made the max-lines an optional parameter together with the desired tap with default to homebrew core. Please check my fork =D


This comment has been minimized.

Copy link

@DKroot DKroot commented Aug 11, 2020

It worked. Thanks for sharing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.