Thursday, November 6, 2008

Connecting with Oracle from you ruby/rails applications on an "Intel Mac OS X" using DBI with OCI8

Prerequisite:

- Mac OS X (10.5.5)
- ruby --version => 1.8.6 (2008-03-03 patchlevel 114) [universal-darwin9.0]

NOTE: Here the machine where i am working is 'SBKislamzD1' and the user name is 'islamz'. In your case just replace this user name with your user name

Installing DBI

The easiest way of installing a gem is using gem command [gem install dbi] But here I am going to use OCI8-1.0.3. With this version of OCI8 library not all the version of DBI works. In my case DBI version 0.2.0 worked. You can't install this version (0.2.0) of DBI using gem install. You need to downloaded the tar file and unzip it.

Lets say the unzip folder is /Users/islamz/Downloads/dbi-0.2.2
Now open a terminal/console and goto /Users/islamz/Downloads/dbi-0.2.2 and run following commands:

> ruby setup.rb config --with=dbi
> ruby setup.rb setup

Now you should run the following command with 'sudo'

> sudo ruby setup.rb install

If you have any other version of DBI gem you might need to uninstall that gem

Installing Oracle Instant Client

At first from http://www.oracle.com/technology/software/tech/oci/instantclient/htdocs/intel_macsoft.html download following packages:

  • - Instant Client Package - Basic (10.2.0.4)
  • - Instant Client Package - SQL*Plus (10.2.0.4)
  • - Instant Client Package - SDK (10.2.0.4)

Now unzip all of packages in /Library/Oracle/instantclient_10_2. After unzipping you should have followings in this folder:
  • - BASIC_README
  • - classes12.jar
  • - genezi
  • - glogin.sql
  • - libclntsh.dylib.10.1
  • - libnnz10.dylib
  • - libocci.dylib.10.1
  • - libociei.dylib
  • - libocijdbc10.dylib
  • - libocijdbc10.jnilib
  • - libsqlplus.dylib
  • - libsqlplusic.dylib
  • - ojdbc14.jar
  • - sdk
  • - sqlplus
  • - SQLPLUS_README

Its now time to add some environment variables. For this purpose
open /etc/profile in edit mode and add the environment variables as stated below:
  • Open the file in edit mode using: sudo vi /etc/profile
  • Add following in this file:

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

  • Save the file and logoff then login
  • To test whether the environment variables are set or not type 'env' in terminal(console)

Now you need to create symlinks. To do this follow these steps:

  • open a terminal/console and goto /Library/Oracle/instantclient_10_2
  • sudo ln -s libclntsh.dylib.10.1 libclntsh.dylib
  • sudo ln -s libocci.dylib.10.1 libocci.dylib
  • in /Library/Oracle/instantclient_10_2 folder you will find two new symlink
Compiling ruby-oci8

To compile ruby-oci8 follow the steps as stated below:
  • download the ruby-oci8-1.0.3.tar If you are using Safari then it will be downloaded in /Users/islamz/Downloads/
  • unzip it to ruby-oci8-1.0.3 (/Users/islamz/Downloads/ruby-oci8-1.0.3)
  • run terminal/console and goto /Users/islamz/Downloads/ruby-oci8-1.0.3
  • export SQLPATH=$ORACLE_HOME
  • export RC_ARCHS=i386
  • ruby setup.rb config
  • make
  • sudo make install [Don't forget to run this command as 'sudo']
So, you are done. Now you need to test whether your compilation is okay or not.

You can easily test by following these steps:
  • run terminal/console
  • irb
  • require 'oci8' #it should return 'true'

If there is no error then you installation is SUCCESSFUL


SIDE NOTES

1) How to check which version of DBI is supported:

- goto /Library/Ruby/Site/1.8/DBD/OCI8/
- open the file OCI8.rb
- check the value of USED_DBD_VERSION. [In my case it was "0.2"]

2) Problems with NetBeans:

If you are using NetBeans for developing your project in that case you might face some problems:

a) You might get some permission related problems. So run Netbeans with sudo


b) When running your application if it throws an error that 'libclntsh.dylib.10.1' is not found in '/scratch/plebld/208/rdbms/lib/' then put the file in the specified path

c) When running your application you are getting this error:

OCIError (Error while trying to retrieve text for error ORA-24812):
lob.c:194:in oci8lib.so
/Library/Ruby/Site/1.8/oci8.rb:1005:in `write'

It means when you are running NetBeans as sudo the environment variables are missing. Follow these steps:

  • SBKislamzD1:~ islamz# sudo su
  • sh-3.2# ORACLE_HOME=/Library/Oracle/instantclient_10_2
  • sh-3.2# export ORACLE_HOME=$ORACLE_HOME
  • sh-3.2# export TNS_ADMIN=$ORACLE_HOME
  • sh-3.2# export LD_LIBRARY_PATH=$ORACLE_HOME
  • sh-3.2# export DYLD_LIBRARY_PATH=$ORACLE_HOME
  • sh-3.2# export NLS_LANG=AMERICAN_AMERICA.AL32UTF8
  • sh-3.2# /Applications/NetBeans/NetBeans\ 6.1.app/Contents/MacOS/netbeans

1 comment:

Bill Leeper said...

To get the Netbeans working follow the instructions I posted here:

http://wleeper.com/2010/07/20/setup-netbeans-on-osx-and-oracle-oci8-for-ruby-on-rails/

Good Luck