Foliosus Web Design LLC: Your website done right

Blogfolio

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

May 05 ’08

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.

Conversation in progress…

  1. 1

    On May 8th LuciDreamer said:

    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.

  2. 2

    On May 8th Brent Miller said:

    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.

  3. 3

    On May 8th LuciDreamer said:

    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!

  4. 4

    On May 13th Michael Teter said:

    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

  5. 5

    On August 7th Mark Dodd said:

    First I am an Apple newby.
    I have OCI and Rails configured on an XP and now want to start developing on my new MacBook Pro.

    I followed your steps with some local help.

    1. I am sure I have instantclient installed correctly. It was tested with sql_plus and was able to access our DBs with TNANAMES

    2. I could not find the gem indicated in your instructions. I used the ...enhanced-adapter gem instead

    3. The compile seemed to go OK. I did not notice any errors

    4. The Test Connectivity step works just fine

    5. the config.yml file had OCI as the adapter. I also tried Oracle

    6. I do not get any errors when I start the server - Mongrel from Aptana

    However when I try to hit the application from a browser I get a "Oracle/OCI libraries could not be loaded." error.

    I am unsure where the problem is and how to resolve it.
    Any help would greatly be appreciated.
    Mark

  6. 6

    On August 10th Brent Miller said:

    @Mark: In your config.yml you should try this:

    adapter: oracle_enhanced

  7. 7

    On August 22nd Brent Miller said:

    Alderete: Because the ActiveRecord Oracle adapter is namespaced to AR::ConnectionAdapters, if you were running in irb you couldn't just require 'activerecord-oracle-adapter' and have it work. Instead, you would have to require 'active_record/connection_adapters/oracle_adapter'. So, for the config line, you have to use:

    config.gem 'activerecord-oracle-adapter', :lib => 'active_record/connection_adapters/oracle_adapter', :source => 'http://gems.rubyonrails.org'

    The :lib option is necessary when the gem name & library name differ, as they do here. I wish the docs for the config settings were a little bit better, but unfortunately that's where we are.

  8. 8

    On August 22nd Alderete said:

    Here's a question: If I am on Rails 2.1, and I want to add the Oracle Adapter and ruby-oci8 as dependencies using the new gem dependency feature of 2.1, is it possible to do that?

    I've tried it using this line:

    config.gem 'activerecord-oracle-adapter', :source => 'http://gems.rubyonrails.org'

    But when I restart my development server, I get an error with a stack trace:

    => Booting Mongrel (use 'script/server webrick' to force WEBrick)
    => Rails 2.1.0 application starting on http://0.0.0.0:3001
    => Debugger enabled
    => Call with -d to detach
    => Ctrl-C to shutdown server
    ** Starting Mongrel listening at 0.0.0.0:3001
    ** Starting Rails with development environment...
    no such file to load -- activerecord-oracle-adapter
    /Library/Ruby/Site/1.8/rubygems/custom_require.rb:27:in `gem_original_require'
    [...]

    Any ideas?

  9. 9

    On August 22nd Alderete said:

    I should add, I'm quite certain the gem is installed; I copied-and-pasted the name of the gem from the output of gem list.

    I get similar failure messages when I try to rake gems:unpack to automatically unpack gem dependencies, but I was successful doing a manual gem unpack activerecord-oracle-adapter. Of course, that didn't change the stack trace / errors when starting up my server....

  10. 10

    On September 4th walt bajkowski said:

    should I run Oracle & ruby on seperate servers

  11. 11

    On October 7th Jens Himmelreich said:

    In respond to LuciDreamer.
    You don't have to create a path like
    /scratch/plebld/208/rdbms/lib/.
    Just put the dylib's in $HOME/lib and netbeans
    (it's the dyld) will found everything.

  12. 12

    On December 9th Russell Balest said:

    Many thanks to you. I've been relying on your info for the past 12 months to use Rails and Oracle on OSX. Recently, the Rosetta solution for Ruby became a big problem because mysql adapter doesn't like ppc bundle, and I needed both Oracle and Mysql together. Now it's all fine. Incidentally, where do you run your Oracle server - also on OSX ? For now, mine is on a Linux VM also on my OSX.

Join the conversation

* indicates a required field

New comment

Will never be disclosed or used inappropriately

Enter the word why to confirm that you are a human being

About the author

Brent Miller is the owner and principal web designer of Foliosus Web Design LLC in Portland, Oregon. He enjoys food, plants, and the color green. If you are interested in hiring him for web work, please contact him.

Categories

Plant of the day

Swirls in a fallen log

Swirls in a fallen log

Fresh photos