Skip to content

Instantly share code, notes, and snippets.

@kimus
Last active April 28, 2024 10:54
Show Gist options
  • Save kimus/10012910 to your computer and use it in GitHub Desktop.
Save kimus/10012910 to your computer and use it in GitHub Desktop.
Installing python cx_oracle on Ubuntu

First of all, it just seems like doing anything with Oracle is obnoxiously painful for no good reason. It's the nature of the beast I suppose. cx_oracle is a python module that allows you to connect to an Oracle Database and issue queries, inserts, updates..usual jazz.

Linux

Step 1:

sudo apt-get install build-essential unzip python-dev libaio-dev

Step 2. Click here to download the appropriate zip files required for this. You'll need:

  • instantclient-basic-linux
  • instantclient-sdk-linux

Get the appropriate version for your system.. x86 vs 64 etc. Make sure you don't get version 12, since it's not supported by the cx_Oracle moduel yet.

Unzip the content in the same location, so you'll end up with a folder named: instantclient_11_2 (in my case) which will contain a bunch of .so and jar files.

just for ease of use I'll use $ORACLE_HOME which will basically point to the location where you unzipped your installclient folders.

export ORACLE_HOME=$(pwd)/instantclient_11_2

Step 3. create a symlink to your SO file.

cd $ORACLE_HOME
ln -s libclntsh.so.11.1   libclntsh.so  #the version number on your .so file might be different

Step 4. Update your /etc/profile or your ~/.bashrc

export ORACLE_HOME=/location/of/your/files/instantclient_11_2
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ORACLE_HOME

Step 5: Edit /etc/ld.so.conf.d/oracle.conf

This is a new file, simple add the location of your .so files here, then update the ldpath using

sudo ldconfig

Step 6. Finaly just install cx_oracle module:

pip install cx_oracle

Mac OS X

Step 1:
install Homebower for the compilation tools

Step 2. Click here to download the appropriate zip files required for this. You'll need:

  • instantclient-basic-macosx
  • instantclient-sdk-macosx

Get the appropriate version for your system.. x32 vs x64 etc. Make sure you don't get version 12, since it's not supported by the cx_Oracle moduel yet.

Unzip the content in the same location, so you'll end up with a folder named: instantclient_11_2 (in my case) which will contain a bunch of .so and jar files.

just for ease of use I'll use $ORACLE_HOME which will basically point to the location where you unzipped your installclient folders.

export ORACLE_HOME=$(pwd)/instantclient_11_2

Step 3. create a symlink to your SO file.

cd $ORACLE_HOME
ln -s libclntsh.dylib.11.2 libclntsh.dylib  #the version number on your .so file might be different
ln -s libocci.dylib.11.2 libocci.dylib

Step 4. Update your /etc/profile or your ~/.bashrc

export ORACLE_HOME=/location/of/your/files/instantclient_11_2
export DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:$ORACLE_HOME
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ORACLE_HOME

Step 5. Finaly just install cx_oracle module:

pip install cx_oracle
@cjpit
Copy link

cjpit commented May 29, 2017

@avandesa simply create that file, and just put the location where you installed the instant client oracle_home dir (that directory has all the .so files in it)
so it might be something like /home/me/oracle

@xarpy
Copy link

xarpy commented Jun 18, 2020

If you are using ubuntu 20.04, do not necessarily need install the python-dev package, because python version 2 has already been deprecated. Continue with the rest of the settings for linux, which will work, and don't forget the detail said by colleague @cjpit.

@ashaydeshmukh
Copy link

I am getting below error while "pip install cx_oracle"

Downloading/unpacking cx-oracle
Cannot fetch index base URL https://pypi.python.org/simple/
Could not find any downloads that satisfy the requirement cx-oracle
Cleaning up...
No distributions at all found for cx-oracle
Storing debug log for failure in /home/emgda/.pip/pip.log

@kimus
Copy link
Author

kimus commented Nov 9, 2021

It appears you cannot now install cx-oracle via pip without allowing "insecure and unverifiable" files:

Can you please try:

$ pip install cx_oracle --allow-external cx_oracle --allow-unverified

@valldriz
Copy link

valldriz commented Dec 5, 2022

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment