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.
Asclepias fascicularis (Asclepiadaceae); Narrow-leaf milkweed

I did everything after cleaning up the previous PPC setup and it works in IRB mode. However when calling the OCI8 from NetBeans within the project, it gives the error:
Oracle/OCI libraries could not be loaded: dlopen(/Library/Ruby/Site/1.8/universal-darwin9.0/oci8lib.bundle, 9): Library not loaded: /scratch/plebld/208/rdbms/lib/libclntsh.dylib.10.1
Referenced from: /Library/Ruby/Site/1.8/universal-darwin9.0/oci8lib.bundle
Reason: image not found - /Library/Ruby/Site/1.8/universal-darwin9.0/oci8lib.bundle
Any possible suggestions?
Thanks.
Unfortunately, I have zero experience with NetBeans. If OCI8 works from IRB, then all of the Ruby-level work is in place and so the problem must be with NetBeans. If you’ve got a PPC-only NetBeans codebase, or something similar, that could be causing the problem; beyond that, I’m completely in the dark.
Thanks, Brent. I was able to resolve the issue, and actually the fix turned out to be so simple.
I just created a new path on my drive: /scratch/plebld/208/rdbms/lib/. This is the exact same path referenced by the error message. Probably it would have been created during the full Oracle install or something, but it did not exist before. Then I placed a copy of the libclntsh.dylib.10.1 executable file there taken from \Library\Oracle\oracleclient folder that contained the latest unzipped version of Oracle client. And it worked.
The new OCI8 combined with Ruby(i386) seems to be running much better and faster! Ruby running in PPC mode would often crash before and eating up lots of CPU. A+++++!
Thanks a lot for all your work, Brent! Excellent and very helpful instructions!
My friend, you kick ass.
These instructions were great! I’ve been in a very poor mood today due to seemingly endless configuration and integration problems, and this is the first success I’ve had all day.
Thanks ever so much for figuring out and posting this!
Michael