User Tools

Site Tools


mac:ruby

Ruby

Mac OS X 10.8 comes with Ruby 1.8.7 (patch level 358). This version has been retired and the developers will no longer be supporting it. Most Ruby programs now support Ruby 1.9.3 and/or Ruby 2.0.0.

There are several ways to support multiple versions of Ruby. The most popular are RVM and rbenv. We used to use RVM, but have switched to rbenv because it is focused and does its job with fewer tricks. Here's a list of reasons to prefer rbenv.

Installing rbenv

We install rbenv and ruby-build, which will integrate with rbenv to install the versions of Ruby we want. We also install several rbenv plugins. The rbenv-gem-rehash plugin automatically runs rbenv rehash whenever necessary. The rbenv-vars plugin allows Ruby programs to define environment variables. The rbenv-binstubs plugin integrates with RubyGems and Bundler to make sure that the right version of Ruby is used to run any programs that require Ruby.

brew update
brew install rbenv
brew install ruby-build
brew install rbenv-aliases
brew install rbenv-gem-rehash
brew install rbenv-vars
brew install rbenv-binstubs
 
# Make sure rbenv is initialized properly.
if ! grep 'rbenv' ~/.bashrc >/dev/null ; then
  echo >> ~/.bashrc ''
  echo >> ~/.bashrc '# rbenv Ruby environment manager'
  echo >> ~/.bashrc 'if which rbenv ; then eval "$(rbenv init -)" ; fi'
fi
eval "$(rbenv init -)"

Installing Ruby

First we'll install a couple libraries that can be used by all the Ruby versions.

brew install openssl
brew install libyaml

We'll build the latest patch levels of Ruby 1.9.3, 2.0.0, and 2.1.

rbenv install --list
rbenv install 2.1.1
rbenv install 2.0.0-p451
rbenv install 1.9.3-p545

We'll also install the latest Rubinius.

rbenv install rbx-2.2.5

We'll set aliases for the latest patch levels, so we don't have to specify the full versions number. (This is not a good idea for .ruby-version files, but is handy for manual switching.)

rbenv alias 1.9    1.9.3-p545
rbenv alias 1.9.3  1.9.3-p545
rbenv alias 2.0    2.0.0-p451
rbenv alias 2.0.0  2.0.0-p451
rbenv alias 2.1    2.1.1
rbenv alias rbx    rbx-2.2.5

Updating

To update the versions of Ruby you have installed, first you have to upgrade ruby-build:

brew update
brew upgrade ruby-build
rbenv install --list

Then you can install the new version(s) of Ruby. Be sure to install all the basic gems, update your aliases and documentation.

Setting the Default Ruby

We'll set Ruby 2.1.1 as the default.

rbenv versions
rbenv global 2.1.1
rbenv shell 2.1.1

Installing Gems

We only need to install gems that we use outside of applications. This will generally be executables written in Ruby, or things we want inside of IRB. For everything else, we'll use Bundler to install applications-specific gems.

First we configure RubyGems to not generate any local documentation:

[ -f ~/.gemrc ] || cat > ~/.gemrc <<EOF
---
:verbose: true
:update_sources: true
gem: --no-ri --no-rdoc
EOF

Then we install our gems for each version of Ruby (except the system version):

GEMS_TO_INSTALL='rake bundler awesome_print hirb wirble pry rails haml html2haml'
ALL_RUBY_VERSIONS='1.9 2.0 2.1 rbx'
CURRENT_RUBY_VERSION=$(rbenv version | cut -d' ' -f1)
for ruby_version in $ALL_RUBY_VERSIONS ; do
  rbenv shell $ruby_version
  gem install $GEMS_TO_INSTALL
done
rbenv shell $CURRENT_RUBY_VERSION

Binstubs

Binstubs are a bit tricky, but the general idea is to have a small executable in a directory in the front of the $PATH that will set up the environment, then call the real executable. For rbenv, the rbenv-binstubs plugin handles this for us, without having to add ./bin to the $PATH (which would be a security issue). It gets in front of the binstubs created by RubyGems and Bundler to ensure the proper version of Ruby is used.

To get this working, we need to tell Bundler to always create binstubs:

bundle config --global bin bin

NOTE: Rails 4 does not like Bundler's binstubs. You'll need to run rake rails:update:bin after running bundle install the first time, to override Bundler's binstubs with Rails' binstubs.

TODO

  • Try chruby instead. It's a lot lighter weight.
  • Install JRuby.
mac/ruby.txt · Last modified: 2014/03/31 21:33 by 99.100.133.164