Categories
homebrew installation versioning

Homebrew install specific version of formula?

2608

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

6

  • 2

    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 stackoverflow.com/a/55764594/3794873

    – dragon788

    Apr 19, 2019 at 16:04

  • 73

    I read all the answers. It’s 2020 and there is no easy way or official command to do that

    – oluckyman

    Apr 10, 2020 at 17:06


  • 4

    @oluckyman 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.

    – bfontaine

    Jul 1, 2020 at 12:13

  • 1

    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.

    Feb 8, 2021 at 3:40

  • 5

    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/66458452

    – kjyv

    Feb 17 at 14:22

2855

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

53

  • 19

    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.

    – mipadi

    Jan 3, 2011 at 20:59

  • 19

    Note: on a fresh Homebrew install, you may need to brew update to establish its Git repo.

    – Bluu

    May 28, 2011 at 0:57

  • 739

    This is ridiculously difficult.

    Jan 16, 2016 at 11:26

  • 40

    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.

    – Calin

    Feb 18, 2017 at 10:02

  • 26

    homebrew/versions is deprecated

    Sep 4, 2017 at 2:44

825

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 [email protected]
==> Searching local taps...
[email protected][email protected]        [email protected]        [email protected]

14

  • 8

    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 120938

    Jul 1, 2012 at 12:28


  • 8

    @RamonTayag, I had the same problem. Be sure to cd /usr/local first.

    – gjb

    Dec 19, 2012 at 11:22

  • 14

    I Found you need a step 3.5: brew unlink FORMULANAME

    Feb 24, 2013 at 2:47


  • 61

    brew versions <formula> isn’t supported anymore.

    – ejoubaud

    Nov 3, 2014 at 10:23

  • 9

    @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.

    – Ryan

    Aug 16, 2017 at 14:52

459

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


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.

8

  • 2

    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.

    Dec 22, 2011 at 21:36

  • 6

    I hope you do know there is something called ‘nvm'[github.com/creationix/nvm]!

    May 27, 2013 at 16:00

  • 51

    I relies on the package version already being installed. If you are missing the version you will still need to apply the selected answer.

    Dec 19, 2013 at 5:11

  • 7

    @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.

    – GDP2

    Dec 8, 2017 at 2:46

  • 11

    It seems that brew switch is broken now. I get: Error: Calling `brew switch` is disabled! Use `brew link` @-versioned formulae instead.

    Feb 3, 2021 at 20:03