Homebrew install specific version of formula?
Asked Answered
S

36

2820

How do I install a specific version of a formula in homebrew? For example, postgresql-8.4.4 instead of the latest 9.0.

Solace answered 21/10, 2010 at 12:58 Comment(7)
postgresql is a weird one because it had separate versions in Homebrew at one point, something like bash might be a better example since it went from 4.x to 5.x within the same formula name. See https://mcmap.net/q/40449/-homebrew-install-specific-version-of-formulaPrimogeniture
General purpose formula reinstallation instructions.Vice
I read all the answers. It's 2020 and there is no easy way or official command to do thatMuster
@Muster there’s no easy way because it’s not an easy problem. Either Homebrew supports old versions (= more burden) either it doesn’t and you have to do it yourself.Contrariwise
I would suggest using Docker PostgreSQL's images for various versions. Quite easy for tests with an empty db, and fairly easy if you want to persist the data.Muenster
This thread has become very long and containt lots of outdated answers. The proper answer in 2022 is this solution, using brew extract: stackoverflow.com/a/66458452Grimbal
I found this blog which worked for me quite well - mikebian.co/installing-an-old-homebrew-packageOrjonikidze
F
2977

TLDR: brew install [email protected] See answer below for more details.


*(I’ve re-edited my answer to give a more thorough workflow for installing/using older software versions with homebrew. Feel free to add a note if you found the old version better.)

Let’s start with the simplest case:

1) Check, whether the version is already installed (but not activated)

When homebrew installs a new formula, it puts it in a versioned directory like /usr/local/Cellar/postgresql/9.3.1. Only symbolic links to this folder are then installed globally. In principle, this makes it pretty easy to switch between two installed versions. (*)

If you have been using homebrew for longer and never removed older versions (using, for example brew cleanup), chances are that some older version of your program may still be around. If you want to simply activate that previous version, brew switch is the easiest way to do this.

Check with brew info postgresql (or brew switch postgresql <TAB>) whether the older version is installed:

$ brew info postgresql
postgresql: stable 9.3.2 (bottled)
http://www.postgresql.org/
Conflicts with: postgres-xc
/usr/local/Cellar/postgresql/9.1.5 (2755 files, 37M)
  Built from source
/usr/local/Cellar/postgresql/9.3.2 (2924 files, 39M) *
  Poured from bottle
From: https://github.com/Homebrew/homebrew/commits/master/Library/Formula/postgresql.rb
# … and some more

We see that some older version is already installed. We may activate it using brew switch:

$ brew switch postgresql 9.1.5
Cleaning /usr/local/Cellar/postgresql/9.1.5
Cleaning /usr/local/Cellar/postgresql/9.3.2
384 links created for /usr/local/Cellar/postgresql/9.1.5

Let’s double-check what is activated:

$ brew info postgresql
postgresql: stable 9.3.2 (bottled)
http://www.postgresql.org/
Conflicts with: postgres-xc
/usr/local/Cellar/postgresql/9.1.5 (2755 files, 37M) *
  Built from source
/usr/local/Cellar/postgresql/9.3.2 (2924 files, 39M)
  Poured from bottle
From: https://github.com/Homebrew/homebrew/commits/master/Library/Formula/postgresql.rb
# … and some more

Note that the star * has moved to the newly activated version

(*) Please note that brew switch only works as long as all dependencies of the older version are still around. In some cases, a rebuild of the older version may become necessary. Therefore, using brew switch is mostly useful when one wants to switch between two versions not too far apart.

2) Check, whether the version is available as a tap

Especially for larger software projects, it is very probably that there is a high enough demand for several (potentially API incompatible) major versions of a certain piece of software. As of March 2012, Homebrew 0.9 provides a mechanism for this: brew tap & the homebrew versions repository.

That versions repository may include backports of older versions for several formulae. (Mostly only the large and famous ones, but of course they’ll also have several formulae for postgresql.)

brew search postgresql will show you where to look:

$ brew search postgresql
postgresql
homebrew/versions/postgresql8    homebrew/versions/postgresql91
homebrew/versions/postgresql9    homebrew/versions/postgresql92

We can simply install it by typing

$ brew install homebrew/versions/postgresql8
Cloning into '/usr/local/Library/Taps/homebrew-versions'...
remote: Counting objects: 1563, done.
remote: Compressing objects: 100% (943/943), done.
remote: Total 1563 (delta 864), reused 1272 (delta 620)
Receiving objects: 100% (1563/1563), 422.83 KiB | 339.00 KiB/s, done.
Resolving deltas: 100% (864/864), done.
Checking connectivity... done.
Tapped 125 formula
==> Downloading http://ftp.postgresql.org/pub/source/v8.4.19/postgresql-8.4.19.tar.bz2
# …

Note that this has automatically tapped the homebrew/versions tap. (Check with brew tap, remove with brew untap homebrew/versions.) The following would have been equivalent:

$ brew tap homebrew/versions
$ brew install postgresql8

As long as the backported version formulae stay up-to-date, this approach is probably the best way to deal with older software.

3) Try some formula from the past

The following approaches are listed mostly for completeness. Both try to resurrect some undead formula from the brew repository. Due to changed dependencies, API changes in the formula spec or simply a change in the download URL, things may or may not work.

Since the whole formula directory is a git repository, one can install specific versions using plain git commands. However, we need to find a way to get to a commit where the old version was available.

a) historic times

Between August 2011 and October 2014, homebrew had a brew versions command, which spat out all available versions with their respective SHA hashes. As of October 2014, you have to do a brew tap homebrew/boneyard before you can use it. As the name of the tap suggests, you should probably only do this as a last resort.

E.g.

$ brew versions postgresql
Warning: brew-versions is unsupported and may be removed soon.
Please use the homebrew-versions tap instead:
  https://github.com/Homebrew/homebrew-versions
9.3.2    git checkout 3c86d2b Library/Formula/postgresql.rb
9.3.1    git checkout a267a3e Library/Formula/postgresql.rb
9.3.0    git checkout ae59e09 Library/Formula/postgresql.rb
9.2.4    git checkout e3ac215 Library/Formula/postgresql.rb
9.2.3    git checkout c80b37c Library/Formula/postgresql.rb
9.2.2    git checkout 9076baa Library/Formula/postgresql.rb
9.2.1    git checkout 5825f62 Library/Formula/postgresql.rb
9.2.0    git checkout 2f6cbc6 Library/Formula/postgresql.rb
9.1.5    git checkout 6b8d25f Library/Formula/postgresql.rb
9.1.4    git checkout c40c7bf Library/Formula/postgresql.rb
9.1.3    git checkout 05c7954 Library/Formula/postgresql.rb
9.1.2    git checkout dfcc838 Library/Formula/postgresql.rb
9.1.1    git checkout 4ef8fb0 Library/Formula/postgresql.rb
9.0.4    git checkout 2accac4 Library/Formula/postgresql.rb
9.0.3    git checkout b782d9d Library/Formula/postgresql.rb

As you can see, it advises against using it. Homebrew spits out all versions it can find with its internal heuristic and shows you a way to retrieve the old formulae. Let’s try it.

# First, go to the homebrew base directory
$ cd $( brew --prefix )
# Checkout some old formula
$ git checkout 6b8d25f Library/Formula/postgresql.rb
$ brew install postgresql
# … installing

Now that the older postgresql version is installed, we can re-install the latest formula in order to keep our repository clean:

$ git checkout -- Library/Formula/postgresql.rb

brew switch is your friend to change between the old and the new.

b) prehistoric times

For special needs, we may also try our own digging through the homebrew repo.

$ cd Library/Taps/homebrew/homebrew-core && git log -S'8.4.4' -- Formula/postgresql.rb

git log -S looks for all commits in which the string '8.4.4' was either added or removed in the file Library/Taps/homebrew/homebrew-core/Formula/postgresql.rb. We get two commits as a result.

commit 7dc7ccef9e1ab7d2fc351d7935c96a0e0b031552
Author: Aku Kotkavuo
Date:   Sun Sep 19 18:03:41 2010 +0300

    Update PostgreSQL to 9.0.0.

    Signed-off-by: Adam Vandenberg

commit fa992c6a82eebdc4cc36a0c0d2837f4c02f3f422
Author: David Höppner
Date:   Sun May 16 12:35:18 2010 +0200

    postgresql: update version to 8.4.4

Obviously, fa992c6a82eebdc4cc36a0c0d2837f4c02f3f422 is the commit we’re interested in. As this commit is pretty old, we’ll try to downgrade the complete homebrew installation (that way, the formula API is more or less guaranteed to be valid):

$ git checkout -b postgresql-8.4.4 fa992c6a82eebdc4cc36a0c0d2837f4c02f3f422
$ brew install postgresql
$ git checkout master
$ git branch -d postgresql-8.4.4

You may skip the last command to keep the reference in your git repository.

One note: When checking out the older commit, you temporarily downgrade your homebrew installation. So, you should be careful as some commands in homebrew might be different to the most recent version.

4) Manually write a formula

It’s not too hard and you may then upload it to your own repository. Used to be Homebrew-Versions, but that is now discontinued.

A.) Bonus: Pinning

If you want to keep a certain version of, say postgresql, around and stop it from being updated when you do the natural brew update; brew upgrade procedure, you can pin a formula:

$ brew pin postgresql

Pinned formulae are listed in /usr/local/Library/PinnedKegs/ and once you want to bring in the latest changes and updates, you can unpin it again:

$ brew unpin postgresql
Farro answered 11/11, 2010 at 20:4 Comment(54)
You can also just checkout the formula using git checkout fa992 -- Library/Formula/postgresql.rb. When you're done, you can undo the changes to the formula using git revert HEAD Library/Formula/postgresql.rb && git checkout -- Library/Formula/postgresql.rb.Unshackle
Postgresql v8.4.4 is no longer at the ftp server that the formula refers to. It is easy, though, to change the formula to reference the 8.4.6, which may work for you. The MD5 in the formula also needs to change - the correct MD5 will be reported when you try to brew install.Caron
Note: on a fresh Homebrew install, you may need to brew update to establish its Git repo.Amygdala
I'm a brew newbie (brewbie?) and I ran the git checkout ... for a specific mysql version, but now what? I tried brew install mysql at this point, but I get Error: This is a head-only formula; install with "brew install --HEAD mysql". Then I try brew install --HEAD mysql and I get Error: No head is defined for mysql. Am I missing something obvious?Pegpega
For update I, I had to brew unlink <formula> before running the brew install partSpillar
You don't mention brew switch at all. Isn't that better?Brake
@AmirRaminfar brew switch will not install or find any previously uninstalled versions.Farro
Hmm I don't think that is is true anymore. When I do brew versions wget I get all of the older versions of wget and I can switch to any of them.Brake
@AmirRaminfar: I cannot test it at the moment but brew-switch.rb does not look like it is going to do any installations. (And I doubt this would be easily possible since there may have been slight changes in the formula DSL.)Farro
Curious why brew tap asks for a github password?Genista
This didn't work for me. brew switch however, worked perfectly.Saxena
Actually brew switch didn't work for me. For example I wanted to install a cassandra specific version shown with brew versions cassandra, but when running this coammand brew switch cassandra 1.2.9 I got the following error Error: cassandra does not have a version "1.2.9" in the Cellar.. In my opinion the workflow of the switch command described in the next answer should be included in the homebrew switch command .Panelboard
For brew switch to work, you need to have the version already installed (= in the cellar) but inactive. brew versions gives you a git command which you may want to use to checkout the older version’s formula.Farro
@Unshackle - Did you mean git reset instead of git revert in your comment?Sometimes
@AronAhmadia: Oops, yes, I believe I did.Unshackle
When I want to change to an earlier version of MySQL, I used this way but forgot to check out the specific formula, I checked out an old version of the git repo. Then I have to manually delete all the additional formula.Jung
The brew switch trick is not documented in the manual, where did you find it?!Stclair
Thanks, @jrmgx. Now it’s in brew tap homebrew/boneyard. Updated the part above.Farro
This answer is misleading, since it refers to commands ("which", "versions") which don't exist in the current (May 2015) version of brew anymore. Please, @Debilski, do us a favour and update it or remove it. Thanks!Hairpin
Thanks, @Hairpin for noticing. I removed the reference to brew which but kept the brew versions still in for now, as it is stated in the paragraph that it won’t work anymore nowadays.Farro
brew versions is gone now. If you are using Option 2, and you need to alternate between two versions installed from homebrew\versions, you can use this brew unlink postgresql8 && brew link postgresql92.Leaseback
With respect to 3b, brew --repository will tell you where homebrew's git repository lives.Alves
This is ridiculously difficult.Nikos
These commands should be executed from $(brew --prefix) folder.Box
You should update this answer to include the way to install versions from the git history, as mentioned by @halfcubeBeers
Hi! As a Homebrew maintainer, I just want to be clear that the supported methods are to use a Homebrew/versions formula or to adapt a formula to install the version that you need. Installing a formula from git history cannot be made to work reliably because the Homebrew ecosystem evolves over time, which is why the versions command was removed. If it works on your dev machine, great, but please don't rely on git manipulations or historic formulas working robustly. Homebrew's stable CLI is the command-line interface.Liss
One other thing to be aware of is that as of early April, the main formulas are split into a separate repo, so you can no longer do git manipulations at the /usr/local root as you could before. I was still able to hack around in the core formulas which are now a tap at /usr/local/Library/Taps/homebrew/homebrew-core, but as @TimSmith points out: this is not really supported and sort of a crapshoot.Jeroboam
do flags not work with versions? I tried: brew install homebrew/versions/ffmpeg28 --with-fontconfig --with-freetype but its not compiling with those flags. Works fine on default ffmpeg 3.x installUndaunted
NEVERMIND It actually did build with those flags -- output was cut off. You can check with brew info ffmpeg28Undaunted
If all you want to do is install a development or beta version of a particular software you can use the --devel flag. Eg: brew install go --develContractor
As of today, the correct directory to execute the manual commands against the repository is cd $(brew --repository)/Library/Taps/homebrew/homebrew-core, then you can use git checkout 4cce79d -- Formula/go.rb and HOMEBREW_NO_AUTO_UPDATE=1 brew install go.Therapist
Seems need to uncheck that answer and check new workingBrandish
homebrew/versions is deprecatedSomebody
If I could give you a million up votes, I'd do it. This completely saved my life today.Chante
If none of these methods work to find the package you want, you may be able to find it with #39188312Weakling
brew versions cassandra doesn't work with Homebrew 1.3.9Germain
@TimSmith, But if we don't have the versions command anymore, how can one use the switch command if you don't know what versions were available for the respective package?!Cherokee
Pinned formulae are now listed in /usr/local/var/homebrew/pinned/ and the cited path no longer exists /usr/local/Library/PinnedKegs/Coparcener
Folks, let's face it, we all have been looking forward to seeing this solution: brew install/upgrade/downgrade [email protected] and it's not supported!Ogilvie
additionally to 3a, apart from what @Calin said, you might need to get the full version of homebrew, because you probably only have a shallow: git -C "$(brew --repo homebrew/core)" fetch --unshallow. For me git log into a formula only returned 1 commit.Redingote
In the newest version, even after I <brew tap homebrew/boneyard>Partake
In the newest version of homebrew, even after I brew tap homebrew/boneyard. When I brew versions opencv, it will still show `Error: Unknown command: versions. Can anyone help?Partake
brew tap homebrew/boneyard - remote: Repository not found. fatal: repository 'https://github.com/Homebrew/homebrew-boneyard/' not foundSacker
I would edit this to add the quick "official" solution and a better slightly more involved process that is more durable, but it might not get accepted. See https://mcmap.net/q/40449/-homebrew-install-specific-version-of-formula for the process.Primogeniture
The github.com/Homebrew/homebrew-versions has been removed.Maya
This answer appears largely defunct. Without the versions repository, is there no way to download older versions?Ligni
This may once have been a good answer, but "versions" is gone.Ionic
It's 2020, and `brew versions' doesn't work anymore. "Unknown command: versions" is the error I get.Ozzie
They should hire you!Kendyl
It seems that brew switch is broken now. I get: Error: Calling `brew switch` is disabled! Use `brew link` @-versioned formulae instead.Kalk
almost not workingParbuckle
brew removed the switch command, see github.com/Homebrew/discussions/discussions/339Rojo
This was the only approach that worrked for me for istioctl cmichel.io/how-to-install-an-old-package-version-with-brewShylashylock
New brew versions have brew switch removed. I imagine that they will keep trying to kill any solution we find... However, I have a solution they probably can't break, I reimplemented brew switch as a zsh function that uses the gh cli to find the correct commit and then downloads and installs it using the fact that brew install can install local files, see my answer for the script: https://mcmap.net/q/40449/-homebrew-install-specific-version-of-formulaHandoff
S
856

Simple Workflow

Now that Homebrew/versions has been deprecated, Homebrew/core supports a few versions of formulae with a new naming format.

To install a specific version, e.g. postgresql 9.5 you simply run:

$ brew install [email protected]

To list the available versions run a search with @:

$ brew search postgresql@
==> Searching local taps...
[email protected][email protected]        [email protected]        [email protected]
Supporting answered 22/3, 2012 at 23:29 Comment(14)
Seems like the simplest, but when I copy and paste, get fatal: Not a git repository (or any of the parent directories): .git. I just cd into the directory instead (without the FORMULANAME.rb), then do the git checkout 120938Promptbook
you have to replace FORMULANAME.rb with the formulaname of the recipe.. did you try this?Supporting
@RamonTayag, I had the same problem. Be sure to cd /usr/local first.Mound
I Found you need a step 3.5: brew unlink FORMULANAMEWendell
I'm trying this on a clean (fresh osx install) system and the formulas checkout fine, but a brew install refuses to use the checked-out version and insists on installing the latest version. When trying to install another version after the latest is installed I get: Warning: <formula> already installedNates
If you have a formula from a tapped repo be sure to cd into that repo, e.g. cd /usr/local/Library/Taps/homebrew-scienceLoch
if you get an error with 'brew versions' try 'brew tap homebrew/boneyard before'Impeccant
brew versions <formula> isn't supported anymore.Ludvig
@sava-chankov I cannot get [email protected] to work for me. It is keg only and never actually links to /usr/local/binTrembles
I love this tl;dr, it also works when I want to roll back from vim8.0 to vim7.4Summerwood
How do you get the list of available versions?Whitneywhitson
@dem7w2, you can create the symlinks with brew link --force [email protected]. If you already have a newer version installed and want the older version to be your default, you'll need an --overwrite in there too.Gilli
Not all Formulas support multiple versions. See my answer for such casesHand
brew switch postgresql 9.5.12 did not work. Only shows Error: postgresql not found in the Cellar. when I run brew install [email protected] it shows only Warning: [email protected] 9.5.12 is already installed. The solution was brew switch [email protected] 9.5.12Socialist
M
469

⚠ This answer won't work anymore after December 2020:
brew switch got disabled in HomeBrew 2.7.0 (deprecated in 2.6.0)


For versions of Homebrew before 2.7:

There's now a much easier way to install an older version of a formula that you'd previously installed. Simply use

brew switch [formula] [version]

For instance, I alternate regularly between Node.js 0.4.12 and 0.6.5:

brew switch node 0.4.12
brew switch node 0.6.5

Since brew switch just changes the symlinks, it's very fast. See further documentation on the Homebrew Wiki under External Commands.

Magnification answered 14/12, 2011 at 16:33 Comment(10)
I tried this, and it succeded with the message " 33 links created for /usr/local/Cellar/node/0.4.7 " , but when I run node afterwards, it fails with the message " -bash: /usr/local/bin/node: No such file or directory " . Any idea what couldlve gone wrong? Note: i downloaded 0.4.7 manually and put it in the cellar directory, because I dont know how to download anyhting but the latest node version using brew.Derogatory
@Souroush Hakami you can't just put it in the folder, you have to put the compiled version in the Cellar folder.Foible
I hope you do know there is something called 'nvm'[github.com/creationix/nvm]!Uneducated
I relies on the package version already being installed. If you are missing the version you will still need to apply the selected answer.Firestone
For node version mgmt in particular, nvm is a better solution, imho.Alvinalvina
@NicStrong Indeed, brew switch relies on the sought-after version being already installed. Unfortunately, though, the currently accepted answer is incredibly unhelpful, convoluted, and outdated. There are better answers below which are much more helpful.Fontaine
It seems that brew switch is broken now. I get: Error: Calling `brew switch` is disabled! Use `brew link` @-versioned formulae instead.Kalk
Indeed: Error: Unknown command: switchWilmington
As called out above brew switch is deprecated so not so obvious how to toggle back to, say, the latest version. I tried brew install <formula> to no avail - already installed. The following worked to at least get me back to the latest: $ brew unlink gdb followed by $ brew link gdb. This tore down the symbolic links to the current version and then re-established them against the latest version.Porphyry
I have reimplemented brew switch as a ZSH function you can add to your .zshrc, take a look at: https://mcmap.net/q/40449/-homebrew-install-specific-version-of-formulaHandoff
F
261

Update: 1/15/2015

  • Find the commit history of the desired software and version. e.g. I need to switch from docker version 1.4.1 to 1.3.3: https://github.com/Homebrew/homebrew-core/commits/master/Formula/docker.rb
  • View the file with this button: enter image description here
  • Click the Raw button: List item
  • Copy the URL (docker.rb url in this example) from address bar
  • brew install <url> (may have to brew unlink first, e.g. brew unlink docker)
  • brew switch docker 1.3.3
  • Switch back to docker 1.4.1 brew switch docker 1.4.1

From this gist

brew update
brew versions FORMULA
cd `brew --prefix`
git checkout HASH Library/Formula/FORMULA.rb  # use output of "brew versions"
brew install FORMULA
brew switch FORMULA VERSION
git checkout -- Library/Formula/FORMULA.rb    # reset formula

## Example: Using Subversion 1.6.17
#
# $ brew versions subversion
# 1.7.3    git checkout f8bf2f3 /usr/local/Library/Formula/subversion.rb
# 1.7.2    git checkout d89bf83 /usr/local/Library/Formula/subversion.rb
# 1.6.17   git checkout 6e2d550 /usr/local/Library/Formula/subversion.rb
# 1.6.16   git checkout 83ed494 /usr/local/Library/Formula/subversion.rb
# 1.6.15   git checkout 809a18a /usr/local/Library/Formula/subversion.rb
# 1.6.13   git checkout 7871a99 /usr/local/Library/Formula/subversion.rb
# 1.6.12   git checkout c99b3ac /usr/local/Library/Formula/subversion.rb
# 1.6.6    git checkout 8774131 /usr/local/Library/Formula/subversion.rb
# 1.6.5    git checkout a82e823 /usr/local/Library/Formula/subversion.rb
# 1.6.3    git checkout 6b6d369 /usr/local/Library/Formula/subversion.rb
# $ cd `brew --prefix`
# $ git checkout 6e2d550 /usr/local/Library/Formula/subversion.rb
# $ brew install subversion
# $ brew switch subversion 1.6.17
# $ git checkout -- Library/Formula/subversion.rb
Fairlie answered 19/7, 2013 at 23:42 Comment(14)
This is the only thing that worked for me. Pretty silly that a package manager requires so much futzing around just to install a prior version. Perhaps I'm spoiled by ruby RVM and bundler, but this process really needs to be streamlined and abstracted.Luau
When I tried to go to a previous version of Ansible, I had to unlink the formula before installing the previous version as indicated. So before doing brew install <url>, brew unlink <formulaname>. Otherwise, this is the simplest and most concise way (so far) to switch to a specific version of a formula.Ha
You hit the Raw button on what file?!Cherokee
This approach seems to have issues as well.. I get an error Calling Formula.sha1 is disabled! for the particular formula I am trying to installCurvet
I tried a bunch of different approaches and this was the only one that worked for me. I was able to install PostgreSQL 9.5.5 (an outdated version).Leviticus
Works perfectly (July 2017). I needed to uninstall first before installing the raw repo file.Tidbit
Still best method (October 2017)Gerlach
It works, but occasionally you have to download the raw .rb file and change the checksum to avoid Homebrew complaining about SHA mismatch. GitHub seems to have a large portion of repo trees rearchived sometime in 2017, and their checksums changed. (Actually it caused much confusion in my team when Bazel began to fail on hash mismatches last July)Incident
I found the address easily by brew log <formula>Socialist
Thanks everyone, the only approach that worked for me tooOleta
This was a lot of extra work, but the only thing that was able to get webpack 3.11.0 on linuxbrew.Antiquate
I have put together a webpage for quickly looking up historic versions of each package: bagonyi.github.io/brewedFascinate
This doesn't work anymore. discourse.brew.sh/t/…Sporocyst
This still works if you just download the <formula>.rb file, and then use brew install ./<formula>.rb. Also, I have made a script to automatically download the correct .rb file and install it, see my answer https://mcmap.net/q/40449/-homebrew-install-specific-version-of-formulaHandoff
T
174

Along the lines of @halfcube's suggestion, this works really well:

  1. Find the library you're looking for at https://github.com/Homebrew/homebrew-core/tree/master/Formula
  2. Click it: https://github.com/Homebrew/homebrew-core/blob/master/Formula/postgresql.rb
  3. Click the "history" button to look at old commits: https://github.com/Homebrew/homebrew-core/commits/master/Formula/postgresql.rb
  4. Click the one you want: "postgresql: update version to 8.4.4", https://github.com/Homebrew/homebrew-core/blob/8cf29889111b44fd797c01db3cf406b0b14e858c/Formula/postgresql.rb
  5. Click the "raw" link: https://raw.githubusercontent.com/Homebrew/homebrew-core/8cf29889111b44fd797c01db3cf406b0b14e858c/Formula/postgresql.rb
  6. Download the .rb file to any folder (in this case: postgresql.rb)
  7. In that folder: brew install postgresql.rb
Timbale answered 16/10, 2011 at 22:12 Comment(17)
This is the easiest way to install needed version of a package if it's unavailable in core repo as package@versionAnticipant
If you can't use the web interface, you can clone the repo and do it locally: use git log master -- Formula/PACKAGENAME.rb to get the commit history, check out the correct commit, and then run brew install Formula/PACKAGENAME.rb to install it.Warpath
I think you need brew unlink before brew install of other version.Whitneywhitson
Who's this mxcl?Firecracker
I do this brew install https://github.com/Homebrew/homebrew-core/raw/afc10f17af8023906b87363a8340aeb62284329f/Formula/postgresql%409.6.rb but get the error, probably due to @ sign: Error: No available formula with the name "postgresql%409.6" In formula file: /Users/maciek/Library/Caches/Homebrew/Formula/postgresql%409.6.rb Expected to find class Postgresql%4096, but only found: PostgresqlAT96. Any ideas?Superfetation
Can someone explain after use git log master -- Formula/PACKAGENAME.rb to get the commit history, how to get the "raw" link?Partake
This flow breaks if GitHub shows a "Sorry, this commit history is taking too long to generate." when clicking "History" for a particular formula. I had to clone homebrew-core locally and run $ git log -- Formula/foo.rb instead.Predial
Unfortunately, this does not seem to work now (Homebrew 2.4.11). In the middle of install per the answer, brew figures out that a newer version is available and decides to uprgade. For example: [skipped] Warning: midnight-commander 4.8.25 is available and more recent than version 4.8.23. [skipped] ==> Upgrading 2 dependents: midnight-commander 4.8.23 -> 4.8.25, midnight-commander 4.8.23 -> 4.8.25 ==> Upgrading midnight-commander 4.8.23 -> 4.8.25Slavey
Installing from a URL was once deprecated, and is now impossible, because it's insecure.Ionic
Doesn't work anymore Invalid usage: Installation of carthage from a GitHub commit URL is unsupported! brew extract carthage` to a stable tap on GitHub instead.`Ample
If you can't install from a URL you can download the ruby file first and install from that.Attenuant
Looks like this doesn't work anymore Invalid usage: Installation of kubernetes-cli from a GitHub commit URL is unsupported! brew extract kubernetes-cli` to a stable tap on GitHub instead.`Dolphin
Again, just download the raw .rb file and brew install <file>.Bridegroom
has error now: undefined method `cellar' for #<BottleSpecification:0x0000000150bdbef0> Homebrew 4.1.11Josefinejoseito
It works, thank you, i installed an old version of docker using this workaroundBlowup
This still works if you just download the <formula>.rb file, and then use brew install ./<formula>.rb. Also, I have made a script to automatically download the correct .rb file and install it, see my answer https://mcmap.net/q/40449/-homebrew-install-specific-version-of-formulaHandoff
I needed to use brew install --formula ./<formula>.rb to avoid getting "Error: Failed to load cask: ./<formula>.rb Cask '<formula>' is unreadable: wrong constant name #<Class:0x0000000120a25500>" errors.Woodchopper
R
130

Solution

brew extract --version=8.4p1  openssh homebrew/cask
brew install [email protected]

The newest [email protected] has bugs after I run brew upgrade, so I backed to the previous version successfully via the above way.

Richia answered 3/3, 2021 at 13:53 Comment(9)
This work for me, 2021, September. I did brew unlink before.Hugh
How do I see available versions?Surefooted
Why use homebrew/cask? Isn't that meant for github.com/Homebrew/homebrew-cask ?Lonnielonny
To @Sajjon's point, only missing steps here are brew unlink <package> then brew link <[email protected]>Gallicanism
I'm doing this, but for the WezTerm cask. However, Homebrew complains: $ brew extract --version="20230326-111934,3666303c" wezterm homebrew/cask ==> Searching repository history Error: Could not find wezterm! The formula or version may not have existed.Barring
@Lonnielonny The above command actually copies in the tap homebrew/cask. You can copy it in any tap. By default it searches in homebrew/coreGuimond
it can not extract to homebrew/cask and homebrew/core now, but it can still extract to homebrew/cask-versionsChristyna
People pointing us here in 2023 as this being the 'correct' answer and it doesn't work either. Sort of glad I put of using Brew up until now. This is terrible DXMclaurin
Totally agree, it's crazy how bad this is. I cannot work because a package uses a dependency that's broken. We need a revolution with this kind of stuff...Ergener
A
112

I've discovered a better alternative solution then the other complex solutions.

brew install https://raw.github.com/Homebrew/homebrew-versions/master/postgresql8.rb

This will download and install PostgreSQL 8.4.8


I found this solution by starting to follow the steps of searching the repo and a comment in the repo .

After a little research found that someone has a collection of rare formulars to brew up with.


If your looking for MySQL 5.1.x, give this a try.

brew install https://raw.github.com/Homebrew/homebrew-versions/master/mysql51.rb
Antrorse answered 19/9, 2011 at 11:55 Comment(10)
hmm, brew install https://github.com/Homebrew/homebrew-versions/blob/master/node06.rb seemed to fail with syntax errors.Genista
@BrianArmstrong You're referring to an html-formatted version of the file, while you need a raw version.Christy
Go to github.com/Homebrew/homebrew/commits/master/Library/Formula/…, pick your version, browse code, raw - use this url.Clambake
Don't forget the rawin the URLMirabella
Only seems to work for major versions? ex: I can downgrade from subversion 1.9.4 to 1.8.x, but not 1.9.3Barret
This is the best way to do this. Now you have to go here github.com/Homebrew/homebrew-core to access the brew github, then go find the version that you want to install. You can go back through old versions. Then choose the "raw" URL for the file you want and install that with "brew install".Embroidery
homebrew-versions is gone and everything's on core now, so the general case is: brew install https://raw.github.com/Homebrew/homebrew-core/<COMMIT>/Formula/<FORMULA>.rb. COMMIT can be found by going to github.com/Homebrew/homebrew-core/commits/master/Formula/….Firecracker
For older MySql, I used this command brew install https://raw.githubusercontent.com/Homebrew/homebrew-core/master/Formula/[email protected]Tarter
Seems it works but is deprecated so will be gone one day soon Warning: Calling Installation of sshuttle from a GitHub commit URL is deprecated! Use 'brew extract sshuttle' to stable tap on GitHub instead.Apace
Installing from a URL was once deprecated, and is now impossible, because it's insecure.Ionic
U
88

UPDATE: This method is deprecated and no longer works.

This method results in error: Installation of mysql from a GitHub commit URL is unsupported! brew extract mysql to a stable tap on GitHub instead. (UsageError)

$ brew install https://raw.githubusercontent.com/Homebrew/homebrew-core/c77882756a832ac1d87e7396c114158e5619016c/Formula/mysql.rb
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 2 taps (homebrew/core and homebrew/cask).

...

Traceback (most recent call last):
    9: from /usr/local/Homebrew/Library/Homebrew/brew.rb:122:in `<main>'
    8: from /usr/local/Homebrew/Library/Homebrew/cmd/install.rb:132:in `install'
    7: from /usr/local/Homebrew/Library/Homebrew/cli/parser.rb:302:in `parse'
    6: from /usr/local/Homebrew/Library/Homebrew/cli/parser.rb:651:in `formulae'
    5: from /usr/local/Homebrew/Library/Homebrew/cli/parser.rb:651:in `map'
    4: from /usr/local/Homebrew/Library/Homebrew/cli/parser.rb:655:in `block in formulae'
    3: from /usr/local/Homebrew/Library/Homebrew/formulary.rb:351:in `factory'
    2: from /usr/local/Homebrew/Library/Homebrew/formulary.rb:138:in `get_formula'
    1: from /usr/local/Homebrew/Library/Homebrew/formulary.rb:142:in `klass'
/usr/local/Homebrew/Library/Homebrew/formulary.rb:227:in `load_file': Invalid usage: Installation of mysql from a GitHub commit URL is unsupported! `brew extract mysql` to a stable tap on GitHub instead. (UsageError)
    12: from /usr/local/Homebrew/Library/Homebrew/brew.rb:155:in `<main>'
    11: from /usr/local/Homebrew/Library/Homebrew/brew.rb:157:in `rescue in <main>'
    10: from /usr/local/Homebrew/Library/Homebrew/help.rb:64:in `help'
     9: from /usr/local/Homebrew/Library/Homebrew/help.rb:83:in `command_help'
     8: from /usr/local/Homebrew/Library/Homebrew/help.rb:103:in `parser_help'
     7: from /usr/local/Homebrew/Library/Homebrew/cli/parser.rb:302:in `parse'
     6: from /usr/local/Homebrew/Library/Homebrew/cli/parser.rb:651:in `formulae'
     5: from /usr/local/Homebrew/Library/Homebrew/cli/parser.rb:651:in `map'
     4: from /usr/local/Homebrew/Library/Homebrew/cli/parser.rb:655:in `block in formulae'
     3: from /usr/local/Homebrew/Library/Homebrew/formulary.rb:351:in `factory'
     2: from /usr/local/Homebrew/Library/Homebrew/formulary.rb:138:in `get_formula'
     1: from /usr/local/Homebrew/Library/Homebrew/formulary.rb:142:in `klass'
/usr/local/Homebrew/Library/Homebrew/formulary.rb:227:in `load_file': Invalid usage: Installation of mysql from a GitHub commit URL is unsupported! `brew extract mysql` to a stable tap on GitHub instead. (UsageError)

I tried to install with the recommended command, but it doesn't work in this particular instance of MySQL 5.7.10. You may have better luck with a more recent Formula.

$ brew extract --version=5.7.10 mysql homebrew/cask
==> Searching repository history
==> Writing formula for mysql from revision 0fa511b to:
/usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask/Formula/[email protected]

$ 

$ brew install /usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask/Formula/[email protected]
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/core).
==> Updated Formulae
Updated 1 formula.
Error: undefined method `core_tap?' for nil:NilClass

Error: Failed to load cask: /usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask/Formula/[email protected]
Cask '[email protected]' is unreadable: wrong constant name #<Class:0x00007f9b9498cad8>
Warning: Treating /usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask/Formula/[email protected] as a formula.
==> Installing [email protected] from homebrew/cask
==> Downloading https://homebrew.bintray.com/bottles/cmake-3.19.4.big_sur.bottle.tar.gz
==> Downloading from https://d29vzk4ow07wi7.cloudfront.net/278f2ad1caf664019ff7b4a7fc5493999c06adf503637447af13a617d45cf484?response-content-disposition=attachment%3Bfilenam
######################################################################## 100.0%
==> Downloading https://downloads.sourceforge.net/project/boost/boost/1.59.0/boost_1_59_0.tar.bz2
==> Downloading from https://phoenixnap.dl.sourceforge.net/project/boost/boost/1.59.0/boost_1_59_0.tar.bz2
######################################################################## 100.0%
==> Downloading https://cdn.mysql.com/Downloads/MySQL-5.7/mysql-5.7.10.tar.gz

curl: (22) The requested URL returned error: 404 Not Found
Error: Failed to download resource "[email protected]"
Download failed: https://cdn.mysql.com/Downloads/MySQL-5.7/mysql-5.7.10.tar.gz

You could modify the Formula at the path above (written in ruby) to attempt to achieve your desired result (e.g., an installation of MySQL 5.7.10 on a recent macOS version).


You can use the strategy of identifying the formula and a particular commit in the history of the formula that matches the version of the package you'd like to install.

  1. Go to https://github.com/Homebrew/homebrew-core

  2. Press t on your keyboard to activate the file finder.

  3. Identify a formula that looks most relevant, perhaps: Formula/mysql.rb, bringing you to a forumla file location: https://github.com/Homebrew/homebrew-core/blob/master/Formula/mysql.rb.

  4. Look at the revision history by clicking on the History button, which is located at https://github.com/Homebrew/homebrew-core/commits/master/Formula/mysql.rb. If you're interested in MySQL 5.7.10, you might want to click the latest revision prior to 5.7.11, which navigates to a GitHub commit:

https://github.com/Homebrew/homebrew-core/commit/c77882756a832ac1d87e7396c114158e5619016c#Formula/mysql.rb

NOTE: You may have to view the commit history in your console per GitHub's suggestion if the commit history does not load in your browser. Replace the commit SHA above in the URL if you're interested in seeing that commit on GitHub. Alternatively, skip to step 7, below.

  1. Click the "View" button to view the source for the mysql.rb file after the commit was applied.

  2. Then click the "Raw" button to view the raw source.

  3. Copy the URL. Alternatively, build the URL yourself with the mysql.rb file name to identify your formula and the particular version of that formula (identified by the commmit SHA in the URL below).

https://raw.githubusercontent.com/Homebrew/homebrew-core/c77882756a832ac1d87e7396c114158e5619016c/Formula/mysql.rb

  1. Install it with $ brew install [URL from step 7]

     $ brew install https://raw.githubusercontent.com/Homebrew/homebrew-core/c77882756a832ac1d87e7396c114158e5619016c/Formula/mysql.rb
    
Unders answered 24/2, 2016 at 5:38 Comment(8)
Unfortunately this is too far down in the list of answers but is the best solution. This is what I use, except instead of using braumeister I just go to the Homebrew GitHub page of the formula that I want (e.g. for gcc go to github.com/Homebrew/homebrew/blob/master/Library/Formula/gcc.rb) and then I go through the "History" of the file until I get to the version that I want.Falstaffian
This was definitely the best solution in my case - installing an older version of Watchman that did not require Xcode 8.1Harass
This is really the easiest way to install an older version. Great answerSpindlelegs
This worked for me but i had to brew unlink the software first.Pippy
This was the simplest solution for me. This should be higher up as the accepted answer is outdated.Judicator
This should be the best answer as it's easily adaptable for any bottle, Python 3.6.5 in my case. Thank you.Jeffery
I think if you simplified it to: find the raw URL with the sha of the package you want and run the brew install X command this would see a lot more up votes.Casas
Unfortunately this method was deprecated by Homebrew and no longer works. Calling Installation of XXX from a GitHub commit URL is disabled!Exterior
D
63

Most of the other answers are obsolete by now. Unfortunately Homebrew still doesn’t have a builtin way of installing an outdated version, unless that version exists as a separate formula (e.g. python@2, [email protected] …).

Luckily, for other formulas there’s a much easier way than the convoluted mess that used to be necessary. Here are the full instructions:

  1. Search for the correct version in the logs:

    brew log formula
    # Scroll down/up with j/k or the arrow keys
    # or use eg. /4\.4\.23 to search a specific version
    
    # This syntax only works on pre-2.0 Homebrew versions
    brew log --format=format:%H\ %s -F --grep=‹version› ‹formula›
    

    This will show a list of commit hashes. Take one that is appropriate (mostly it should be pretty obvious, and usually is the most recent (i.e. top) one.

  2. Find the URL at which the formula resides in the upstream repository:

    brew info ‹formula› | grep ^From:
    
  3. Fix the URL:

    1. Replace github.com with raw.githubusercontent.com
    2. Replace blob/master with the commit hash we found in the first step.
  4. Install the desired version by replacing master in the previously found URL by the commit hash, e.g.:

    brew install https://raw.githubusercontent.com/Homebrew/homebrew-core/‹hash›/Formula/‹formula›.rb
    

(The last step may necessitate running brew unlink ‹formula› before.)


If you have copied a commit hash you want to use, you can use something like this example to install that version, replacing the value and bash with your commit hash and your desired formula.

BREW_VERSION_SHA=32353d2286f850fd965e0a48bcf692b83a6e9a41
BREW_FORMULA_NAME=bash
brew info $BREW_FORMULA_NAME \
| sed -n \
    -e '/^From: /s///' \
    -e 's/github.com/raw.githubusercontent.com/' \
    -e 's%blob/%%' \
    -e "s/master/$BREW_VERSION_SHA/p" \
| xargs brew install

This example is installing bash 4.4.23 instead of bash 5, though if you performed a brew upgrade afterward then bash 5 would get installed over top, unless you first executed brew pin bash. Instead to make this smoother WITHOUT pinning, you should first install the latest with brew install bash, then brew unlink bash, then install the older version you want per the script above, and then use brew switch bash 4.4.23 to set up the symlinks to the older version. Now a brew upgrade shouldn't affect your version of Bash. You can brew switch bash to get a list of the versions available to switch to.


Alternative using a custom local-only tap

Another way of achieving the same goal appears to be:

brew tap-new username/repo-name
# extract with a version seems to run a grep under the hood
brew extract --version='4.4.23' bash username/repo-name
brew install [email protected]
# Note this "fails" when trying to grab a bottle for the package and seems to have
# some odd doubling of the version in that output, but this isn't fatal.

This creates a formula@version in your custom tap that you can install per the above example. The downside is that you probably still need to brew unlink bash and then brew link [email protected] in order to use your specific version of Bash or any other formula.

Diphthong answered 13/12, 2018 at 16:18 Comment(7)
They apparently changed the syntax for brew log so any extra -F or --grep options were failing for me.Primogeniture
@Primogeniture Bloody hell, Homebrew 2 completely broke brew log. I can’t be bothered to write a new version now because that requires properly parsing the git log message, or alternatively running git log directly on the corresponding repository. And the changelog doesn’t seem to even mention this breaking change. Beyond annoying.Diphthong
Unless they accept that the change in behavior is a bug it seems like an External Command might be required to operate within the structure of brew to get that type of git log filtering back. docs.brew.sh/External-CommandsPrimogeniture
This was the only place I was able to find the correct brew extract usage, thanks!Callen
I would suggest making the custom local0only tap the first part of your answer. It really seems like the best solution overall.Whitneywhitson
I need to come back and update this answer with some potential fixes for certain older formulas if they were written before the sha1 checksums were deprecated and became invalid in Formulas. Basically you need to brew edit [email protected] and then find/replace sha1 with sha256, and then try to brew install [email protected] and when it errors out, replace the checksum that shows up as "expected" (the sha1) with the one that shows up as "received" (the sha256).Primogeniture
Per the above comment, one thing to keep in mind is that if you are installing any (much) older Formula, the Homebrew API/methods may have changed since that time so you should brew edit appFormula against the current version and compare to the brew edit [email protected] if you encounter any errors trying to brew install [email protected] after the brew extract command in the answer above.Primogeniture
P
47

Official method ( judging from the response to https://github.com/Homebrew/brew/issues/6028 )

Unfortunately Homebrew still doesn’t have an obvious builtin way of installing an older version.

Luckily, for most formulas there’s a much easier way than the convoluted mess that used to be necessary. Here are the full instructions using bash as an example:

brew tap-new $USER/local-tap
# extract with a version seems to run a `git log --grep` under the hood
brew extract --version=4.4.23 bash $USER/local-tap
# Install your new version from the tap
brew install [email protected]
# Note this "fails" trying to grab a bottle for the package and seems to have
# some odd doubling of the version in that output, but this isn't fatal.

This creates the formula@version in your custom tap that you can install per the above example. An important note is that you probably need to brew unlink bash if you had previously installed the default/latest version of the formula and then brew link [email protected] in order to use your specific version of Bash (or any other formula where you have latest and an older version installed).

A potential downside to this method is you can't easily switch back and forth between the versions because according to brew it is a "different formula".

If you want to be able to use brew switch $FORMULA $VERSION you should use the next method.


Scripted Method (Recommended)

This example shows installing the older bash 4.4.23, a useful example since the bash formula currently installs bash 5.

  • First install the latest version of the formula with brew install bash
  • then brew unlink bash
  • then install the older version you want per the snippets below
  • finally use brew switch bash 4.4.23 to set up the symlinks to your version

If you performed a brew upgrade after installing an older version without installing the latest first, then the latest would get installed clobbering your older version, unless you first executed brew pin bash.

The steps here AVOID pinning because it is easy to forget about and you might pin to a version that becomes insecure in the future (see Shellshock/etc). With this setup a brew upgrade shouldn't affect your version of Bash and you can always run brew switch bash to get a list of the versions available to switch to.

Copy and paste and edit the export lines from the code snippet below to update with your desired version and formula name, then copy and paste the rest as-is and it will use those variables to do the magic.

# This search syntax works with newer Homebrew
export BREW_FORMULA_SEARCH_VERSION=4.4.23 BREW_FORMULA_NAME=bash
# This will print any/all commits that match the version and formula name
git -C $(brew --repo homebrew/core) log \
--format=format:%H\ %s -F --all-match \
--grep=$BREW_FORMULA_SEARCH_VERSION --grep=$BREW_FORMULA_NAME

When you are certain the version exists in the formula, you can use the below:

# Gets only the latest Git commit SHA for the script further down
export BREW_FORMULA_VERSION_SHA=$(git -C $(brew --repo homebrew/core) log \
 --format=format:%H\ %s -F --all-match \
--grep=$BREW_FORMULA_SEARCH_VERSION --grep=$BREW_FORMULA_NAME | \
head -1 | awk '{print $1}')

Once you have exported the commit hash you want to use, you can use this to install that version of the package.

brew info $BREW_FORMULA_NAME \
| sed -n \
    -e '/^From: /s///' \
    -e 's/github.com/raw.githubusercontent.com/' \
    -e 's%blob/%%' \
    -e "s/master/$BREW_FORMULA_VERSION_SHA/p" \
| xargs brew install

Follow the directions in the formula output to put it into your PATH or set it as your default shell.

Primogeniture answered 19/4, 2019 at 16:2 Comment(5)
This seems to be one of the best most up-to-date answers - thank you @dragon788! I installed "StackOverflow Power User" for chrome, to expand ALL comments, and search "brew extract" as I got the deprecation warning many others have mentioned. I see this is your most recent contribution on this question, so I'm assuming this is (at least one of) the most up-to-date answers.Idonna
Also, here's the official docs for brew extract docs.brew.sh/Manpage#extract-options-formula-tapIdonna
I do still come back to this myself on occasion in order to grab alternate versions of software that I may still need to compile because it is used somewhere in the large collection of microservices I help maintain. I'm glad others find it useful as well.Primogeniture
There is also the gotcha that if you go too far back they were using SHA1 instead of SHA256 or newer, and so you have to update the formula to handle any brew ABI/API changes as well. #3988183Primogeniture
This method no longer works properly because they changed brew extract to remove the links to bottles, so you will always end up building from source. I have made a general solution that works by searching the homebrew repo commits using the gh CLI, see: https://mcmap.net/q/40449/-homebrew-install-specific-version-of-formulaHandoff
M
37

Homebrew changed recently. Things that used to work do not work anymore. The easiest way I found to work (January 2021), was to:

  • Find the .rb file for my software (first go to Formulas, find the one I need and then click "History"; for CMake, this is at https://github.com/Homebrew/homebrew-core/commits/master/Formula/cmake.rb)
    • Pick the desired version among the revisions, e.g. 3.18.4, click three dots in the top right corner of the .rb file diff (...) and then click Raw. Copy the URL.
  • Unlink the old version brew unlink cmake
  • Installing directly from the git URL does not work anymore (brew install https://raw.githubusercontent.com/Homebrew/homebrew-core/2bf16397f163187ae5ac8be41ca7af25b5b2e2cc/Formula/cmake.rb will fail)
    • Instead, download it and install from a local file curl -O https://raw.githubusercontent.com/Homebrew/homebrew-core/2bf16397f163187ae5ac8be41ca7af25b5b2e2cc/Formula/cmake.rb && brew install ./cmake.rb

Voila! You can delete the downloaded .rb file now.

Makeyevka answered 28/1, 2021 at 23:58 Comment(6)
Sometime you have to think simple enough, Thanks btw :DPankey
I'm trying to get this to work for downloading CMake 3.19.2, I get an error that I can't post here in full details because of character limitations, but the jist is this: curl: (22) The requested URL returned error: 404 Error: Failed to download resource "cmake_bottle_manifest" I know the url is correct which is this, raw.githubusercontent.com/Homebrew/homebrew-core/…, what am I doing wrong? I even see the file in my directory where it downloaded it as well, so this is very confusingSoaring
Here is the exact command I used: curl -O raw.githubusercontent.com/Homebrew/homebrew-core/… && brew install ./cmake.rbSoaring
I posted my question in more detail here: #67782125Soaring
Still works by Jan 2023 :–) I just (re-)installed the last available Atom by: curl -O https://raw.githubusercontent.com/Homebrew/homebrew-cask/4afa5ab933c047a2ae70209fde95a922b1764479/Casks/atom.rb && brew reinstall --cask ./atom.rb Thanks so much!Spence
Because finding the correct .rb file to download in the commit history is hard, I made a basic script that uses the gh cli to automate it, see my answer: https://mcmap.net/q/40449/-homebrew-install-specific-version-of-formulaHandoff
S
29

I just used Homebrew to go back to Maven 2.2.1 since the simple brew install maven installed Maven 3.0.3.

First you have to leave the maven dir there so

$ brew unlink maven

Use the brew tap command

$ brew tap homebrew/versions
Cloning into '/usr/local/Library/Taps/homebrew-versions'...
remote: Counting objects: 590, done.
remote: Compressing objects: 100% (265/265), done.
remote: Total 590 (delta 362), reused 549 (delta 325)
Receiving objects: 100% (590/590), 117.49 KiB | 79 KiB/s, done.
Resolving deltas: 100% (362/362), done.
Tapped 50 formula

Now you can install the maven2 formula:

$ brew install maven2
==> Downloading http://www.apache.org/dist/maven/maven-2/2.2.1/binaries/apache-maven-2.2.1-bin.tar.gz
######################################################################## 100.0%
/usr/local/Cellar/maven2/2.2.1: 10 files, 3.1M, built in 6 seconds
$ mvn --version
Apache Maven 2.2.1 (r801777; 2009-08-06 12:16:01-0700)
Java version: 1.6.0_37
Java home: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
Default locale: en_US, platform encoding: MacRoman
OS name: "mac os x" version: "10.7.4" arch: "x86_64" Family: "mac" 

Edit: You can also just brew switch maven 2.2.1 to switch to a different version.

Edit: The Apache Maven project reorganized their repo. Updated this answer to account for this change.

Skep answered 12/2, 2012 at 20:35 Comment(2)
This was very helpful. Note, the apache foundation has reorganized the maven binaries into version (1,2,3) specific directories, so you will need to edit the maven.rb for 2.2.1 and perhaps others to fit the new structure. For example, the binary for 2.2.1 was located at: apache.org/dist/maven/maven-2/2.2.1/binaries/…. Note the "maven-2/2.2.1/" in the url.Insincerity
@CharlesForcey You are correct about the maven dist repo being reorganized. I will update the answer to use brew tap to use the older maven2 formula. Thanks for the comment.Skep
S
28

Upgraded Postgres by accident?

My case:

  • postgresql was upgraded from 11 to 12 accidentally (after running brew upgrade without arguments)
  • I want to keep Postgres 11.

Solution:

  1. Stop the DB:
brew services stop postgresql
  1. Install Postgres 11:
brew install postgresql@11
  1. Enable it:
brew link postgresql@11 --force
  1. (Optional) Rename DB data directory from postgres to postgres@11:
cd /usr/local/var
ls -lh
mv postgresql@11 postgresql@11-fresh-backup
mv postgres postgresql@11
  1. Start the DB:
brew services start postgresql@11

If you have any errors, check /usr/local/var/log/[email protected] (notice the @11).

Steerageway answered 10/1, 2020 at 17:7 Comment(3)
This only works for the special case of postgresql@11 which is explicitly kept around by Homebrew maintainers to help with datastore migrations (performed with the special command brew postgresql-upgrade-database). This answer is not useful for almost any other formula.Mouthwash
@AdamWróbel I've decided this it is fine to answer the original question about Postgres. brew postgresql-upgrade-database is, well, for upgrades. My answer is about using an older Postgres version. Simply switching binaries won't work.Steerageway
NodeJS users who are not using a version manager e.g. nvm, FYI I've had luck using this technique for older LTS releases. At least at time of writing v10 & v12 should be available, e.g. brew install node@12 formulae.brew.sh/formula/nodeSeoul
I
22

Based on the workflow described by @tschundeee and @Debilski’s update 1, I automated the procedure and added cleanup in this script.

Download it, put it in your path and brewv <formula_name> <wanted_version>. For the specific OP, it would be:

cd path/to/downloaded/script/
./brewv postgresql 8.4.4

:)

Inclusive answered 9/7, 2012 at 14:6 Comment(2)
just awesome. why isn't this in brew?Xenogenesis
Not found anymore :(Mohican
P
21

None of the other answers cover this adequately for 2022, so I'm adding these instructions that are adapted from a Homebrew maintainer's answer for a similar question for installing a specific Cask version: https://mcmap.net/q/40834/-install-a-previous-version-of-chromedriver-with-brew

Uninstall your formula

brew uninstall <formula>

Switch over to your clone of the homebrew-core repo and reset to a commit containing the version you want.

cd "$(brew --prefix)/Library/Taps/homebrew/homebrew-core/Formula"
git log <formula>.rb # examine recent commits for <formula>
git reset --hard <commit-sha> # reset clone to last known-good commit for <formula>

Checkout a new branch for your version so brew won't autorefresh master after each command, install your formula, and pin it.

git checkout -b <formula>-downgrade-<version>
brew install <formula>
brew pin <formula>

Clean up

git checkout master
brew update

When you're ready to upgrade your formula, you can brew unpin <formula> and upgrade as usual.

Plosive answered 5/8, 2022 at 18:51 Comment(3)
For me the step "brew install <formula>" for some reason kept updating the master, instead of created branch which resulted to latest version, not the one I wanted. "brew reinstall <formula>" worked though, even without a need to create a branch and uninstall firstSemidiurnal
To force homebrew to install from the downloaded git repo in Dec. 2023 follow the directions for Default Tap Coning: docs.brew.sh/Installation#default-tap-cloningEchinus
That directory no longer exists, and I can't find a formula file at all for the project I'm trying to roll back.Mclaurin
H
18

An updated answer since that adds to what @lance-pollard already posted as working answer.

How to Install specific version of a Formula (formula used in this example is terraform):

  1. Find your formula file, e.g: https://github.com/Homebrew/homebrew-core/blob/master/Formula/terraform.rb
  2. Get the commit version from github’s history with https://github.com/Homebrew/homebrew-core/commits/master/Formula/terraform.rb or git log master -- Formula/terraform.rb if you have cloned the repo locally.
  3. Get the raw git URL with the commit version of your formula: If the formula link in github.com is https://github.com/Homebrew/homebrew-core/blob/e4ca4d2c41d4c1412994f9f1cb14993be5b2c59a/Formula/terraform.rb, your raw URL will be: https://raw.githubusercontent.com/Homebrew/homebrew-core/e4ca4d2c41d4c1412994f9f1cb14993be5b2c59a/Formula/terraform.rb
  4. Install it with: brew install https://raw.githubusercontent.com/Homebrew/homebrew-core/e4ca4d2c41d4c1412994f9f1cb14993be5b2c59a/Formula/terraform.rb
Hand answered 19/12, 2017 at 20:45 Comment(1)
To get the raw git URL in step 3, you can also just hit the "Raw" button and copy the URL that the browser takes you to.Floorage
W
14

The other answers here are great, but if you need to install an older version of the package and ensure that the package name is modified, you'll need a different approach. This is important when using scripts (in my case, PHP build scripts) which use brew --prefix package_name to determine what directory to use for compilation.

If you are using brew extract a version is added to the end of the package name which will break the brew --prefix lookup.

Here's how to install an older package version while maintaining the original package name:

# uninstall the newer version of the package that you accidentally installed
brew uninstall --ignore-dependencies icu4c

# `extract` the version you'd like to install into a custom tap
brew tap-new $USER/local-tap
brew extract --version=68.2 icu4c $USER/local-tap

# jump into the new tap you created
cd $(brew --repository $USER/local-tap)/Formula

# rename the formula
mv [email protected] icu4c.rb

# change the name of the formula by removing "AT682" from the `class` definition
# the exact text you'll need to remove will be different
# depending on the version you extracted
nano icu4c.rb

# then, install this specific formula directly
brew install $(brew --repository $USER/local-tap)/Formula/icu4c.rb

I wrote more about this here.

Wigging answered 11/5, 2021 at 14:24 Comment(1)
Thank you very much, @Wigging , for sharing that!Vitus
S
12

On the newest version of homebrew (0.9.5 as of this writing) there will be a specific recipe for the version of the homebrew keg you want to install. Example:

$ brew search mongodb
mongodb    mongodb24  mongodb26

Then just do brew install mongodb26 like normal.

In the case that you had already installed the latest version, make sure to unlink the latest version and link the desired version: brew unlink mongodb && brew link mongodb26.

Sphinx answered 30/4, 2015 at 22:1 Comment(1)
While this may answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.Pyelography
K
11

The problem with homebrew/versions is that someone has to have that specific version of software listed in the repository for you to be able to use it. Also, since brew versions is no longer supported, another solution is required. For solutions that indicate using brew switch, this will only work if you haven't done a brew cleanup since the version needs to exist on your computer.

I had a problem with wanting to install a specific older version of docker-machine which wasn't listed in homebrew/versions. I solved this using the below, which should also work for any brew installed software. The example below will use docker-machine as the package I want to downgrade from version 0.5.0 to 0.4.1.

  1. Go to your homebrew Formula directory.
    You can determine this by running brew info [any package name]. For example, brew info docker-machine gives me a line that shows me a path - /usr/local/Cellar/docker-machine/0.5.0. This tells me that on my machine, homebrew is installed at /usr/localand my Formula directory is located by default at /usr/local/Library/Formula

  2. Locate the specific formula file (.rb) for your package. Since I want to downgrade docker-machine, I can see a docker-machine.rb file.

  3. Get the version history for this formula file . Enter git log docker-machine.rb. This will list out the complete commit history for this file. You will see output like this:

    ...more 

    commit 20c7abc13d2edd67c8c1d30c407bd5e31229cacc
    Author: BrewTestBot 
    Date:   Thu Nov 5 16:14:18 2015 +0000

        docker-machine: update 0.5.0 bottle.

    commit 8f615708184884e501bf5c16482c95eff6aea637
    Author: Vincent Lesierse 
    Date:   Tue Oct 27 22:25:30 2015 +0100

        docker-machine 0.5.0

        Updated docker-machine to 0.5.0

        Closes #45403.

        Signed-off-by: Dominyk Tiller 

    commit 5970e1af9b13dcbeffd281ae57c9ab90316ba423
    Author: BrewTestBot 
    Date:   Mon Sep 21 14:04:04 2015 +0100

        docker-machine: update 0.4.1 bottle.

    commit 18fcbd36d22fa0c19406d699308fafb44e4c8dcd
    Author: BrewTestBot 
    Date:   Sun Aug 16 09:05:56 2015 +0100

        docker-machine: update 0.4.1 bottle.

    ...more

The tricky part is to find the latest commit for the specific version you want. In the above, I can tell the latest 0.4.1 version was committed with this commit tag : commit 5970e1af9b13dcbeffd281ae57c9ab90316ba423. The commits above this point start using version 0.5.0 (git log entries are listed from latest to earliest date).

  1. Get a previous version of the formula file. Using the commit tag from step #3 (you can use the first 6 chars), you can get an older version of the formula file using the following:

    git checkout 5970e1 docker-machine.rb

  2. Uninstall your current package version. Just run the normal brew commands to uninstall the current version of your package.
    Ex. brew uninstall docker-machine

  3. Install the older package version Now, you can just run the normal brew install command and it will install the formula that you have checkout out. Ex. brew install docker-machine

You may need to re-link by using the brew link docker-machine if necessary.

If at any time you want to revert back to the latest version of a specific package, go into the Formula directory and issue the following commands on your formula file (.rb)

git reset HEAD docker-machine.rb
git checkout -- docker-machine.rb

Then you can brew uninstall docker-machine and brew install docker-machine to get the latest version and keep it that way going forward.

Knot answered 10/11, 2015 at 17:30 Comment(3)
Very thorough write up @rchawdry, thank you. BTW: to find the commit that matches the version you want quickly add the --grep parameter git log --grep 0.4.1 docker-machine.rb.Hypercatalectic
For me, the path was /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core/FormulaLilliamlillian
I wasn't able to find enough of version history for the cairo brew in this way. @sealocal's answer showed more, for some reason.Lilliamlillian
T
11

Edit: 2021, this answer is no longer functional due to the github install being deprecated. (Thanks Tim Smith for update).

Install an old brew package version (Flyway 4.2.0 example)

Find your local homebrew git dir or clone Homebrew/homebrew-core locally

cd /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core/

OR

git clone [email protected]:Homebrew/homebrew-core.git

List all available versions

git log master -- Formula/flyway.rb

Copy the commit ID for the version you want and install it directly

brew install https://raw.githubusercontent.com/Homebrew/homebrew-core/793abfa325531415184e1549836c982b39e89299/Formula/flyway.rb
Tidbit answered 24/5, 2018 at 16:12 Comment(4)
why not simply run brew install Formula/flyway.rb from homebrew-core directory? it worked for me without using raw.github.. addressSocialist
This may be the only answer that's functional as of 2020-06-21 (Homebrew 2.4.0-93-g634c457)Roussel
Just use brew log some_package is okAlexalexa
No longer functional (Homebrew 2.7.0). The brew install fails with a message that installation from a GitHub commit URL is unsupported.Liss
M
10

I've tried most of the solutions here and they are outdated. I had to combine some ideas from here with my own work. As a result I've created a script to help me do the heavy lifting which you can find here

Usage:

brewv.sh formula_name desired_version
Madonnamadora answered 20/2, 2019 at 15:38 Comment(0)
Q
7

Currently the old ways of installing specific formula versions have been deprecated. So it seems like we have to use brew edit [formula]. E.g. say we want to install an the 62.1 version of icu4c (needed e.g. for postgresql 10.5). Then you'd have to

> brew edit icu4c
# drops you to editor

Here you'd have to alter the url, version and sha256 (perhaps also mirror) to the corresponding 62.1 strings.

url "https://ssl.icu-project.org/files/icu4c/62.1/icu4c-62_1-src.tgz"
mirror "https://github.com/unicode-org/icu/releases/download/release-62-1/icu4c-62_1-src.tgz"
version "62.1"
sha256 "3dd9868d666350dda66a6e305eecde9d479fb70b30d5b55d78a1deffb97d5aa3"

then run brew reinstall icu4c to finally download the 62.1 version.

Quip answered 24/1, 2019 at 5:46 Comment(3)
how to get the sha256 ? in my case https://storage.googleapis.com/dart-archive/channels/stable/release/1.24.3/sdk/dartsdk-macos-x64-release.zipCoralyn
@Coralyn you can download the file and use the command sha256sum in your terminal.Subdeacon
If make this solution you might want to go back to the previous configuration. See: github.com/Homebrew/homebrew-core/tree/master/FormulaSubdeacon
G
6

it could be done very easy for last version of brew.

brew tap homebrew/versions
brew install subversion17 # for svn 1.7 branch instead of last available
brew install postgresql8  # for postgresql 8 (which you ask)
Gustavo answered 11/8, 2014 at 6:26 Comment(1)
... if somebody bothered to create a versioned recipe for that package.Tutuila
A
6

If you can't find your version with brew search <formula>, you can also try going over the commit logs for your formula to find the version you want:

here is an example for installing an older version of nginx via brew:

From there, we can install 1.6.3 with the sha and raw git url:

brew install https://raw.githubusercontent.com/Homebrew/homebrew/eba75b9a1a474b9fc4df30bd0a32637fa31ec049/Library/Formula/nginx.rb

Armillas answered 15/9, 2015 at 18:32 Comment(0)
C
6

None of these really worked for my case (Python), so I'll add my 2 cents:

cd `brew --prefix`
git log Library/Formula/python.rb

Output looks like this:

commit 9ff2d8ca791ed1bd149fb8be063db0ed6a67a6de
Author: Dominyk Tiller <[email protected]>
Date:   Thu Jun 30 17:42:18 2016 +0100

    python: clarify pour_bottle reason

commit cb3b29b824a264895434214e191d0d7ef4d51c85
Author: BrewTestBot <[email protected]>
Date:   Wed Jun 29 14:18:40 2016 +0100

    python: update 2.7.12 bottle.

commit 45bb1e220341894bbb7de6fd3f6df20987dc14f0
Author: Rakesh <[email protected]>
Date:   Wed Jun 29 10:02:26 2016 +0530

    python 2.7.12

    Closes #2452.

    Signed-off-by: Tim D. Smith <[email protected]>

commit cf5da0547cd261f79d69e7ff62fdfbd2c2d646e9
Author: BrewTestBot <[email protected]>
Date:   Fri Jun 17 20:14:36 2016 +0100

    python: update 2.7.11 bottle.

...

I want version 2.7.11 so my hash is cf5da0547cd261f79d69e7ff62fdfbd2c2d646e9 (or cf5da05 for short). Next, I check out that version and install the formula python:

git checkout cf5da05
brew install python

Finally, clean up:

git checkout master
Conk answered 5/7, 2016 at 21:18 Comment(5)
Life changes and this answer is most relevant. Do not forget about HOMEBREW_NO_AUTO_UPDATE and read github.com/Homebrew/brew/issues/1188Rosarosabel
The correct commands are cd brew --prefix/Homebrew and git log Library/Taps/homebrew/homebrew-core/Formula/python.rbCherokee
But even that doesn't work because /Library/Taps is ignored.Cherokee
Don't forget about HOMEBREW_NO_AUTO_UPDATE. Yes, don't forget about the thing you need until after you discover you need it. xDVice
How about this one? HOMEBREW_NO_INSTALL_CLEANUP isn't this something you need remember?Socialist
P
6

Here my simple answer for it - was really annoyed that there is no built in solution for that so I've built my own lazy "script". Feel free to contribute 😃👍🏻

# Please define variables
packageName=<packageName>
packageVersion=<packageVersion>

# Create a new tab
brew tap-new local/$packageName

# Extract into local tap
brew extract --version=$packageVersion $packageName local/$packageName

# Verify packages is present
brew search $packageName@

# Run brew install@version as usual
brew install local/$packageName/$packageName@$packageVersion

https://gist.github.com/ArgonQQ/cff4834dab6b254cc2140bb1454b47ef

Parol answered 5/5, 2021 at 10:44 Comment(0)
P
3

I created a tool to ease the process prescribed in this answer.

To find a package pkg with version a.b.c, run:

$ brew-install-specific [email protected]

This will list commits on the pkg homebrew formula that mention the given version along with their GitHub urls.

Matching versions:
1. pkg: update a.b.c bottle.
   https://github.com/Homebrew/homebrew-core/commit/<COMMIT-SHA>
2. pkg: release a.b.c-beta
   https://github.com/Homebrew/homebrew-core/commit/<COMMIT-SHA>
3. pkg a.b.c
   https://github.com/Homebrew/homebrew-core/commit/<COMMIT-SHA>

Select index: 

Verify the commit from the given URL, and enter the index of the selected commit.

Select index: 2
Run:
  brew install https://raw.githubusercontent.com/Homebrew/homebrew-core/<COMMIT-SHA>/Formula/pkg.rb

Copy and run the given command to install.

Parttime answered 6/5, 2020 at 8:21 Comment(0)
W
3

Closest to @Lance's answer but that didn't work for me as is, in 2023, using M1 macOS 13 and trying to install specific version of opencv@4.

Bottom-line is: you should override the content of your local opencv.rb file with that of the remote opencv.rb file found in brew's GitHub repo, that corresponds to the version of opencv you want to install.

Essentially, we want to update the url and sha256 in the .rb file for the package. But sha256 were not available from the opencv Releases Github page and creating sha256 locally throws a checksum error for me. But getting them from the .rb file from the commit history resolves this issue.

Breakdown: in 5 simple steps

  1. Uninstall the package first: brew uninstall opencv@4

  2. Find the .rb file for the correct version of the package you want to install

    1. Search under Homebrew > Homebrew-core > Formula on Github

    2. In the history of the opencv.rb file: https://github.com/Homebrew/homebrew-core/commits/master/Formula/opencv.rb

      • Select the commit you want corresponding to the desired version, say opencv: update 4.6.0_1 bottle. and View file.
      • Copy the contents of / Download the file
  3. Open the .rb file on your system: brew edit opencv@4

  4. Replace the contents of this file with the file you downloaded from Github

  5. Install the package again using the .rb file path: brew install /opt/homebrew/Library/Taps/homebrew/homebrew-core/Formula/opencv.rb

This worked perfectly for me, whereas, @Lance's answer using brew install gave an Error: Your Command Line Tools (CLT) does not support macOS 13.

None of the other answers worked for me either.

Woodwaxen answered 7/2, 2023 at 23:46 Comment(0)
P
1

Update on the Library/Formula/postgresql.rb line 8 to

http://ftp2.uk.postgresql.org/sites/ftp.postgresql.org/source/v8.4.6/postgresql-8.4.6.tar.bz2

And MD5 on line 9 to

fcc3daaf2292fa6bf1185ec45e512db6

Save and exit.

brew install postgres
initdb /usr/local/var/postgres

Now in this stage you might face the postgresql could not create shared memory segment error, to work around that update the /etc/sysctl.conf like this:

kern.sysv.shmall=65536
kern.sysv.shmmax=16777216

Try initdb /usr/local/var/postgres again, and it should run smooth.

To run postgresql on start

launchctl load -w /usr/local/Cellar/postgresql/8.4.6/org.postgresql.postgres.plist

Hope that helps :)

Parasynthesis answered 13/7, 2011 at 16:30 Comment(0)
V
1

I just copied an older release of elasticsearch into the /usr/local/Cellar/elasticsearch directory.

$ mkdir /usr/local/Cellar/elasticsearch/5.4.3/bin
$ cp elasticsearch /usr/local/Cellar/elasticsearch/5.4.3/bin
$ brew switch elasticsearch 5.4.3

That's it. Maybe it's useful for anyone.

Vengeful answered 8/9, 2017 at 11:53 Comment(1)
An ugly hack. But, yeah it worked for me as well, while I was trying to search & install an older version of consul! Cheers.Kuebbing
M
1

Here is how I downgrade KOPS (which does not support versioning)

# brew has a git repo on your localhost
cd /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core

git remote -v
origin  https://github.com/Homebrew/homebrew-core (fetch)
origin  https://github.com/Homebrew/homebrew-core (push)

# find the version of kops.rb you need
git log Formula/kops.rb

# checkout old commit
# kops: update 1.18.1 bottle.
git checkout 2f0ede7f27dfc074d5b5493894f3468f27cc73f0 -- Formula/kops.rb

brew unlink kops
brew install kops

# now we have old version installed
ls -1 /usr/local/Cellar/kops/
1.18.1
1.18.2

which kops
/usr/local/bin/kops
ls -l /usr/local/bin/kops
/usr/local/bin/kops -> ../Cellar/kops/1.18.1/bin/kops
kops version
Version 1.18.1

# revert to the newest version
brew uninstall kops
git checkout -f
brew link kops
kops version
Version 1.18.2
Miyamoto answered 25/1, 2021 at 12:10 Comment(0)
H
1

When I tried to use all the other methods, they were no longer working, as it seems Homebrew continues to wage a war against their own tool.

So I have made a brew-switch function that you can add to your ~/.zshrc that will download any version of any core formula (see the discussion on the Homebrew GitHub).

It works by:

  1. searching commits in the Homebrew/homebrew-core repo for the most recent one that contains the name of your formula and the requested version
  2. downloading the <formula>.rb file from that commit
  3. using brew install ./<formula>.rb to install the file locally
  4. running brew pin <formula> to stop brew upgrade from messing with it

Usage

The general usage is:

brew-switch <formula> <version>

For example, to install version 2.8.6 of the argocd formula:

brew-switch argocd 2.8.6

Install

1. Dependencies

# install github command line
brew install gh

# authenticate with your github account
gh auth login

2. Add brew-switch to .zshrc

Open your ~/.zshrc file, and add the brew-switch function.

For example, if you like vim:

## don't forget to close and reopen your terminal after changing
vim ~/.zshrc

Add these lines to the end of the file:

# a script to install a specific version of a formula from homebrew-core
# USAGE: brew-switch <formula> <version>
function brew-switch {
  local _formula=$1
  local _version=$2

  # fail for missing arguments
  if [[ -z "$_formula" || -z "$_version" ]]; then
    echo "USAGE: brew-switch <formula> <version>"
    return 1
  fi

  # ensure 'gh' is installed
  if [[ -z "$(command -v gh)" ]]; then
    echo ">>> ERROR: 'gh' must be installed to run this script"
    return 1
  fi

  # find the newest commit for the given formula and version
  local _commit_sha=$(
    gh search commits \
      --owner Homebrew \
      --repo homebrew-core \
      --limit 1 \
      --sort committer-date \
      --order desc \
      --json sha \
      --jq '.[0].sha' \
      "\"${_formula}\" \"${_version}\""
  )

  # fail if no commit was found
  if [[ -z "$_commit_sha" ]]; then
    echo "ERROR: No commit found for ${_formula}@${_version}"
    return 1
  else
    echo "INFO: Found commit ${_commit_sha} for ${_formula}@${_version}"
  fi

  # download the formula file from the commit
  local _raw_url="https://raw.githubusercontent.com/Homebrew/homebrew-core/${_commit_sha}/Formula/${_formula:0:1}/${_formula}.rb"
  local _formula_path="/tmp/${_formula}.rb"
  echo "INFO: Downloading ${_raw_url}"
  if ! curl -fL "$_raw_url" -o "$_formula_path"; then
    echo ""
    echo "WARNING: Download failed, trying old formula path"

    # try the old formula path
    _raw_url="https://raw.githubusercontent.com/Homebrew/homebrew-core/${_commit_sha}/Formula/${_formula}.rb"
    echo "INFO: Downloading ${_raw_url}"
    curl -fL "$_raw_url" -o "$_formula_path" || (echo "ERROR: Failed to download ${_raw_url}" && return 1)
  fi

  # if the formula is already installed, uninstall it
  if brew ls --versions "$_formula" >/dev/null; then
    echo ""
    echo "WARNING: '$_formula' already installed, do you want to uninstall it? [y/N]"
    local _reply=$(bash -c "read -n 1 -r && echo \$REPLY")
    echo ""
    if [[ $_reply =~ ^[Yy]$ ]]; then
      echo "INFO: Uninstalling '$_formula'"
      brew unpin "$_formula"
      brew uninstall "$_formula" || (echo "ERROR: Failed to uninstall '$_formula'" && return 1)
    else
      echo "ERROR: '$_formula' is already installed, aborting"
      return 1
    fi
  fi

  # install the downloaded formula
  echo "INFO: Installing ${_formula}@${_version} from local file: $_formula_path"
  brew install --formula "$_formula_path"
  brew pin "$_formula"
}
Handoff answered 30/11, 2023 at 0:7 Comment(0)
Y
0

I decided, against my better judgment, to create a formula for Maven 3.1.1, which homebrew/versions did not have. To do this:

  1. I forked homebrew/versions on github.
  2. I symlinked from $(brew --prefix)/Library/Taps to the local working copy of my fork. I'll call this my-homebrew/versions.
  3. I tested by specifying the formula as my-homebrew/versions/<formula>.
  4. I sent a pull request to homebrew/versions for my new formula.

Yay.

Yabber answered 2/11, 2013 at 13:30 Comment(0)
H
0

For versions not currently in the default brew formulas, you can easily create your own tap with the tool from https://github.com/buildtools-version-taps/homebrew-versions-tap-tool

Hilmahilt answered 15/7, 2017 at 17:18 Comment(1)
This tool appears to be integrated into Homebrew now? You can brew tap-new user/repo and then brew extract --version='Major.minor' FORMULA user/repo and then brew install [email protected].Primogeniture
R
0

brew versions and brew install https://raw.githubusercontent.com/Homebrew/homebrew-core/<COMMIT-HASH>/Formula/<Formula>.rb not supported now.

You can try like this:

$ brew extract --version 5.6.2 gradle vitahlin/core
$ brew install [email protected]
Ritualist answered 6/11, 2020 at 10:56 Comment(0)
C
-1

Building on top of the existing answers, I wrote an utility in my dotfiles repository that makes this job really easy:

$ chezmoi --version
chezmoi version v2.40.0, commit 6a8ca1634654734bb33a036ffb9c21e6b9f4d28d, built at 2023-09-19T08:14:08Z, built by Homebrew

$ brew-install-version chezmoi 2.39.0
👉 brew update --force
Updated 1 tap (homebrew/core).
👉 git -C /home/linuxbrew/.linuxbrew/Homebrew/Library/Taps/homebrew/homebrew-core log --grep 2.39.0 -1 --pretty=format:%H -- Formula/c/chezmoi.rb
👉 git -C /home/linuxbrew/.linuxbrew/Homebrew/Library/Taps/homebrew/homebrew-core reset --hard 208563b7cd7ffeb9ed0bf8a44334eb27f6c961c4
HEAD is now at 208563b7cd7 chezmoi: update 2.39.0 bottle.
👉 brew uninstall --force chezmoi
Uninstalling chezmoi... (8 files, 32.8MB)
👉 exec brew install --force chezmoi
==> Downloading https://ghcr.io/v2/homebrew/core/chezmoi/manifests/2.39.0
Already downloaded: /home/felipecrs/.cache/Homebrew/downloads/7eecd64c25af73f74f6a4e69674f6cfc1f9a7d21820bd3281f9ab477a31ede6c--chezmoi-2.39.0.bottle_manifest.json
==> Fetching chezmoi
==> Downloading https://ghcr.io/v2/homebrew/core/chezmoi/blobs/sha256:4df65f4d11f6ff0a13c67b522f77a2eaf7caed6fa00de6e9929ac5c86c9b7064
########################################################################################################################################## 100.0%
==> Pouring chezmoi--2.39.0.x86_64_linux.bottle.tar.gz
==> Caveats
zsh completions have been installed to:
  /home/linuxbrew/.linuxbrew/share/zsh/site-functions
==> Summary
🍺  /home/linuxbrew/.linuxbrew/Cellar/chezmoi/2.39.0: 8 files, 32.8MB
==> Running `brew cleanup chezmoi`...

$ chezmoi --version
chezmoi version v2.39.0, commit 153ff486c16ac654298225a15d8131201fe7c154, built at 2023-09-03T19:27:58Z, built by Homebrew

Note that the script is not ready for distribution without actually installing my dotfiles. But if someone has interest in using it standalone-ly, I can make it happen.

Consecrate answered 26/9, 2023 at 1:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.