Skip to content

Instantly share code, notes, and snippets.

@ericboehs
Last active September 8, 2016 01:58
Show Gist options
  • Save ericboehs/5329013 to your computer and use it in GitHub Desktop.
Save ericboehs/5329013 to your computer and use it in GitHub Desktop.

Using Homebrew versions in place of rbenv/rvm

Installation

For the git checkout commands we'll want to be in /usr/local. Let's go there now:

cd /usr/local

Remove old versions of ruby

brew rm -f ruby

Setup homebrew to find old formulas:

brew tap homebrew/versions; brew update

Run brew versions ruby and then git checkout the ruby formula we want. Here's a nice one liner to do it for you:

$(brew versions ruby | grep 1.9.3-p392 | cut -d\  -f2-)

Now with the old formula in place we can install ruby 1.9.3

brew install ruby

Now for each version of ruby we want to install, we need to brew unlink the formula, git checkout the version we want and brew install it:

brew unlink ruby
$(brew versions ruby | grep 2.0.0-p0 | cut -d\  -f2-)
brew install ruby

To finish the install you'll want to add $(cd $(which gem)/..;pwd) to your PATH in .zshenv or another sourced file such as .zshrc or, if you must, .bashrc. There may be a more elegant way to do this, but it works.

vim ~/.zshenv
source ~/.zshenv

Note: brew cleanup deletes old versions -- don't run it!

Switching versions

brew switch ruby 1.9.3-p392
source ~/.zshenv

brew switch ruby 2.0.0-p0
source ~/.zshenv

Note: You can run ruby -v && readlink $(which gem) to verify your ruby version and gem path.

Installing gems

Nothing much different here, just brew switch to the ruby version, source your env and gem install

gem install bundler foreman pg rails --no-ri --no-rdoc -f
readlink $(which bundle)

brew switch ruby 1.9.3-p392
source ~/.zshenv

gem install bundler foreman pg rails --no-ri --no-rdoc -f
readlink $(which bundle)

Alias Helper

If you want an easier way to switch versions, I hacked together this zsh function.

# ~/.zshrc
rbv () { brew switch ruby $(brew switch ruby list 2>&1 |tail -1|cut -d\  -f3-|tr -d ','|tr ' ' "\n" | grep "^$1" | head -1) && source ~/.zshenv }

Now you can just run rbv 1.9.3 and it will find the latest installed version of ruby 1.9.3 and switch to it. It runs source ~/.zshenv so make sure you have the dynamic gem path in your $PATH as I mention above in the installation section.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment