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: 
`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`
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)