Question: 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?

Answer: 
**[This is probably the best way as of 11.1.2022](https://stackoverflow.com/a/9832084/1574376):**

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

```
$ brew install postgresql@9.5

```

To list the available versions run a search with @:

```
$ brew search postgresql@
==> Formulae
postgresql                 postgresql@11              postgresql@13              postgresql@9.5             qt-postgresql
postgresql@10              postgresql@12              postgresql@9.4             postgresql@9.6             postgrest

==> Casks
navicat-for-postgresql

```

---

**DEPRECATED in Homebrew 2.6.0 (December 2020):**

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](https://stackoverflow.com/a/4158763/1574376).
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](https://i.stack.imgur.com/YKnK2.png)](https://i.stack.imgur.com/YKnK2.png)