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.