Setting Up a Ruby Environment on Mac OS X 10.7

I recently picked up a new Mac mini, so I could have a stand-alone development system. One of the nice things about a fresh system is that it offers a clean slate on which to install development tools.

Ruby is one of the languages I use on a regular basis. Unfortunately, the proliferation of interpreters and gems can be hard to keep under control. One of my goals with the new system was to improve this process.

This posts documents the commands I executed, for future reference.

Mac OS X 10.7 Ruby Environment

Before we get started, let’s inspect the standard Ruby install that ships with Mac OS X 10.7.

$ which ruby
/usr/bin/ruby

$ ruby --version
ruby 1.8.7 (2010-01-10 patchlevel 249) [universal-darwin11.0]

$ which gem
/usr/bin/gem

$ gem --version
1.3.6

$ gem environment
RubyGems Environment:
- RUBYGEMS VERSION: 1.3.6
- RUBY VERSION: 1.8.7 (2010-01-10 patchlevel 249) [universal-darwin11.0]
- INSTALLATION DIRECTORY: /Library/Ruby/Gems/1.8
- RUBY EXECUTABLE: /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby
- EXECUTABLE DIRECTORY: /usr/bin
- RUBYGEMS PLATFORMS:
- ruby
- universal-darwin-11
- GEM PATHS:
- /Library/Ruby/Gems/1.8
- /Users/jaredhanson/.gem/ruby/1.8
- /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :benchmark => false
- :backtrace => false
- :bulk_threshold => 1000
- REMOTE SOURCES:
- http://rubygems.org/

Install RVM: Ruby Version Manager

RVM is a tool that allows us to work with multiple Ruby environments, including different interpreter versions and sets of gems.

Install it just as the instructions recommend:

$ bash < <(curl -s https://rvm.beginrescueend.com/install/rvm)

The installation script will output useful messages, explaining exactly what it is doing and offering hints about what to do after installation is complete. The one critical thing to do is source the rvm script, so that the command is available in the shell. I keep this in my dotfiles, so it is always available. You can also source it temporarily in the current shell by executing:

$ source "$HOME/.rvm/scripts/rvm"

Install Ruby

Now that RVM is installed, we can proceed to install local versions of Ruby. To minimize differences, I first install the same version of Ruby shipped with the OS. RVM makes it easy to install and switch to newer version later.

$ rvm install 1.8.7
$ rvm use 1.8.7

Now, the version of Ruby in use is completely isolated from the system.

$ which ruby
/Users/jaredhanson/.rvm/rubies/ruby-1.8.7-p352/bin/ruby
$ which gem
/Users/jaredhanson/.rvm/rubies/ruby-1.8.7-p352/bin/gem

Rake is my most utilized Ruby-based utility, so that is first on my list to install.

$ gem install rake
/Users/jaredhanson/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/1.8/timeout.rb:60: [BUG] Segmentation fault
ruby 1.8.7 (2011-06-30 patchlevel 352) [i686-darwin11.0.1]

Abort trap: 6

Damn.

Install Ruby with GCC 4.2

Mac OS X 10.7 is using an LLVM-based compiler by default, which is causing trouble. Luckily, a solution can be found on Stack Overflow.

RVM makes it easy to remove the broken installation of Ruby:

$ rvm remove 1.8.7

Installing Ruby 1.8.7 with plain (non-LLVM) gcc fixes the issue:

CC=/usr/bin/gcc-4.2 rvm install 1.8.7

Gems can now be installed successfully:

$ gem install rake
Fetching: rake-0.9.2.gem (100%)
Successfully installed rake-0.9.2
1 gem installed
Installing ri documentation for rake-0.9.2...
Installing RDoc documentation for rake-0.9.2...

$ which rake
/Users/jaredhanson/.rvm/gems/ruby-1.8.7-p352/bin/rake

Conclusion

RVM is now installed, and can be used to manage multiple Ruby environments. Additionally, gemsets can be used to manage which sets of gems are available in each environment. This is a much cleaner and more flexible way to manage a Ruby development system.

Post a comment