How To Install ruby-oci8 on Ubuntu Server 64bits

Ruby-oci8 is the Oracle connector for Ruby, using this library your Ruby programs can communicate with the Oracle database.

If you have not installed Ruby yet on your machine, please refer to the Ubuntu package on this post.

Install few additional Linux packages:

sudo apt-get install libaio-dev unzip

Create an Oracle folder:

sudo mkdir /opt/oracle

Go the Oracle website to download the Instant Client Basic and SDK for Linux 64 bits.
Download files in the Oracle folder you have created and unzip them:

cd /opt/oracle
unzip oracle-basic-11.zip
unzip oracle-sdk-11.zip

Once you have unzip the 2 zip files, go in the Instant Client folder and create a link for application that does not know Instant Client 11.

cd instantclient_11_2/
sudo ln -s libclntsh.so.11.1 libclntsh.so

Create the Oracle Instant Client system variable

export LD_LIBRARY_PATH=/opt/oracle/instantclient_11_2

Then, install ruby-oci8:

sudo env LD_LIBRARY_PATH=/opt/oracle/instantclient_11_2 /usr/bin/gem install ruby-oci8

Try to connect to the database, create a Ruby file for example sql.rb and copy/paste the following code:

require ‘rubygems’
require ‘oci8′
tnsnames = ‘(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = host_name_or_ip)(PORT = 1521)) (CONNECT_DATA = (SID = SID)))’
conn = OCI8.new(‘user’, ‘password’, tnsnames)
cursor = conn.exec(‘SELECT sysdate FROM dual’)
while r = cursor.fetch()
puts r.join(‘,’)
end
cursor.close
conn.logoff

And run it:

ruby sql.rb
Fri Sep 24 08:28:00 +0900 2010

Enjoy and have fun with Ruby and Oracle.

Error messages:

sql:1:in `require’: no such file to load — oci8 (LoadError)

you forgot to put the below line before require ‘oci8′

require ‘rubygems’

or

you can also create an system variable that will automatically use rubygems:

RUBYOPT=”rubygems”
export RUBYOPT

/var/lib/gems/1.8/gems/ruby-oci8-2.0.4/lib/oci8lib_18.so: libclntsh.so.11.1: cannot open shared object file: No such file or directory – /var/lib/gems/1.8/gems/ruby-oci8-2.0.4/lib/oci8lib_18.so (LoadError)

The LD_LIBRARY_PATH variable is not well configured.

8 Responses

Leave a Reply