Random Technology I'm working with

Friday, October 24, 2008

Install Oracle Instant Client 11.1 and cx_Oracle 4.4 on Ubuntu 8.04

Purpose
I'm working on building a python app that needs to connect to an existing Oracle Database. The server it will be deployed on is Ubuntu 8.04 Server - 32 bit. No gui is installed. When I started down the road of installing Oracle Instant Client and cx_Oracle, I ran into several problems. I was able to find bits and pieces on the web that helped me along, but I did not find an all in one document. I'm sure there are some out there, but my Google-fu failed me.

Disclaimer

This works with Ubuntu 8.04, Oracle Instant Client 11.1, and cx_Oracle 4.4. It should work in a similar manner with other versions, but I have not tested anything but this configuration.

Step 1 - Install Prerequisite Software

Start out by becoming root. If you don't like to be root, that's fine, just prepend all commands with sudo.

bmorgan@muddy:~$ sudo su -


You will need to have the following packages installed to get Oracle Instant Client and cx_Oracle installed.
  • build-essential

  • unzip

  • python-dev

  • libaio-dev
root@muddy:~# apt-get install build-essential unzip python-dev libaio-dev


Step 2 - Get, Install and Configure Oracle Instant Client 11.1

Make a directory to install Oracle Instant Client into. I like to use /usr/local/oracle, but feel free to use whatever you want.

root@muddy:~# mkdir /usr/local/oracle


Download the Basic and the SDK packages from http://www.oracle.com/technology/tech/oci/instantclient/instantclient.html.

extract the files to the directory you created earlier

root@muddy:~# unzip -d /usr/local/oracle basic.zip
root@muddy:~# unzip -d /usr/local/oracle sdk.zip


Make a couple of symbolic links in the instantclient directory that you unzipped the instant client into.
Don't skip this step. If you do, cx_Oracle will not compile correctly

root@muddy:~# cd /usr/local/oracle/instantclient_11_1
root@muddy:/usr/local/oracle/instantclient_11_1# ln -s libclntsh.so.11.1 libclntsh.so
root@muddy:/usr/local/oracle/instantclient_11_1# ln -s libocci.so.11.1 libocci.so


Create a file called oracle.conf in the /etc/ld.so.conf.d directory and add the path that the instant client installed into. For example, mine is /usr/local/oracle/instantclient_11_1

root@muddy:~# echo /usr/local/oracle/instantclient_11_1 > /etc/ld.so.conf.d/oracle.conf
root@muddy:~# ldconfig


Step 3 - Get, Install and Configure cx_Oracle 4.4

Download cx_Oracle source from http://sourceforge.net/projects/cx-oracle/
Be sure to grab the source file. It will end in .tar.gz

Extract the file once downloaded.

root@muddy:~# tar -zxvf cx_Oracle-4.4.tar.gz


Change into the extracted directory.

root@muddy:~# cd cx_Oracle-4.4


set your ORACLE_HOME environment var to the directory that you installed the instant client into

root@muddy:~/cx_Oracle-4.4# export ORACLE_HOME=/usr/local/oracle/instantclient_11_1


Compile cx_Oracle.

root@muddy:~/cx_Oracle-4.4# python setup.py build


You might get an error like this:

/usr/bin/ld: cannot find -lclntsh
collect2: ld returned 1 exit status
error: command 'gcc' failed with exit status 1


If you did, it's because you skipped the symlinks I told you about in step 2.

root@muddy:/usr/local/oracle/instantclient_11_1# ln -s libclntsh.so.11.1 libclntsh.so
root@muddy:/usr/local/oracle/instantclient_11_1# ln -s libocci.so.11.1 libocci.so


run the build again, and it should complete.

Install it

root@muddy:~/cx_Oracle-4.4# python setup.py install


If the build completes without error, fire up the python interactive interpreter

root@muddy:~/cx_Oracle-4.4# python

>>> import cx_Oracle

if you get an error like the following, it's because you forgot to install the libaio-dev package in step 1.

Traceback (most recent call last):
File "", line 1, in
ImportError: libaio.so.1: cannot open shared object file: No such file or directory

root@muddy:~/cx_Oracle-4.4# apt-get install libaio-dev


Step 4 - Final System Configuration

Create a file in /etc/profile.d called oracle.sh and set the environment variables required by Oracle in it.

root@muddy:~# echo export ORACLE_HOME=/usr/local/oracle/instantclient_11_1 >> /etc/profile.d/oracle.sh
root@muddy:~# echo export TNS_ADMIN=\$ORACLE_HOME >> /etc/profile.d/oracle.sh

If you don't want ORACLE_HOME and TNS_ADMIN to bet set at a system wide level, you can skip this step and set those values in your ~/.profile file, or export them some other way.
They **DO** have to be set or cx_Oracle will not be happy.

Put your tnsnames.ora file in the directory that you assigned to TNS_ADMIN above. I set that to be the same directory I installed the instant client in, but it's totally up to you.
Just be sure that the TNS_ADMIN environment variable matches the path that you put it in.

If you created the /etc/profile.d/oracle.sh file above, reboot - This is so the new profile will take effect system wide

Once your machine is back up and you have your tnsnames.ora file in place test it out by opening the interactive python shell and making a connection to your database.

root@muddy:/usr/local/oracle/instantclient_11_1# python
Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cx_Oracle
>>> con = cx_Oracle.Connection('username','password','SID')
>>> con
>>>


Conclusion

That's it. Now you can use cx_Oracle in your python project.
If you run into problems using this guide, please let me know. I'd like to build an all in one comprehensive doc that can get a complete newbie up and running with no fuss.

9 comments:

Shad Sharma said...

Thanks. Helped a lot!

Jim Green said...

These instructions worked perfectly for cx_Oracle 5.0.4 with version 11.2 of instant client on Solaris 10. Many thanks for posting this step by step guide as I could not find good install documentation anywhere.

Unknown said...

magic this worked after a lot of hairpulling and bad explanations

clickclack said...

I used your instructions to install cx_Oracle-5.1 on Red Hat Linux for python2 and python3. Thank you!

Andrea said...

Thanks a lot for sharing the installation steps, after lot of trouble and wasting so much of time finally I just followed your steps and successfully installed the client. No body has shared the whole installation process in such a good way as you have listed it.
sap ecc 6

Anonymous said...

Hi, can somebody tell me where to get those *.ora files? I tried to create manually from some example, but some doc said to run "lsnrctl reload" after changing listener.ora, but I don't have such bin on Linux
http://www.oracle-base.com/articles/misc/OracleNetworkConfiguration.php

I got the error like
ORA-12505:
TNS:listener does not currently know of SID given in connect descriptor

or

ORA-12154:
TNS:could not resolve the connect identifier specified

Dan said...

Very good - thanks!

Similar to what I had found in my install last week, but I like that you have stressed the prerequisites and symlinks. Not sure why Oracle made it so difficult.

Here's my earlier documentation: Install Oracle Client on Ubuntu - Dashdrum.

Matheus Garcia said...

For ORA-12154:
TNS:could not resolve the connect identifier specified

Just make sure TNS_ADMIN is point to the directory which has the tnsnames.ora file:

echo $TNS_ADMIN

Anonymous said...

Hi,
i have installed oracle 10g on Ubuntu 12.04 server edition,now i was trying to install oracle 10g application server but while installing i am getting the following types of errors:
/u01/Oracle10gAS/opmn/bin/opmn: error while loading shared libraries: libclntsh.so.10.1: cannot open shared object file: No such file or directory
opmnctl: opmn is not running

ADMN-705002
The most likely causes are:
1. TaskMaster cannot read configuration files, such as $ORACLE_HOME/opmn/conf/opmn.xml, and $ORACLE_HOME/dcm/dcm.conf, etc.
2. Cannot connect to the repository