ray_apps_blog

April 24, 2008

How to setup Ruby and new Oracle Instant Client on Leopard

Filed under: mac,oracle,ruby — Raimonds Simanovskis @ 1:52 pm

!!! Update !!!

New version of instructions for Snow Leopard is available here.

Introduction

We waited for it long, long time and finally it has arrived – Oracle Instant Client for Intel Mac. I was lucky to test beta version of the client already for the last couple of weeks and so far everything was working OK. Therefore as the final version is now available to everybody I am here rewriting my previous instructions on how to get Ruby working with Oracle on Mac.

Installing Oracle Instant Client for Intel Mac

At first you need to download Oracle Instant Client for Intel Mac. Download “Instant Client Package – Basic” and “Instant Client Package – SDK” and also I suggest “Instant Client Package – SQL*Plus” if you would like to have command line sqlplus utility.

Unzip downloaded archives and move it where you would like to have it – I am keeping it in /usr/local/oracle/instantclient_10_2. Then go to this directory and make symbolic links for dynamic libraries

sudo ln -s libclntsh.dylib.10.1 libclntsh.dylib
sudo ln -s libocci.dylib.10.1 libocci.dylib

Then I recommend to create and place somewhere your tnsnames.ora file where you will keep your database connections definitions – I place this file in directory /usr/local/oracle/network/admin.

Then finally you need to set up necessary environment variables – I place the following definitions in my .bash_profile script:

export DYLD_LIBRARY_PATH="/usr/local/oracle/instantclient_10_2"
export SQLPATH="/usr/local/oracle/instantclient_10_2"
export TNS_ADMIN="/usr/local/oracle/network/admin"
export NLS_LANG="AMERICAN_AMERICA.UTF8"
export PATH=$PATH:$DYLD_LIBRARY_PATH

Use your path to Oracle Instant Client if it differc from /usr/local/oracle/instantclient_10_2. And as you see I also define NLS_LANG environment variable – this is necessary if your database is not in UTF8 encoding but in Ruby you want to get UTF-8 encoded strings from the database. Specifying this NLS_LANG environment variable you will force that Oracle Instant Client will do character set translation.

After these steps relaunch Terminal application (so that new environment variables are set), specify database connection in tnsnames.ora file and try if you can access your database with sqlplus from command line.

Ruby installation

If you are using Leopard then I assume that you are using preinstalled Ruby which is the simplest option. I tried to compile Ruby from sources on Mac OS X Leopard but when I compared performance then original Ruby was a little bit faster on some benchmarks and therefore I sticked with original one.

Compile and install ruby-oci8

Download the latest version of ruby-oci8 (version 1.0.0 at time of writing this post).

As Oracle Instant Client is available just for Intel i386 architecture you need to change in file /usr/lib/ruby/1.8/universal-darwin9.0/rbconfig.rb line 17 to:

'-arch i386'

Then go to directory where you extracted ruby-oci8 source and execute ruby-oci8 standard installation sequence:

ruby setup.rb config
make
sudo make install

After that you can change back file /usr/lib/ruby/1.8/universal-darwin9.0/rbconfig.rb line 17 to:

'-arch ppc -arch i386'

Now try

ruby -r oci8 -e "OCI8.new('scott', 'tiger','orcl').exec('select * from emp') do |r| puts r.join(','); end"

or similar to verify that you can access Oracle database from ruby.

That’s it! Please write in comments if something is not working according to these instructions.


20 Comments »

  1. This worked perfectly for me!! Thanks for posting these instructions.

    Comment by bdewitt — April 24, 2008 @ 5:47 pm | Reply

  2. Installed the instant client as you suggested, but I’m having problems linking in Xcode.
    I’m getting allot of unresolved references:
    “_oparse”, referenced from:
    -[FBOracleCursor executeSQL:] in FBOracleCursor.o
    “_obndrn”, referenced from:
    -[FBOracleCursor bindInputVars] in FBOracleCursor.o
    “_ocom”, referenced from:
    -[FBOracleConnection commitSQLStatement] in FBOracleConnection.o
    “_ofen”, referenced from:
    -[MyDataSource fetchTimer] in MyDataSource.o
    “_odefin”, referenced from:
    -[FBOracleCursor describe] in FBOracleCursor.o
    “_oopen”, referenced from:
    -[FBOracleCursor initLDA:WithConnection:] in FBOracleCursor.o
    “_ologof”, referenced from:
    -[FBOracleConnection closeDatabase] in FBOracleConnection.o
    “_olog”, referenced from:
    -[FBOracleConnection openDatabase] in FBOracleConnection.o
    “_odescr”, referenced from:
    -[FBOracleCursor describe] in FBOracleCursor.o
    “_oexec”, referenced from:
    -[MyDataSource fetchThreadNew:] in MyDataSource.o
    “_opinit”, referenced from:
    -[FBOracleConnection openDatabase] in FBOracleConnection.o
    “_oclose”, referenced from:

    Any clues ?

    Comment by Flemming Bengtsson — April 24, 2008 @ 10:57 pm | Reply

  3. I have just one idea that if you are launching Xcode from Finder or Dock, then .bash_profile is not executed and DYLD_LIBRARY_PATH environment variable is not set. You can set environment variables for GUI applications in ~/.MacOSX/environment.plist file. I have the following in this file:

    { PATH = “/usr/local/bin:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/oracle/instantclient_10_2″;
    TNS_ADMIN = “/usr/local/oracle/network/admin”;
    NLS_LANG = “AMERICAN_AMERICA.UTF8″;
    DYLD_LIBRARY_PATH = “/usr/local/oracle/instantclient_10_2″;
    SQLPATH = “/usr/local/oracle/instantclient_10_2″;
    }

    There is also alternative XML format for property list files. If you already are having some content in this file then user Property List Editor application to add additional environment variables. After modifying the contents of this file you need to log off and log in again.

    Comment by Raimonds Simanovskis — April 24, 2008 @ 11:08 pm | Reply

  4. Thanks for your very quick response!

    I already have this set up in my ~/.MacOSX/environment.plist file and it works fine with the old ppc libraries.
    I have replaced the old installation with the new intel version and created the necessary symbolic links and added them to my project cleaned and rebuild.
    I’m at a lost here, any ideas would be greatly appreciated….

    Comment by Flemming Bengtsson — April 24, 2008 @ 11:24 pm | Reply

  5. As an alternative to editing the rbconfig.rb file, you can simply export an environment variable.

    export ARCHFLAGS=“-arch i386″

    Comment by Pete Jackson — April 25, 2008 @ 12:23 am | Reply

  6. @Flemming: I don’t recognize what you’re seeing in Xcode, but if you’re doing ruby development there and have previously extracted a PPC version of Ruby using “ditto”, then make sure you’ve switched back to the “fat” version of ruby. The PPC-only binary of ruby will not work with the i386 Oracle client.

    Comment by Pete Jackson — April 25, 2008 @ 1:02 am | Reply

  7. I was wondering if it could be something to do with the symbolic links ?
    In my ppc installation the libclntsh.dylib is not a symbolic link but a Mach-O dynamic library.
    Anyone knows how o get from the Oracle shipped libclntsh.dylib.10.1 to the above type of library ?

    Comment by Flemming Bengtsson — April 25, 2008 @ 8:11 am | Reply

  8. There should be no problem if libclntsh.dylib is symbolic link – if any program will try to load this file then the filesystem will resolve this symbolic link and from other programs perspective there should be no difference.

    Yet another idea – if previously you had PPC libraries and if you have not restarted your computer after installation of Intel libraries then probably old PPC shared libraries are still sitting in memory. So try to remove old PPC libraries to some other directory where nobody will see it and then restart computer to ensure that after restart new Intel libraries will be loaded in memory.

    Comment by Raimonds Simanovskis — April 25, 2008 @ 8:52 am | Reply

  9. On my MacBook Pro, I did not have to do the symbolic link for libocci.dylib.10.1, also you do not need the tnsnames.ora for instantclient and thus do not need the TNS_ADMIN env.

    Comment by Matthew Larson — April 25, 2008 @ 1:16 pm | Reply

  10. couldn’t get this to work on Tiger with built-from-source ruby….
    mkmf.log:

    ./conftest
    have_func: checking for OCIInitialize() in oci.h… ——————– no

    “gcc -o conftest -I. -I/usr/local/lib/ruby/1.8/i686-darwin8.11.1 -I/usr/local/src/ruby-oci8-1.0.0/ext/oci8 -g -O2 -fno-common -pipe -fno-common -I/usr/local/lib/oracle/instantclient_10_2/sdk/include conftest.c -L”.” -L”/usr/local/lib” -L. -L/usr/local/lib -L/usr/local/lib/oracle/instantclient_10_2 -lclntsh -lruby-static -L/usr/local/lib -L/usr/local/lib/oracle/instantclient_10_2 -lclntsh -lpthread -ldl -lobjc ”
    /usr/bin/ld: /usr/local/lib/oracle/instantclient_10_2/libclntsh.dylib load command 8 unknown cmd field
    /usr/bin/ld: /usr/local/lib/oracle/instantclient_10_2/libclntsh.dylib load command 8 unknown cmd field
    collect2: ld returned 1 exit status

    Comment by Dean Holdren — April 25, 2008 @ 5:18 pm | Reply

  11. When I was testing Oracle Instant Client beta then there was information that it will run just on Leopard – initially I tried to use it on Tiger but was unable to do it (got similar error messages).

    So I assume that final release of new Instant Client also requires Leopard (I will ask Oracle to add such notice to download pages if this really is so).

    Comment by Raimonds Simanovskis — April 25, 2008 @ 9:00 pm | Reply

  12. Found a solution…

    Problem was that I had two instant client installed, one for when I was running the project in Xcode and one inside the application bundle used when launched from the Finder as a standalone app.

    I had forgot to replace the instant client in the application bundle, it now works flawlessly!

    Comment by Flemming Bengtsson — April 28, 2008 @ 4:21 pm | Reply

  13. Just confirming that the Oracle Instant Client does in fact require Leopard. When I try to get it working on Tiger (version 10.4.11) I get an error that says “Bus error”.

    Comment by Gene — May 28, 2008 @ 5:05 pm | Reply

  14. I’d just like to say, thanks! I had all of this working already, but the UTF8 problem was really throwing me for a loop. Your suggestion to set NLS_LANG totally fixed it, and made my afternoon.

    Comment by Brian Hardy — July 11, 2008 @ 9:21 pm | Reply

  15. Hi.
    I’m getting this annoying error when i do the sudo ruby setup.rb config command:

    “error messages:
    Set the environment variable ORACLE_HOME if Oracle Full Client.
    Append the path of Oracle client libraries to DYLD_LIBRARY_PATH if Oracle Instant Client.”

    Well, i’ve got the Instant Client and i guess that all enviroments variables are OK (i did just like you write).
    Dont know what else to do. Any tips?

    Thanks!

    Comment by Matias — July 16, 2008 @ 5:12 am | Reply

  16. I’ve try setting the DYDL_LIBRARY_PATH too:
    export DYDL_LIBRARY_PATH=”/usr/local/oracle/instantclient_10_2″

    same error message…

    Comment by Matias — July 16, 2008 @ 5:15 am | Reply

  17. When you do “sudo ruby setup.rb” then sudo is not inheriting environment variables from your current environment (you can check this if you do “sudo env”).

    Therefore 1) either do “ruby setup.rb config” and “make” with your user (without sudo) and then afterwards just do “sudo make install” or 2) do at first “sudo su – root” and then set all necessary environment variables as root and then run “ruby setub.rb config”.

    Comment by Raimonds Simanovskis — July 16, 2008 @ 12:21 pm | Reply

  18. Worked perfectly for me, thanks a lot!

    Comment by Sébastien Briois — September 4, 2009 @ 10:49 am | Reply

  19. Hi there.
    Thank you for a great post. It was very helpfull.
    Anyone reading this post should bookmark this guys contents.

    I have a new PC and needed some installation help so i went over to http://www.InstallSoftware.com but they did not provide me with the in depth
    info this guy did. he kicks all the bigger sites’ butts.

    Thanks Again

    Comment by InstallSoftware — February 2, 2010 @ 5:25 am | Reply

  20. Пощелкал по рекламе. спасибо за пост

    Comment by Rengenx — March 13, 2010 @ 4:30 pm | Reply


RSS feed for comments on this post.

Leave a comment

Blog at WordPress.com.