Adventures in HttpContext All the stuff after 'Hello, World'

Getting Ruby 1.9, Readline, Rails, and Mysql all running on Snow Leopard

In my never ending love/hate relationship with Ruby, Rails and my Mac I’ve finally gotten Ruby 1.9 up and running with Rails 2.3 and MySql 64 bit.  All on Snow Leopard.  There was an even a little detour with Readline.  If you’ve scoured other posts about Snow Leopard, Ruby, Rails and Mysql and ended up here I feel your pain.  I hope this helps you on your way. Most of this info is from other places which I’ve explained in a little (just a little) but more depth.

Install XCode

You need XCode to do any of this, so install it.  If you’re upgrading to Snow Leopard, reinstall XCode so you get the correct c compiler.

Your profile

Here’s the deal.  We’re going to install ruby 1.9 to your /usr/local directory.  It will dump stuff in /usr/local/bin and other stuff in /usr/local/lib.  Why here?  That’s where it goes.  The default install of ruby on Snow Leopard 1.8, lives in /System/Library/Frameworks/Ruby.framework/Versions/Current.  Current is really an alias (actually, symlink) to the 1.8 directory at the same level.  For some it may seem like a good idea to install ruby 1.9 here.  It’s not.  Just put in /usr/local like everyone else.

Because ruby 1.9 will live in our /usr/local you have to help out your terminal a little.  You have to tell it where to look for the bin of ruby 1.9.  So when you run “ruby” from terminal you get the 1.9 version in /usr/local, not the 1.8 version in the System Library.  That’s why you have to add a path to /usr/local in your profile.  Do this from the terminal:

mate ~/.bash_profile

This says, “open or create the file .bash_profile, in the home directory (~/), using textmate”.  You can use any other editor if you know how- but if you did you probably wouldn’t need to read this.  So just buy- and use- textmate.  It’s a nice app.  Now, .bash_profile is a file used by bash, aka the terminal app, for settings.  Some places you’ll see “mate .profile” instead.  This will work too– but if you have a .profile and .bash_profile you may run in to problems.  Just have one, preferably .bash_profile, and write this in it:

export PATH=/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:$PATH

This appends our /usr/local/bin and sbin to the current list of directories to search for when trying to find out where all those little commands live which you type into the terminal.  Keen eyes may have noticed the mysql/bin thrown in there.  This is for later.  The :$PATH at the end is extremely important- it includes other paths which are included in other places. Once this is done then type “source .bash_profile” from terminal to load the changes.

Try Installing Ruby

One way to install ruby is by using MacPorts.  If you want to get a little more hands on, we’re going to pull the source down and build it ourselves.  MacPorts is probably the easiest option.  We’re not doing the easy option.

Note: Read all this first!  In the terminal, make sure you’re in your home directory by doing a simple “cd”.  Then, do this:

mkdir src
cd src

We’re creating a new directory called src for our source files, and moving into said directory.  Now we can get the code:

curl -O ftp://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.1-p376.tar.gz

tar xzvf ruby-1.9.1-p376.tar.gz

cd ruby-1.9.1-p376/

Curl is a nice little app to pull things from the internet.  The -O option simply names the local file the same as the remote file.  Ruby 1.9.1-p376 was the latest version as of this writing, but [In my never ending love/hate relationship with Ruby, Rails and my Mac I’ve finally gotten Ruby 1.9 up and running with Rails 2.3 and MySql 64 bit.  All on Snow Leopard.  There was an even a little detour with Readline.  If you’ve scoured other posts about Snow Leopard, Ruby, Rails and Mysql and ended up here I feel your pain.  I hope this helps you on your way. Most of this info is from other places which I’ve explained in a little (just a little) but more depth.

Install XCode

You need XCode to do any of this, so install it.  If you’re upgrading to Snow Leopard, reinstall XCode so you get the correct c compiler.

Your profile

Here’s the deal.  We’re going to install ruby 1.9 to your /usr/local directory.  It will dump stuff in /usr/local/bin and other stuff in /usr/local/lib.  Why here?  That’s where it goes.  The default install of ruby on Snow Leopard 1.8, lives in /System/Library/Frameworks/Ruby.framework/Versions/Current.  Current is really an alias (actually, symlink) to the 1.8 directory at the same level.  For some it may seem like a good idea to install ruby 1.9 here.  It’s not.  Just put in /usr/local like everyone else.

Because ruby 1.9 will live in our /usr/local you have to help out your terminal a little.  You have to tell it where to look for the bin of ruby 1.9.  So when you run “ruby” from terminal you get the 1.9 version in /usr/local, not the 1.8 version in the System Library.  That’s why you have to add a path to /usr/local in your profile.  Do this from the terminal:

mate ~/.bash_profile

This says, “open or create the file .bash_profile, in the home directory (~/), using textmate”.  You can use any other editor if you know how- but if you did you probably wouldn’t need to read this.  So just buy- and use- textmate.  It’s a nice app.  Now, .bash_profile is a file used by bash, aka the terminal app, for settings.  Some places you’ll see “mate .profile” instead.  This will work too– but if you have a .profile and .bash_profile you may run in to problems.  Just have one, preferably .bash_profile, and write this in it:

export PATH=/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:$PATH

This appends our /usr/local/bin and sbin to the current list of directories to search for when trying to find out where all those little commands live which you type into the terminal.  Keen eyes may have noticed the mysql/bin thrown in there.  This is for later.  The :$PATH at the end is extremely important- it includes other paths which are included in other places. Once this is done then type “source .bash_profile” from terminal to load the changes.

Try Installing Ruby

One way to install ruby is by using MacPorts.  If you want to get a little more hands on, we’re going to pull the source down and build it ourselves.  MacPorts is probably the easiest option.  We’re not doing the easy option.

Note: Read all this first!  In the terminal, make sure you’re in your home directory by doing a simple “cd”.  Then, do this:

mkdir src
cd src

We’re creating a new directory called src for our source files, and moving into said directory.  Now we can get the code:

curl -O ftp://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.1-p376.tar.gz

tar xzvf ruby-1.9.1-p376.tar.gz

cd ruby-1.9.1-p376/

Curl is a nice little app to pull things from the internet.  The -O option simply names the local file the same as the remote file.  Ruby 1.9.1-p376 was the latest version as of this writing, but]4 for the latest release.  Tar xzvf simply unpacks the compressed download.

Very helpful hint: If you ever are unsure about a command, simply type the command and –help.  As in, curl –help.  This is very helpful.

Next, we get to the good stuff.  First, run autoconf simply by typing “autoconf”:

autoconf

Autoconf is a tool used for generating configuration scripts.  It’s important you run this.  Then, run the configuration script.  The thing which worked for me was:

./configure --prefix=/usr/local/ --enable-shared --with-readline-dir=/usr/local

This tells us to install ruby in the /usr/local directory, build a shared library for ruby, and use the readline installation found in /usr/local. Very Important: some users prefer adding a suffix to the ruby 1.9 install so it doesn’t interfere with the system install of ruby.  By adding the –program-suffix=19 option to configure you’ll append “19” to all commands, like “ruby19″ and “gem19″.  This is a smart idea as it won’t interfere with the default ruby installation.  Using this technique there are ways to easily switch between ruby installations.  If you don’t care about 1.8, and just want the ease of typing “ruby” and getting the latest 1.9, omit the –program-suffix option.

If you run ./configure and get an error of:

configure: WARNING: unrecognized options: –with-readline-dir

You did not run autoconf.  Type it and run it, then run configure again.

Sometimes you’ll see the –enable-pthread option.  There seems to be some debate on whether this is a good idea.  I say omit it unless you know what you’re doing.  You can google for more info.  Feel free to explore and google other configure options- simply type “configure –help” to list them all.

Next we need to run:

make

This builds all the source files needed for the install.  If you run this and get an error of:

readline.c: In function ‘username_completion_proc_call’:

readline.c:1159: error: ‘username_completion_function’ undeclared (first use in this function)

You don’t have readline- or at least the proper version of readline- installed.  This is a problem.  Let’s get it.

Readline

cd ~/src
curl -O ftp://ftp.gnu.org/gnu/readline/readline-6.0.tar.gz
tar xzvf readline-6.0.tar.gz
cd readline-6.0
./configure --prefix=/usr/local
make
sudo make install

You’ve now installed readline.  You may get a warning of install: you may need to run ldconfig at at the end. Don’t worry about it.  At least I didn’t have to worry about it.

Do everything again

By now, you know the drill.  Hop back to the ruby source code directory and try it again.  But if you ran make in the ruby install step and got errors, just run “make clean” to reset everything.  It’s a good idea.

make clean
autoconf
./configure --prefix=/usr/local/ --with-readline-dir=/usr/local --enable-shared
make
sudo make install

That should be it.  Hopefully you’re error free.  Typing:

which ruby

should return:

/usr/local/bin/ruby

and typing “ruby -v” should return the version of ruby you’ve just downloaded.  Unless you used the –program-suffix option above, then it’s probably “ruby19 -v”

Rails

Now you can download and install rails.  Simply run:

sudo gem update --system
sudo gem install rails

Mysql

Mysql is a piece of cake.  Simply grab the x86_64 install package from the Mysql site. Even though it’s for 10.5, it works fine on 10.6 (Snow Leopard).  Once this is done, you can build the mysql gem:

sudo env ARCHFLAGS="-arch x86_64" gem install mysql -- --with-mysql-config=/usr/local/mysql/bin/mysql_config

Fin

Now, try to get everything running.  Go back to your home directory, create a rails app, and see if it works:

cd ~/
rails playground --databse=mysql
cd playground
rake db:create
script/server

Go to http://localhost:3000, check your environment settings, and you should see:

Ruby version 1.9.1 (i386-darwin10.2.0)
RubyGems version 1.3.5
Rack version 1.0
Rails version 2.3.5
Active Record version 2.3.5
Active Resource version 2.3.5
Action Mailer version 2.3.5
Active Support version 2.3.5

And voila, you’re done!

UPDATE:

If you use Textmate to develop with Rails, your Textmate Ruby path will point to the system’s 1.8 version, so you’ll get awakard issues of Gems not being available or other weird stuff when trying to run Ruby within Textmate (like when you want to RSpec tests).  This fix is simple: go to Textmate -> Preferences -> Advanced -> Shell Variables and add TM_RUBY with a value of /usr/local/bin/ruby and you’ll be good to go.