164

I have a few kegs of the same package in /usr/local/Cellar/libfoo like /usr/local/Cellar/libfoo/1.0.1, /usr/local/Cellar/libfoo/HEAD and /usr/local/Cellar/libfoo/mycopy

How can I brew link to a specific version?

Glen Selle
  • 3,867
  • 4
  • 33
  • 58
jrwren
  • 15,914
  • 5
  • 32
  • 52

8 Answers8

318

The usage info:

Usage: brew switch <formula> <version>

Example:

brew switch mysql 5.5.29

You can find the versions installed on your system with info.

brew info mysql

And to see the available versions to install, you can provide a dud version number, as brew will helpfully respond with the available version numbers:

brew switch mysql 0

Update (15.10.2014):

The brew versions command has been removed from brew, but, if you do wish to use this command first run brew tap homebrew/boneyard.

The recommended way to install an old version is to install from the homebrew/versions repo as follows:

$ brew tap homebrew/versions
$ brew install mysql55

For detailed info on all the ways to install an older version of a formula read this answer.

Community
  • 1
  • 1
SimonW
  • 5,527
  • 3
  • 27
  • 35
  • 1
    I followed the 15.10.2014 Update method and needed to add /usr/local/Cellar/mysql55/5.5.40/bin to my path. Most Homebrew stuff has a symlink in /usr/local/bin to /usr/local/Cellar..., but the updated installation does not automatically add this symlink. – Powers Dec 08 '14 at 20:59
  • 1
    You still need to link the installed version and overwrite your current link by using: brew link --overwrite mysql55 – Yann VR Jul 01 '16 at 22:24
  • 2
    Unfortunately, `brew tap homebrew/versions` now gives me an error `Error: homebrew/versions was deprecated. This tap is now empty as all its formulae were migrated` ` – Juraj Martinka Jun 14 '19 at 11:57
  • Is there a way to figure out what versions are available without `versions` or just guessing-and-checking with `switch`? – Kyle Strand Jun 14 '19 at 19:52
  • 1
    versions is deprecated so I think the only way left is to find an old formula and install it from a direct URL – SimonW Jun 24 '19 at 06:17
11

I asked in #machomebrew and learned that you can switch between versions using brew switch.

$ brew switch libfoo mycopy 

to get version mycopy of libfoo.

jrwren
  • 15,914
  • 5
  • 32
  • 52
4

If you have installed, for example, php 5.4 it could be switched in the following way to php 5.5:

$ php --version
PHP 5.4.32 (cli) (built: Aug 26 2014 15:14:01) 
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2014 Zend Technologies

$ brew unlink php54

$ brew switch php55 5.5.16

$ php --version
PHP 5.5.16 (cli) (built: Sep  9 2014 14:27:18) 
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
FelikZ
  • 2,672
  • 3
  • 29
  • 40
4

brew switch libfoo mycopy

You can use brew switch to switch between versions of the same package, if it's installed as versioned subdirectories under Cellar/<packagename>/

This will list versions installed ( for example I had Cellar/sdl2/2.0.3, I've compiled into Cellar/sdl2/2.0.4)

brew info sdl2

Then to switch between them

brew switch sdl2 2.0.4
brew info 

Info now shows * next to the 2.0.4

To install under Cellar/<packagename>/<version> from source you can do for example

cd ~/somewhere/src/foo-2.0.4
./configure --prefix $(brew --Cellar)/foo/2.0.4
make

check where it gets installed with

make install -n

if all looks correct

make install

Then from cd $(brew --Cellar) do the switch between version.

I'm using brew version 0.9.5

stefanB
  • 69,149
  • 26
  • 113
  • 140
4

Sadly brew switch is deprecated in Homebrew 2.6.0 (December 2020)

$ brew switch
Error: Unknown command: switch

TLDR, to switch to package version 10:

brew unlink package
brew link package@10

To use another version of a package, for example node:

  • First, ensure that the specific version is installed using brew list. My package here is node (16) and node@14.
➜  ~ brew list
==> Formulae
node
node@14

➜  ~ node -v
v16.1.0
  • Unlink the current package: brew unlink node.
➜  ~ brew unlink node
Unlinking /usr/local/Cellar/node/16.1.0... 7 symlinks removed.
  • Link the correct version
➜  ~ brew link node@14
Linking /usr/local/Cellar/node@14/14.16.1_1... 3857 symlinks created.

If you need to have this software first in your PATH instead consider running:
  echo 'export PATH="/usr/local/opt/node@14/bin:$PATH"' >> ~/.zshrc
➜  ~ node -v
v14.16.1
jackblk
  • 504
  • 2
  • 9
2

In case brew switch produces an error (in this example trying to switch to node version 14):

> brew switch node 14
Error: Calling `brew switch` is disabled! Use `brew link` @-versioned formulae instead.

The correct way switching versions would be :

> brew link --overwrite node@14
Tomas Liubinas
  • 159
  • 2
  • 7
1

Homebrew removed brew switch subcommand in Homebrew 2.6.0. Get it back from here.

brew tap laggardkernel/tap
brew switch --help

name@version formula

There's mainly two ways to switch to an old version of an app.

If it's a bigger version change. Homebrew may have created a versioned package in the repo. Like go, go@1.10, they are two different formulae, installed into two different locations.

# install the old one
brew install go@1.10

# link the executable into /usr/local/bin, or /opt/homebrew/bin
brew link --overwrite --force go@1.10

brew switch

But not every package has a versioned variant. If you just upgraded to the new version and the old one is still in your system, skip step 1, 2.

  1. In this situation, search in the homebrew-core repo and download the specific formula. e.g. mysql 8.0.23
  2. Download the raw file, and install from it brew install /path/to/downloaded/mysql.rb.
  3. Now both the latest and the old 8.0.23 (same formula mysql) exist, switch (link out) the old version with brew switch mysql 8.0.23

brew info mysql will list all the old version still exist.

Step 1, 2 could be replaced by brew extract, but that requires user maintain its own tap. I won't cover it here, just search it if you're interested.

Simba
  • 11,459
  • 3
  • 37
  • 50
-2

if @simon's answer is not working in some of the mac's please follow the below process.

If you have already installed swiftgen using the following commands:

$ brew update $ brew install swiftgen

then follow the steps below in order to run swiftgen with older version.

Step 1: brew uninstall swiftgen Step 2: Navigate to: https://github.com/SwiftGen/SwiftGen/releases and download the swiftgen with version: swiftgen-4.2.0.zip.

Unzip the package in any of the directories.

Step 3: Execute the following in a terminal:

$ mkdir -p ~/dependencies/swiftgen
$ cp -R ~/<your_directory_name>/swiftgen-4.2.0/ ~/dependencies/swiftgen
$ cd /usr/local/bin
$ ln -s ~/dependencies/swiftgen/bin/swiftgen swiftgen
$ mkdir ~/Library/Application\ Support/SwiftGen
$ ln -s ~/dependencies/swiftgen/templates/ ~/Library/Application\ Support/SwiftGen/

$ swiftgen --version

You should get: SwiftGen v0.0 (Stencil v0.8.0, StencilSwiftKit v1.0.0, SwiftGenKit v1.0.1)

enter image description here

DILIP KOSURI
  • 355
  • 3
  • 7