Skip to content

Instantly share code, notes, and snippets.

@thom-nic
Created July 16, 2013 19:14
Show Gist options
  • Save thom-nic/6011715 to your computer and use it in GitHub Desktop.
Save thom-nic/6011715 to your computer and use it in GitHub Desktop.
Installing CX Oracle for Python & Mac OS X. Instructions exist around the web, but they seem to be piecemeal and incomplete.

Installing cx_Oracle for Python/ Mac OSX

Portions taken from http://www.cs.utexas.edu/~mitra/csSpring2011/cs327/cx_mac.html (in case that link ever dies.)

Assume you've got homebrew installed.

Download the following files from Oracle

Create a directory /usr/lib/share/oracle

export ORACLE_HOME=/usr/lib/share/oracle
export VERSION=11.2.0.3.0
export ARCH=x86_64

mkdir -p $ORACLE_HOME

Unpack both files to that directory:

cd $ORACLE_HOME
tar -xzf instantclient-basic-$VERSION-macosx-x64.zip
tar -xzf instantclient-sdk-$VERSION-macosx-x64.zip

ln -s libclntsh.dylib.11.2 libclntsh.dylib
ln -s libocci.dylib.11.2 libocci.dylib

export DYLD_LIBRARY_PATH=$ORACLE_HOME
export LD_LIBRARY_PATH=$ORACLE_HOME

(Note I did not have to do anything with ottclasses.zip as suggested in the original utexas instructions.)

If you're using Pip:

Last step is to simply run pip, you might have to add an arch flag:

env ARCHFLAGS="-arch $ARCH" pip install cx_Oracle

If you're building from source:

Download cx_Oracle-version.tar.gz from Sourceforge

export CX_ORA_VERSION=5.1.2
tar -xzf cx_Oracle-$CX_ORA_VERSION.tar.gz
cd cx_Oracle-$CX_ORA_VERSION

env ARCHFLAGS="-arch $ARCH" python setup.py build
sudo python setup.py install

Notes

Some folks have recommended compiling for 32-bit (i386) instead, although I'm not sure why. 64 bit seems to work for me so YMMV.

Basically:

# compile cx_Oracle for 32-bit (requires downloading 32-bit instantclient sources)
ARCHFLAGS="-arch i386" pip install cx_Oracle

# then make your virtualenv python 32-bit only:
virtualenv .
mv bin/python python.fat
lipo python.fat -remove x86_64 -output bin/python

See: http://stackoverflow.com/questions/8169946/cant-get-cx-oracle-to-work-with-python-version-2-7-mac-os-10-7-2-lion-mis/8452731#8452731

@floer32
Copy link

floer32 commented May 12, 2016

Also, here was my slightly adjusted one-stop script ... I kept tweaking until I got it working and this is what I wound up with. It assumes you've put the zips in the same directory as where you are running the script.

set -e

export ORACLE_HOME_PARENT=/usr/lib/share/
export ORACLE_HOME=$ORACLE_HOME_PARENT/oracle
export VERSION=11.2.0.4.0
export ARCH=x86_64

mkdir -p $ORACLE_HOME_PARENT

unzip instantclient-basic-macos.x64-"$VERSION".zip -d /tmp/oracle
unzip instantclient-sdk-macos.x64-"$VERSION".zip -d /tmp/oracle

mv /tmp/oracle/instantclient_* /tmp/oracle_actually
mv /tmp/oracle_actually $ORACLE_HOME

cd $ORACLE_HOME

# yes your version is PROBABLY 11.2+ ... AND YET for some reason 11.1 is supposed to be used here ... I don't even know
# it didn't work for me until I changed to use 11.1 here, not 11.2
ln -s libclntsh.dylib.11.1 libclntsh.dylib
ln -s libocci.dylib.11.1 libocci.dylib

export DYLD_LIBRARY_PATH=$ORACLE_HOME
export LD_LIBRARY_PATH=$ORACLE_HOME

cd -
  • move the req'd zips into the current directory before running
  • run the script
  • after you run this script you have two more steps to install the lib
    • system-wide installation: env CFLAGS="-Qunused-arguments" ARCHFLAGS="-arch $ARCH" sudo pip install cx_Oracle
    • virtualenv installation: activate your virtualenv and then run same command, WITHOUT sudo

@mcescalante
Copy link

mcescalante commented May 12, 2016

I used these instructions as reference while installing cx_Oracle today to El Capitan (10.11), and as some others have commented, they're a bit outdated. My main fixes to the instructions:

  • Shouldn't recommend /usr/lib anymore in 10.11 because of SIP. Proper location is /usr/local/lib
  • Download links don't work (users can easily make a throwaway account on Oracle and install themselves)
  • tar commands here make you do some shuffling once unzipped (moving files around)
  • libclntsh.dylib.11.2 and libocci.dylib.11.2 have been changed to 11.1

I put together an updated set (tested & working) of instructions based on my above corrections here: https://gist.github.com/mcescalante/50f1663825fea582953c187f83c8b92d

@jobforlife
Copy link

jobforlife commented Aug 15, 2016

I tried to install cx_oracle 5.2.1 instanceclient 12.1.0.2 on python2.7 on OS X EI Captain. Below is ok
Download three packages:  
instantclient-basic-macos.x64-12.1.0.2.0.zip
     instantclient-sqlplus-macos.x64-12.1.0.2.0.zip
    instantclient-sdk-macos.x64-12.1.0.2.0.zip
mkdir -p ~/oracle
cp instantclient_-12.1.0.2.0.zip ~/oracle
cd /oracle/instantclient_12_1
_Configure enviroment variables*
export ORACLE_HOME=
/oracle/instantclient_12_1
export DYLD_LIBRARY_PATH=$ORACLE_HOME  
export LD_LIBRARY_PATH=$ORACLE_HOME
export PATH=$PATH:$ORACLE_HOME

add tnsnames.ora under $ORACLE_HOME/network/admin directory, create the dir at first and then add tnsnames.ora file

Prepare for cx_Oracle
cd $ORACLE_HOME/sdk
unzip ottclasses.zip  
cd ..  
cp -R ./sdk/_ .  
cp -R ./sdk/include/_ .  
ln -s libclntsh.dylib.12.1 libclntsh.dylib  
ln -s libocci.dylib.12.1 libocci.dylib

mkdir -p ~/lib
ln -s $ORACLE_HOME/{libclntsh.dylib.12.1,libnnz12.dylib,libociei.dylib} ~/lib/
ln -s $ORACLE_HOME/{libsqlplus.dylib,libsqlplusic.dylib} ~/lib/

Install cx_Oracle
sudo pip install --no-cache-dir --allow-external --allow-unverified cx_Oracle 
now cx_oracle has been installed, but it not runnable.

Fix issues
If you import cx_oracle in python, it throw exception:
import cx_Oracle  

错误信息如下:  

Traceback (most recent call last):  
  File "ex1.py", line 1, in   
    import cx_Oracle  
ImportError: dlopen(/Library/Python/2.7/site-packages/cx_Oracle.so, 2): Library not loaded: @rpath/libclntsh.dylib.11.1  
  Referenced from: /Library/Python/2.7/site-packages/cx_Oracle.so  
  Reason: image not found  

You can fix it by below 2 steps
curl -O https://raw.githubusercontent.com/kubo/fix_oralib_osx/master/fix_oralib.rb
sudo ruby fix_oralib.rb --ic_dir $ORACLE_HOME /Library/Python/2.7/site-packages/cx_Oracle.so
It is ok now

It takes me 2 days to do it

@bingxiao
Copy link

bingxiao commented Aug 19, 2016

@jobforlife fix_oralib.rb saved my day. I was installing with instantclient_12_1, python3.5.2 on El Capitan.

@mbucknell
Copy link

mbucknell commented Aug 24, 2016

Red herring. The problem is not cx_oracle

@connerxyz
Copy link

Why do you mention "Assume you've got homebrew installed."?

@zexinzhang
Copy link

Finally, thank you, works for me on os x EI Capitan. I've tried all kinds of tutorials for about one week, only this one worked. Thank you again.

@mzavyalov
Copy link

I managed to install instant clients as per this guide and installed cx_Oracle with pip.

But while importing in python I get the following error


Python 3.6.0 (default, Mar  4 2017, 12:32:34)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cx_Oracle
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: dlopen(/usr/local/lib/python3.6/site-packages/cx_Oracle.cpython-36m-darwin.so, 2): Symbol not found: _OCIBindByName2
  Referenced from: /usr/local/lib/python3.6/site-packages/cx_Oracle.cpython-36m-darwin.so
  Expected in: flat namespace
 in /usr/local/lib/python3.6/site-packages/cx_Oracle.cpython-36m-darwin.so
>>>

Googled it, but couldn't find any similar issues

@haio
Copy link

haio commented May 8, 2017

@jobforlife you save my day 👍

@NickStefan
Copy link

THANK YOU! its like they dont want anyone to use it...

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