install cx_oracle for python

Each Answer to this Q is separated by one/two green lines.

Am on Debian 5, I’ve been trying to install cx_oracle module for python without any success. First, I installed oracle-xe-client and its dependency (followed tutorial in the following link here).

Then, I used the scripts in /usr/lib/oracle/xe/app/oracle/product/10.2.0/client/bin to populate environment variables such as PATH, ORACLE_HOME and NLS_LANG.

Once, this was completed, I tried to run:

sudo easy_install cx_oracle

But I keep getting the following error:

Searching for cx-oracle
Best match: cx-Oracle 5.0.4
Processing cx_Oracle-5.0.4.tar.gz
Running cx_Oracle-5.0.4/ -q bdist_egg --dist-dir /tmp/easy_install-xsylvG/cx_Oracle-5.0.4/egg-dist-tmp-8KoqIx
error: cannot locate an Oracle software installation

Any idea what I missed here?

The alternate way, that doesn’t require RPMs. You need to be root.

  1. Dependencies

    Install the following packages:

    apt-get install python-dev build-essential libaio1
  2. Download Instant Client for Linux x86-64

    Download the following files from Oracle’s download site:

    files preview

  3. Extract the zip files

    Unzip the downloaded zip files to some directory, I’m using:

  4. Add environment variables

    Create a file in /etc/profile.d/ that includes

    export ORACLE_HOME=/opt/ora/instantclient_11_2

    Create a file in /etc/ that includes


    Execute the following command

    sudo ldconfig

    Note: you may need to reboot to apply settings

  5. Create a symlink

    cd $ORACLE_HOME 
    ln -s
  6. Install cx_Oracle python package

    • You may install using pip

      pip install cx_Oracle
    • Or install manually

      Download the cx_Oracle source zip that corresponds with your Python and Oracle version. Then expand the archive, and run from the extracted directory:

      python build 
      python install

I recommend that you grab the rpm files and install them with alien. That way, you can later on run apt-get purge no-longer-needed.

In my case, the only env variable I needed is LD_LIBRARY_PATH, so I did:

echo export LD_LIBRARY_PATH=/usr/lib/oracle/11.2/client/lib >> ~/.bashrc
source ~/.bashrc

I suppose in your case that path variable will be /usr/lib/oracle/xe/app/oracle/product/10.2.0/client/lib.

The following worked for me, both on mac and Linux. This one command should download needed additional files, without need need to set environment variables.

python -m pip install cx_Oracle --pre

Note, the –pre option is for development and pre-release of the Oracle driver. As of this posting, it was grabbing cx_Oracle-6.0rc1.tar.gz, which was needed. (I’m using python 3.6)

Thx Burhan Khalid, I overlooked your “You need to be root” quote, but found the way when you are not the root here.

At point 7 you need to use:

sudo env ORACLE_HOME=$ORACLE_HOME python install 


sudo env ORACLE_HOME=/path/to/instantclient python install

Thanks Burhan Khalid. Your advice to make a a soft link make my installation finally work.

To recap:

  1. You need both the basic version and the SDK version of instant client

  2. You need to set both LD_LIBRARY_PATH and ORACLE_HOME

  3. You need to create a soft link (ln -s in my case)

None of this is documented anywhere, which is quite unbelievable and quite frustrating. I spent over 3 hours yesterday with failed builds because I didn’t know to create a soft link.

I think it may be the sudo has no access to get ORACLE_HOME.You can do like this.

sudo visudo

modify the text add

Defaults env_keep += “ORACLE_HOME”


sudo python build install

Alternatively you can install the cx_Oracle module without the PIP using the following steps

  1. Download the source from here
    [cx_Oracle-6.1.tar.gz ]
  2. Extract the tar using the following commands (Linux)

    gunzip cx_Oracle-6.1.tar.gz

    tar -xf cx_Oracle-6.1.tar

  3. cd cx_Oracle-6.1

  4. Build the module

    python build

  5. Install the module

    python install

This just worked for me on Ubuntu 16:

Download (‘’ and ‘’) from Oracle web site and then do following script (you can do piece by piece and I did as a ROOT):

apt-get install -y python-dev build-essential libaio1
mkdir -p /opt/ora/
cd /opt/ora/

## Now put 2 ZIP files:
# ('' and '') 
# into /opt/ora/ and unzip them -> both will be unzipped into 1 directory: /opt/ora/instantclient_12_2 

rm -rf /etc/profile.d/
echo "export ORACLE_HOME=/opt/ora/instantclient_12_2"  >> /etc/profile.d/
echo "export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ORACLE_HOME"  >> /etc/profile.d/
chmod 777 /etc/profile.d/
source /etc/profile.d/
env | grep -i ora  # This will check current ENVIRONMENT settings for Oracle

rm -rf /etc/
echo "/opt/ora/instantclient_12_2" >> /etc/
ls -lrth libclntsh*   # This will show which version of 'libclntsh' you have... --> needed for following line:

ln -s

pip install cx_Oracle   # Maybe not needed but I did it anyway (only pip install cx_Oracle without above steps did not work for me...)

Your python scripts are now ready to use ‘cx_Oracle’… Enjoy!

This worked for me

python -m pip install cx_Oracle --upgrade

For details refer to the oracle quick start guide

If you are trying to install in MAC , just unzip the Oracle client which you downloaded and place it into the folder where you written python scripts.
it will start working.

There is too much problem of setting up environmental variables.
It worked for me.

Hope this helps.


Try to reinstall it with the following code:

!pip install --proxy http://username:[email protected]:8080 --upgrade --force-reinstall cx_Oracle

If you require to install a specific version of cx_Oracle, like 7.3 which was the last version with support for Python 2, you can do the following:

python -m pip install cx_Oracle==7.3

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .