Install cx_Oracle 5.1.3 in Ubuntu 14.04 with oracle-instantclient12.1

To be able to install cx_Oracle 5.1.3 (to manipulate an Oracle 12c database with python) in Ubuntu 14.04 with oracle-instantclient12.1 succesfully, you have to do following things:

1. Download these instant client packages from http://www.oracle.com/technetwork/topics/linuxx86-64soft-092277.html:

oracle-instantclient12.1-tools-12.1.0.1.0-1.x86_64.rpm
oracle-instantclient12.1-odbc-12.1.0.1.0-1.x86_64.rpm
oracle-instantclient12.1-devel-12.1.0.1.0-1.x86_64.rpm
oracle-instantclient12.1-sqlplus-12.1.0.1.0-1.x86_64.rpm
oracle-instantclient12.1-jdbc-12.1.0.1.0-1.x86_64.rpm
oracle-instantclient12.1-basic-12.1.0.1.0-1.x86_64.rpm

(the packages that are in red are required)

2. Convert all rpm packages to deb using alien:

$ sudo alien -d *.rpm


3. Install all those deb packages:

$ sudo dpkg -i *.deb


4. Create the ORACLE_HOME environment variable:

Create a file: /etc/ld.so.conf.d/oracle.conf and in the first line of the script, write (it depends on your system):

/usr/lib/oracle/12.1/client64/lib

Run the commands:

$ export ORACLE_HOME=/usr/lib/oracle/12.1/client64 
$ export LD_LIBRARY_PATH=$ORACLE_HOME/lib
$ sudo ldconfig


5. Download the cx_Oracle tar ball from pypi and extract it:

$ tar -xzvf cx_Oracle-5.1.3.tar.gz
$ cd cx_Oracle-5.1.3

6. Modify these following lines of cx_Oracle-5.1.3/setup.py, from line 123 (the red ones):

# try to determine the Oracle home
#userOracleHome = os.environ.get("ORACLE_HOME")
userOracleHome = "/usr/lib/oracle/12.1/client64"
if userOracleHome is not None:
    if not CheckOracleHome(userOracleHome):
        messageFormat = "Oracle home (%s) does not refer to an " \
                "9i, 10g, 11g or 12c installation."
        raise DistutilsSetupError(messageFormat % userOracleHome)
else:
    for path in os.environ["PATH"].split(os.pathsep):
        if CheckOracleHome(path):
            break
    if oracleHome is None:
        #~ raise DistutilsSetupError("cannot locate an Oracle software " \
                #~ "installation
        oracleHome = "/usr/lib/oracle/12.1/client64"

7. Build and install cx_Oracle 5.1.3:

$ python setup.py build
$ sudo python setup.py install


8. Check the installation by import the cx_Oracle module in the python clie:

$ python

> import cx_Oracle




* Updates Sep 23, 2014: you are able to install cx_Oracle simply using pip instead of installing it from source:

(myvirtualenv)$ pip install cx_Oracle