Programmers' Pain
10Jul/114

Howto install cx-oracle on CentOS 5.x

Python Logo (http://www.python.org/)

Some Python scripts that I’ve written have to communicate with an Oracle Database. To be able to do so you have to install a Python module that implements the needed Oracle bindings against the Python Database API. Usually those modules aren’t part of recent Linux distributions due to license restrictions. Since I haven’t really found some easy and straight forward howto or tutorial how to install the cx-oracle Python extension module on CentOS 5.x I decided to write my own little tutorial.

Since cx-oracle is build against libraries provided by Oracle you have to install Oracle’s Instant Client first. There’s no need to install the full Oracle client package containing sqlplus and other things if you only need it for Python purposes. Download the RPM package that matches the architecture of your CentOS-Installation and the version of your Oracle database. Than use yum to install the RPM package:

yum --nogpgcheck install oracle-instantclient-basic-10.2.0.3-1.x86_64.rpm

In case the Instant Client reports missing dependencies yum will add them to the list of to be installed packages automatically. Afterwards you have to install the cx-oracle Python extension module via downloading the RPM package that matches the architecture of your CentOS-Installation and the version of your Oracle database. Again use yum to install the RPM package:

yum --nogpgcheck install cx_Oracle-5.0-10g-py24-1.x86_64.rpm

Now you’re ready to use the Oracle bindings in Python. But if you try to use the module in your Python shell you’ll see the following error:

Python 2.4.3 (#1, Mar  5 2011, 21:26:05)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-50)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cx_Oracle
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ImportError: libclntsh.so.10.1: cannot open shared object file: No such file or directory
>>>

The problem is that the just installed libraries of the Oracle Instant Client aren’t part of the default library path of your CentOS installation. Either you have to extend the LD_LIBRARY_PATH Bash variable of your current user or you have to add the lib directory of the Instant Client installation to the system-wide library path if all users should be allowed to use the Oracle Instant Client. To do so you have to create a new file, e.g. oracle.conf, in the /etc/ld.so.conf.d directory with the following content:

/usr/lib/oracle/10.2.0.3/client64/lib/

This tells ldconfig to also look for libraries in the lib folder of the Instant Client installation. To update the library cache just call ldconfig without any parameter. This will take a while since ldconfig will re-read every configured library folder and add its content to the library cache. The new oracle.conf file has to be owned by the root user as well as ldconfig has to be called as the root user. Afterwards so should be able to use the cx_Oracle module in your Python shell:

Python 2.4.3 (#1, Mar  5 2011, 21:26:05)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-50)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cx_Oracle
>>>
Comments (4) Trackbacks (1)
  1. Hi,

    I followed the steps you mentioned.

    When I import cx_Oracle in python, it says no module found.

    Any thoughts please?

    Thanks

    • Hi! Did yum report any error while installing the cx_Oracle rpm? It’s kind of odd that the installation of that Python module did work without any problems but rasing an ‘No module named cx_Oracle’-error while trying to use it. Unless you don’t use a different Python version at runtime or something like that.

  2. I can install package cx_Oracle with Oracle 11g client on CentOS 6.3 (Final) with

    rpm -ivh cx_Oracle-5.1-11g-py26-1.i386.rpm

    [root@tk-dfe ~]# python
    Python 2.6.6 (r266:84292, Jun 18 2012, 14:10:23)
    [GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2
    Type “help”, “copyright”, “credits” or “license” for more information.
    >>> import cx_Oracle
    >>> dir(cx_Oracle)
    [‘ATTR_PURITY_DEFAULT’, ‘ATTR_PURITY_NEW’, ‘ATTR_PURITY_SELF’, ‘BFILE’, ‘BINARY’, ‘BLOB’, ‘Binary’, ‘CLOB’, ‘CURSOR’, ‘Connection’, ‘Cursor’, ‘DATETIME’, ‘DBSHUTDOWN_ABORT’, ‘DBSHUTDOWN_FINAL’, ‘DBSHUTDOWN_IMMEDIATE’, ‘DBSHUTDOWN_TRANSACTIONAL’, ‘DBSHUTDOWN_TRANSACTIONAL_LOCAL’, ‘DataError’, ‘DatabaseError’, ‘Date’, ‘DateFromTicks’, ‘EVENT_DEREG’, ‘EVENT_NONE’, ‘EVENT_OBJCHANGE’, ‘EVENT_SHUTDOWN’, ‘EVENT_SHUTDOWN_ANY’, ‘EVENT_STARTUP’, ‘Error’, ‘FIXED_CHAR’, ‘FIXED_UNICODE’, ‘FNCODE_BINDBYNAME’, ‘FNCODE_BINDBYPOS’, ‘FNCODE_DEFINEBYPOS’, ‘FNCODE_STMTEXECUTE’, ‘FNCODE_STMTFETCH’, ‘FNCODE_STMTPREPARE’, ‘INTERVAL’, ‘IntegrityError’, ‘InterfaceError’, ‘InternalError’, ‘LOB’, ‘LONG_BINARY’, ‘LONG_STRING’, ‘NATIVE_FLOAT’, ‘NCLOB’, ‘NUMBER’, ‘NotSupportedError’, ‘OBJECT’, ‘OPCODE_ALLOPS’, ‘OPCODE_ALLROWS’, ‘OPCODE_ALTER’, ‘OPCODE_DELETE’, ‘OPCODE_DROP’, ‘OPCODE_INSERT’, ‘OPCODE_UPDATE’, ‘OperationalError’, ‘PRELIM_AUTH’, ‘ProgrammingError’, ‘ROWID’, ‘SPOOL_ATTRVAL_FORCEGET’, ‘SPOOL_ATTRVAL_NOWAIT’, ‘SPOOL_ATTRVAL_WAIT’, ‘STRING’, ‘SUBSCR_NAMESPACE_DBCHANGE’, ‘SUBSCR_PROTO_HTTP’, ‘SUBSCR_PROTO_MAIL’, ‘SUBSCR_PROTO_OCI’, ‘SUBSCR_PROTO_SERVER’, ‘SYSDBA’, ‘SYSOPER’, ‘SessionPool’, ‘TIMESTAMP’, ‘Time’, ‘TimeFromTicks’, ‘Timestamp’, ‘TimestampFromTicks’, ‘UCBTYPE_ENTRY’, ‘UCBTYPE_EXIT’, ‘UCBTYPE_REPLACE’, ‘UNICODE’, ‘Warning’, ‘_Error’, ‘__doc__’, ‘__file__’, ‘__name__’, ‘__package__’, ‘apilevel’, ‘buildtime’, ‘clientversion’, ‘connect’, ‘makedsn’, ‘paramstyle’, ‘threadsafety’, ‘version’]
    >>>

    work well done ^^’

    http://cx-oracle.sourceforge.net/

  3. worked perfectly. i didn’t see the ldconfig step at first… maybe a tag might help.

    very helpful, thanks


Leave a comment

Connect with Facebook