Connecting Ruby on Rails to Oracle on an Intel Mac in Leopard, take 2

Since I posted the first version of this article, there have been a couple of changes. The biggest one is that Intel has released Intel Mac versions of the InstantClient. Woohoo! That makes the previous version rather too complicated, so I’ve updated it here. This tutorial assumes that you’re using Rails 2.0 or greater. If you’re starting from the setup we had before, and you want to fix it, start with the cleanup instructions at the bottom, and then come back here.

Grab the new Oracle InstantClient libraries

The new Intel Mac versions are available from the Oracle downloads site. Install them in /Library/Oracle/. You can do side-by-side installations in folders with whatever names you want, since apps find them by using the $ORACLE_HOME environment variable (and it’s friends). I’ve got mine in /Library/Oracle/instantclient/10.2.0.4. Also make sure that you’ve got the files required to run sqlplus and the sdk. You can drop those in the same directory.

Oh, and don’t forget to copy over your tnsnames.ora file.

Symlink the libraries

In the directory where you’ve installed the instant client, run this:

ln -s libclntsh.dylib.10.1 libclntsh.dylib

Set the environment variables correctly

You’ll probably want to put these lines in your .bash_profile, but they also must be run (or source‘d) from the command line to take effect:

export ORACLE_HOME=/Library/Oracle/instantclient/10.2.0.4
export TNS_ADMIN=$ORACLE_HOME
export LD_LIBRARY_PATH=$ORACLE_HOME
export DYLD_LIBRARY_PATH=$ORACLE_HOME
export PATH=$PATH:$ORACLE_HOME

On to the next step, making Rails & Oracle play nice.

Getting Rails to talk to Oracle

First,

sudo gem install activerecord-oracle-adapter --source http://gems.rubyonrails.org

This installs the oracle adapter, which is how ActiveRecord deals with Oracle. It doesn’t, however, install the Ruby oci8 driver, which is how Ruby talks to Oracle. We’ll do that now.

Compile the ruby-oci8 library

Make sure you’ve got the Oracle Instant Client SDK installed in your Oracle Instant Client directory (/Library/Oracle/instantclient/10.2.0.4)

The most recent stable version of the oci8 library is 1.0.1. Download it and unpack the file in the finder: it should unzip into ~/Downloads/ruby-oci8-1.0.1.

Now we can finish configuring the environment before we compile the library.

cd ~/Downloads/ruby-oci8-1.0.0
export SQLPATH=$ORACLE_HOME
export RC_ARCHS=i386
ruby setup.rb config
make
sudo make install

These steps should cause some compiler output to scroll by. Pay attention if you run into any errors on the setup step; the error messages are reasonably helpful at pointing you towards any problems.

Test connectivity

At this point, we’re done. We’ve got the latest Oracle InstantClient, and we’ve installed the ruby-oci8 library. Now it’s a question of making sure that everything works as advertised:

ruby /usr/bin/irb

In the IRb console, type:

require 'oci8'

If the console returns true or [], you’re in business.

Configure your database.yml

The last step is to make your application use the Oracle connection. In your database.yml, use the following to make it work:

development:
adapter: oracle
database: your_instance_name
username: your_user_name
password: your_password

The database name comes straight out of your tnsnames.ora file. You don’t need to specify any other connection information in database.yml, since the tnsnames.ora file has everything you need.

Your application is now talking to Oracle, without Rosetta!

Fix the ruby_fat and ruby_ppc setup

If you followed my previous instructions and went through all of the shenanigans with the ruby_ppc and ruby_fat versions and would like to undo them, it’s quite simple. Just remove the ruby_ppc files, and the symlinks to them (called ruby, and rename ruby_fat as ruby:

cd /usr/bin
sudo rm ruby
sudo rm ruby_ppc
sudo mv ruby_fat ruby
cd /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin
sudo rm ruby
sudo rm ruby_ppc
sudo mv ruby_fat ruby

Then, you should also remove the 2 management scripts:

sudo rm /usr/bin/ppc_ruby.sh
sudo rm /usr/bin/fat_ruby.sh

And that’s enough for the cleanup. From here you can start following the instructions beginning with getting the new Oracle libraries.

4 Responses to “Connecting Ruby on Rails to Oracle on an Intel Mac in Leopard, take 2”

Subscribe to comments

Leave a Reply