Thursday, December 8, 2011

Installing (painlessly) Ruby 1.9.3 into Mac OS X

Update: Nov. 2012. I no longer recommend rbenv, I now recommend RVM (ruby version manager) since both require Xcode to function. And the latest JRuby (1.7.0) now defaults to 1.9.3 and doesn't require the --1.9 switched to be passed or a JRUBY_OPTS variable set.
(I also updated the post to reflect the proper capitalization of JRuby vs. Jruby, which is incorrect.)
-----

Installing the latest version of Ruby on Mac OS X is a bit more complicated than it should be. There are no .dmg installers and the built in version 1.8.7 is quite old now.  There are various ways of installing ruby on a Mac and here is a list from the least painful to the most annoying.

  1. Using JRuby or MacRuby are the least painful way to install Ruby 1.9 to OS X. They come with their own .dmg installer, it's easy and fast. Unfortunately JRuby (although it includes 1.9) defaults to 1.8 so you have to go into your .bash_profile file and add a line there. See previous blog here. Note that because ruby is already installed on the Mac you'll have to use JRuby/MacRuby vs. ruby, jgems/macgems vs. gems and jirb/macirb vs. irb on the command line, and to run rails in JRuby it's the arcane jruby -S rails. I haven't gotten rails to run well with MacRuby yet, but MacRuby is the way to go for integration with the Mac. MacRuby plays well with Xcode too. But one thing both of these have against them: they're not 1.9.3, they only go to 1.9.2.
  2. Using ruby_build with rbenv mostly painless but there are a few snags I hit and below is an explanation on how I installed it and how to avoid the snags.
  3. Using RVM with Xcode. Rvm or the ruby version manager is the way to go on Linux but on the Mac it requires the gargantuan (and thankfully now free) Xcode to be installed. Once you have Xcode on board it should be easy sailings (mostly). Though if you use Netbeans watch out it has not so good interaction with rvm's rubies.
  4. MacPorts or HomebrewI haven't played around with these because they requires Xcode to install, but looks straight forward. Macports has an dmg installer. And Homebrew looks like it's even easier. Homebrew is actually written in ruby. There is also Fink but I haven't heard much about it. 
  5. Install from source (Painful! And no I haven't tried it). I've installed ruby from source on Ubuntu and the process was straight forward-ish but on the Mac I'm not so sure. My guess is that the new compiled ruby will go to /usr/local/bin instead of the /usr/bin/ where the normal Ruby is but I don't know. Also this requires Xcode installed and probably some interesting work on the path. Additionally compiling from source you'll have to worry about weather you have the right headers for x or y plugin. I ended up deleting my Ubuntu compiled Ruby because the it would not play with the gtk2 ruby gem. 


Recommended: ruby_build with rbenv

  • Pros: light and fast, not much to install.
  • Cons: may not work the first time.

The first thing to install is ruby_build. First you'll need Git on your system, if you don't have it already (you should) go get that first (get further info on that at ProGit). Then go to the website https://github.com/sstephenson/ruby-build. There you'll see the command line inputs to get ruby_build installed.


$ git clone git://github.com/sstephenson/ruby-build.git
$ cd ruby-build
$ ./install.sh

This should install ruby_build to usr/local, you may have to do sudo ./install.sh to get it to work.

That's the easy part. Now we need to get rbenv which will allow us to get multiple version of ruby installed but more importantly Ruby 1.9.3.

To install rbenv you need to create the directory where it will go, possibly adjust the path, check your ~/.bash_profile file for errors (I had some in there), then install Ruby 1.9.3 and possibly even delete some files and then re-install it (just to be on the safe side).

The instructions on how to install rbenv are on the website. They are also almost the same as the ones for ruby_build. First create a ~/.rbenv folder and then follow the commands on the website.

Immediately after you install rbenv check your .bash_profile file and make sure it looks ok. Mine was missing a line ending between two commands and would not work until I fixed that. Also be aware that because rbenv and ruby_build work using shims you need to re-build the look up paths or re-hash every time you install anything or do what I do: close the terminal and open a new terminal window.

Once you have rbenv installed you need to install Ruby 1.9.3 into it and ruby build uses this command shortcut to do it.
rbenv install 1.9.3-p0
After you install it some commands like Rails or Rake may still use the old path. I deleted these from the /usr/bin/ file and re-installed and that fixed it. But be aware that you need to re-start the terminal every-time you install. The rbenv rehash command didn't work once on mine as advertised, but restarting the terminal always did.

Recommended: JRuby

  • Pros: Easy install, works with Rails, plays well with other installations.
  • Cons: Requires the preface "jruby -S" on many commands, defaults to 1.8.7, doesn't have 1.9.3 yet.

To install Jruby just download the dmg installer from JRuby.org and follow the install instructions. To access Jruby just type jruby and the file name or jruby -S and the command. Additionally there a two shortcuts to the gem and irb commands in jgem and jirb. Also try out jirb_swing which is an irb session in a swing windows and has auto-complete.

To set JRuby to default to 1.9.2 you have to edit your .bash_profile file, more info on this blog post.
To use Rails with JRuby you may have to use the Java version of many gems, more info on this blog post.

JRuby is a good back up ruby and works fairly well overall. Don't be worried if the start-up seems to take forever, it's because it's loading all of Java first. Once Java is loaded it runs pretty well.

No comments:

Post a Comment