Fixing Snow Leopard ruby readline

Building ruby readline

Since I upgraded to Snow Leopard I’ve been missing readline whe using irb. As I discovered in this article, this is due to apple’s ruby linking to libedit instead of libreadline. I didn’t have that problem before the upgrade since I had compiled ruby myself.

This time, I was looking for another solution. I could have compiled ruby with readline support, but then probably I’d had to reinstall some gems too. So I present you the quick way to fix your readline

Step 0: Setup temp dir

mkdir -p /tmp/rlruby
cd /tmp/rlruby
sudo -s

Step 1: Install readline

curl -O ftp://ftp.cwru.edu/pub/bash/readline-6.0.tar.gz
tar xvf readline-6.0.tar.gz
cd readline-6.0
./configure && make && make install
cd ..

Step 2: Get ruby source

To keep the complications to a minimum, I downloaded ruby from apple (check 10.6.2 open source, or other releases). The current patchlevel is ruby-75 so fetch that one:

curl -O http://www.opensource.apple.com/tarballs/ruby/ruby-75.tar.gz
tar xvf ruby-75.tar.gz
cd ruby-75

Step 4: Build readline extension

We don’t need to build all ruby, just the readline extension

cd ruby/ext/readline/
ruby extconf.rb
make

At this point, you’ll probably get the following error:

readline.c: In function ‘username_completion_proc_call’:
readline.c:730: error: ‘username_completion_function’ undeclared (first use in this function)
readline.c:730: error: (Each undeclared identifier is reported only once
readline.c:730: error: for each function it appears in.)
readline.c: In function ‘username_completion_proc_call’:
readline.c:730: error: ‘username_completion_function’ undeclared (first use in this function)
readline.c:730: error: (Each undeclared identifier is reported only once
readline.c:730: error: for each function it appears in.)
lipo: can't open input file: /var/folders/s4/s4qO7oueE3ijABAH7qB6Dk+++TI/-Tmp-//ccW5lOLL.out (No such file or directory)
make: *** [readline.o] Error 1

We need to tell gcc that our readline is in /usr/local

make readline.o CFLAGS='-I/usr/local/include -DHAVE_RL_USERNAME_COMPLETION_FUNCTION'
cc -arch i386 -arch x86_64 -pipe -bundle -undefined dynamic_lookup -o readline.bundle readline.o -L/usr/local/lib -L/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib -L. -arch i386 -arch x86_64 -lruby -lreadline -lncurses -lpthread -ldl

To be sure we are using the real readline run otool and make sure libedit doesn’t appear on the results:

$ otool -L readline.bundle
readline.bundle:
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/libruby.1.dylib (compatibility version 1.8.0, current version 1.8.7)
/usr/lib/libncurses.5.4.dylib (compatibility version 5.4.0, current version 5.4.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 125.0.0)

Step 5: Replace readline.bundle

cd /System/Library/Frameworks/Ruby.framework/Versions/Current/usr/lib/ruby/1.8/universal-darwin10.0/
mv readline.bundle readline.bundle.libedit
cp /tmp/rlruby/ruby-75/ruby/ext/readline/readline.bundle readline.bundle

Now launch irb and check if all your favorite shortcuts are in place

About these ads

8 thoughts on “Fixing Snow Leopard ruby readline

  1. Awesome! I had previously recompiled ruby to get this. Since getting a new work mbp, I haven’t gone down that road yet and severely miss vi-editing-mode every time I get in irb. Thanks for the great hack, Cheers!

  2. Have you managed to make this same fix for ruby 1.9 yet?

    I have found the same irb/readline problem with ruby 1.9.1p376 (2009-12-07 revision 26041) [i386-darwin10.2.0]

  3. Pingback: Mauricio de Amorim » Blog Archive » Erro ao instalar MySql com Homebrew no MacOS

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s