Installing ruby 1.9preview1 on OS X Leopard
18th November 2008
Tonight I’m trying conciseness.
Editor’s note: I failed.
I recently decided to test my git_remote_branch gem with Ruby 1.9, for the heck of it. Well, I was making sure it ran on a bunch of platforms: Windows, Ruby 1.8.7 and with the most recent Git version (1.6.0.2, get it). So it seemed fitting to check it out under Ruby 1.9.
On Leopard, the only missing dependency to Ruby 1.9 is readline 5.2. This article will present the installation of both. And help heat up your apartment.
Linux too
These instructions will mostly work on Linux as well (tried on Ubuntu). There will be a few minor differences though.
- Make sure you download the original readline from the gnu site and patch it yourself;
- Uninstall older versions of Ruby1.9;
- Make sure you have the basic dev tools installed, like gcc and make;
- Skip the part about installing Xcode :-)
Prerequisites
You first need to have the Apple developer tools installed on your mac. They’re available on your installation CD. Put it in, run the installer. It’s pretty straightforward.
# in your terminal
open /Volumes/Mac\ OS\ X\ Upgrade\ DVD/Optional\ Installs/Xcode\ Tools/XcodeTools.mpkg
If you don’t compile your own stuff often, you may have to set up your PATH variable in your ~/.bash_profile.
# file ~/.bash_profile
export PATH="/usr/local/bin:$PATH"
Now, prepare a working directory to keep the source close to the corresponding executables.
# in your terminal
sudo mkdir -p /usr/local/src
sudo chgrp admin /usr/local/src
sudo chmod -R 775 /usr/local/src
cd /usr/local/src
Once you’ve set yourself up, if you don’t care about the details, you can skip to the end for the Cliff’s notes.
Installing readline
This one’s not as straightforward as it could have been. The gzipped readline library available on the gnu site is 12 patches behind. It so happens that the 12th patch fixes a problem with compilation under OS X. So I applied all 12 to the code and repackaged it. The example uses that file, compiled by me.
You can also do do the patching by yourself if you so wish. Here’s where you can download the readline code:
So let’s get on with the instructions to install readline from my patched package:
# in your terminal
curl -O http://s3.amazonaws.com/webmat-public/readline-5.2-patch012.tar.gz
md5 readline-5.2-patch012.tar.gz
# should be a9f37d2a22d181f8c23c6a320907917d
tar xzf readline-5.2-patch012.tar.gz
cd readline-5.2-patch012
./configure --prefix=/usr/local
make
sudo make install
cd ..
Build Ruby 1.9
Build options
Note that here you have a few options as to how you want to distinguish your 1.9 stack from your main 1.8 one.
In the following example, I build Ruby with the ‘1.9’ suffix, which means all executables will be suffixed with 1.9: ruby1.9, gem1.9, irb1.9, rake1.9 and so on. This approach is ideal for casual use of two versions side by side. If you don’t care about the details, skip right over the next paragraph.
The industrial approach would be to put ruby in a non standard directory and only add it to your path when you want to use that version (or use the explicit path to invoke executables). To go industrial, you can simply use the --prefix=/usr/local/ruby1.9 option and then drop the --program-suffix argument when you run configure for Ruby. This setup is ideal if you really want to have a bunch of versions living side by side (e.g. all 1.9 versions as well as 1.8.7 in addition to the current 1.8.6).
Actual installation of Ruby 1.9
Pick the most recent version or Ruby 1.9 on the ftp server. At the time of writing, 1.9.1-preview1 is the most recent.
So, still from /usr/local/src:
# in your terminal
curl -O ftp://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.1-preview1.tar.gz
tar xzvf ruby-1.9.1-preview1.tar.gz
cd ruby-1.9.1-preview1
./configure --prefix=/usr/local --program-suffix=1.9 --enable-pthread --with-readline-dir=/usr/local --enable-shared
make
sudo make install
Now you’re about to see some of the funniest looking progress indicators around. Ruby’s about making the programmer happy, and it delivers even in the details!
Note: the recent source packages of Ruby1.9 automatically include the documentation, as the end of the make install attests.
Try Ruby 1.9
# in your terminal
ruby1.9 --version
gem1.9 --version
irb1.9
Once inside the Ruby interactive shell,
# in irb1.9
RUBY_VERSION
#=> "1.9.1"
stabby = ->(msg='inside the stabby lambda') { puts msg }
stabby.call
# => "inside the stabby lambda"
stabby.call 'hello world'
# => "hello world"
Yep, Ruby 1.9 introduces the very cool stabby lambda syntax. Ruby 1.8’s lambdas couldn’t have optional parameters (unless you fiddled with *args). 1.9’s stabby lambdas can, with a syntax as clean as a simple method definition, as you just experimented.
Now install the gems you use everyday (or kindly make available to your peers) and help make them 1.9 compatible.
For the sake of the stabby lambda!
That’s it! (Except for those who skipped to the Cliff’s Notes).
Cliff’s Notes
# Install patched readline
cd /usr/local/src
curl -O http://s3.amazonaws.com/webmat-public/readline-5.2-patch012.tar.gz
md5 readline-5.2-patch012.tar.gz
# should be a9f37d2a22d181f8c23c6a320907917d
tar xzf readline-5.2-patch012.tar.gz
cd readline-5.2-patch012
./configure --prefix=/usr/local
make
sudo make install
cd ..
# Install Ruby1.9
curl -O ftp://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.1-preview1.tar.gz
tar xzvf ruby-1.9.1-preview1.tar.gz
cd ruby-1.9.1-preview1
./configure --prefix=/usr/local --program-suffix=1.9 --enable-pthread --with-readline-dir=/usr/local --enable-shared
make
sudo make install
# Try Ruby 1.9
ruby1.9 --version
gem1.9 --version
irb1.9
# in irb1.9
RUBY_VERSION
stabby = ->(msg='inside the stabby lambda') { puts msg }
stabby.call
stabby.call 'hello world'

November 19th, 2008 at 06:58
[...] Installing ruby 1.9preview1 on OS X Leopard - How to do it. [...]
November 19th, 2008 at 10:41
Why don’t you just use macports
$> port search ruby19
ruby19 lang/ruby19 1.9.1-preview1 Powerful and clean object-oriented scripting language
sudo port install ruby1.9Tada.wav
November 19th, 2008 at 10:58
@Hukl
LOL @ tada.wav :-)
I’m trying to minimize my use of Macports now. It’s actually not installed on my new machine.
On my previous machine, half the time when I used port, it would end up failing to build a dependency or something. I never found an obvious way to fix these problems and never really investigated further either. So the blame is probably half on my side.
What I like about building my own is that the day the next version comes out, I can go back and build it myself and not wait for the macport update. Also, by using the “industrial strength” approach described briefly in the article, I can actually have an arbitrary amount of rubies installed in parallel.
These reasons may seem a bit shallow, but I guess I just like to have a better understanding and complete control over how the stuff is installed on my machine. :-)
November 19th, 2008 at 11:28
Sure, macports aren’t perfect but I like it more than having to maintain a lot of software that was built manually. I had it for years and it got messy. I can live with the macports issues and actually its not so hard to update a portfile for a new version. most of the time you have to adjust the url of the new tgz.
But I understand your reasoning.
November 19th, 2008 at 11:40
@Hukl
Funny, a few minutes ago I wanted to use wget and I don’t have it installed at the moment. I realized I wouldn’t mind using port for small things like that ;-)
I’d love a quick primer on how to deal with the Macport issues, actually.
The doc didn’t seem too clear to me last time I checked. I’d just like to know what clutter I can remove when an install fails, what file to update to fix a build and finally how to contribute that fix.
Are you aware of any good tutorial? OTOH I don’t want to spend an evening going through all of their doc ;-)
November 19th, 2008 at 11:41
@Hukl
I just noticed the last post on your site is about macports. I’ll have to check it out after work :-)
November 26th, 2008 at 01:37
[...] I wanted to easily reference both the Ruby 1.8 and Ruby 1.9 so I could easily work with both. I came across a neat switch that I had never cared to use before but made perfect sense for my [...]
December 5th, 2008 at 06:03
Thanks for posting patched readline source, but it didn’t work for me. Here’s what I got when I tried to make:
December 5th, 2008 at 06:16
My apologies–it worked fine.Many thanks–the broken OS X readline has been a pain in my ass for years.Had to have someone hack the XBoard source to make it compile.
December 22nd, 2008 at 14:29
@Huki
Error: Port ruby1.9 not found
:D
February 2nd, 2009 at 09:53
hi Mathieu, great posting. did you also try compiling the stable version 1.9.1 on leopard already? we are currently doing compiles for various osx versions, including leopard for the rubyosx (ruby one click installer for osx) project http://rubyosx.com the aim of the project is exactly what you said, keeping an osx version slim, not even installing xcode in the “worst case”. if you already did a compile, would you mind sending us your output of “ruby runner.rb” from the “/test” directory, so we could see which tests fail/work on your machine? see also the current thread on the rubyosx list regarding the osx leopard compile: http://rubyforge.org/pipermail/rubyosx-list/2009-February/000010.html
February 2nd, 2009 at 09:54
does this actually still apply to ruby 1.9.1 stable? when i try to compile ruby with the –with-readline-dir=/usr/local switch, it says “configure: WARNING: unrecognized options: –with-readline-dir” other people seem to have that problem as well, see: http://www.ruby-forum.com/topic/175410
February 19th, 2009 at 23:34
I am just new at this
what is readline and why do i need it and what happens if i don’t have it
thanks fousto@me.com
February 23rd, 2009 at 10:03
Readline is a library that can be used to easily implement a “memory” of past commands, among other features. It’s probably used by IRB.