Skip to content

Instantly share code, notes, and snippets.

@thom-nic
Created July 16, 2013 19:14
Show Gist options
  • Star 60 You must be signed in to star a gist
  • Fork 27 You must be signed in to fork a gist
  • 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

@YADAAB
Copy link

YADAAB commented Jun 13, 2015

Hi, this is the error message to be precise from log files:

Skipping link https://pypi.python.org/packages/2.7/c/cx_Oracle/cx_Oracle-5.1.3-11g.win-amd64-py2.7.exe#md5=940538b99a52e414c0c100cd629fce51 (from https://pypi.python.org/simple/cx-oracle/); unknown archive format: .exe

As I navigate to the specified location from my browser, I get a .exe file downloaded into my machine and I think Mac is not able to recognize the format.

@pravr
Copy link

pravr commented Aug 3, 2015

The sdk link points to the basiclite zip :) Please change it when you get time.

Thanks a lot for the set of instructions!

@geraldsousa
Copy link

Has anyone been able to get cx_Oracle 5.1.2 or 5.2 to install on OS X 10.11.1 El Capitan and Python 3 (3.4 or 3.5)?

@melarish
Copy link

If anyone else is still getting:
Complete output from command python setup.py egg_info: Traceback (most recent call last): File "<string>", line 20, in <module> File "/private/tmp/pip-build-7nKcf7/cx-Oracle/setup.py", line 170, in <module> raise DistutilsSetupError("cannot locate an Oracle software " \ distutils.errors.DistutilsSetupError: cannot locate an Oracle software installation ---------------------------------------- Command "python setup.py egg_info" failed with error code 1 in /private/tmp/pip-build-7nKcf7/cx-Oracle

Then do export PATH=$PATH:/usr/lib/share/oracle/instantclient_11_2 as well. Sorted it for me.

@gskluzacek
Copy link

@GCSousa -- I was able to install cx_Oracle 5.2 on El Capitan 10.11.2 (15C50) with Python 3.4.3. See my instructions and test code at the bottom of this post... I left all the files in my /Users/gskluzacek/Downloads directory as I just wanted to try this out.

I also install SQL Plus as I wanted to test the instant client prior to installing cx_Oracle

Note when I executed the step to install cx_Oracle using PIP I did get a couple of warnings. Additionally since I was using Python 3.4.3 I had to use pip3, e.g.:

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


You are using pip version 6.0.8, however version 7.1.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
Collecting cx-Oracle
Downloading cx_Oracle-5.2.tar.gz (108kB)
100% |################################| 110kB 1.7MB/s
Installing collected packages: cx-Oracle
Running setup.py install for cx-Oracle
building 'cx_Oracle' extension
/usr/bin/clang -fno-strict-aliasing -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -g -arch x86_64 -I/Users/gskluzacek/Downloads/instantclient_11_2/sdk/include -I/Library/Frameworks/Python.framework/Versions/3.4/include/python3.4m -c cx_Oracle.c -o build/temp.macosx-10.6-intel-3.4-11g/cx_Oracle.o -DBUILD_VERSION=5.2
In file included from cx_Oracle.c:203:
In file included from ./SessionPool.c:139:
In file included from ./Connection.c:765:
./Cursor.c:230:50: warning: comparison of constant 0 with boolean expression is always false [-Wtautological-constant-out-of-range-compare]
self->environment->encoding) < 0)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^ ~
In file included from cx_Oracle.c:203:
In file included from ./SessionPool.c:139:
In file included from ./Connection.c:765:
In file included from ./Cursor.c:246:
In file included from ./Variable.c:168:
./NumberVar.c:370:26: warning: absolute value function 'abs' given an argument of type 'long' but has parameter of type 'int' which may cause truncation of value [-Wabsolute-value]
length = numDigits + abs(scale) + 3;
^
./NumberVar.c:370:26: note: use function 'labs' instead
length = numDigits + abs(scale) + 3;
^~~
labs
2 warnings generated.
/usr/bin/clang -bundle -undefined dynamic_lookup -g -arch x86_64 build/temp.macosx-10.6-intel-3.4-11g/cx_Oracle.o -L/Users/gskluzacek/Downloads/instantclient_11_2 -lclntsh -o build/lib.macosx-10.6-intel-3.4-11g/cx_Oracle.so -shared-libgcc
Successfully installed cx-Oracle-5.2


-- 0 --

refer to links:

-- 1 --

downloaded from:
http://www.oracle.com/technetwork/topics/intel-macsoft-096467.html

  • Instant Client Package - Basic
  • Instant Client Package - SQL*Plus
  • Instant Client Package - SDK

Safari will automatically unzip the files into the following folders within the downloads directory:

  • instantclient_11_2
  • instantclient_11_2-2
  • instantclient_11_2-3

Place all files in the same directory by:

  1. moving the files from the instantclient_11_2-2 folder to the instantclient_11_2 folder
  2. moving the sdk folder in the instantclient_11_2-3 folder to the instantclient_11_2 folder

-- 2 --

create sym links

ln -s libclntsh.dylib.11.1 libclntsh.dylib
ln -s libocci.dylib.11.1 libocci.dylib

-- 3 --

need to set the following environment variables...

export ORACLE_HOME=< path to insta-client >
export DYLD_LIBRARY_PATH=$ORACLE_HOME
export LD_LIBRARY_PATH=$ORACLE_HOME
export VERSION=< version of insta-client, e.g.: 11.2.0.4.0 >
export ARCH=x86_64

-- 4 --

Test SQL Plus

example usage without setting up a TNS names file...

./sqlplus user/pwd@//host:port/sid

-- 5 --

install cs_Oracle with PIP

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

-- 6 --

Test by running the ora_test2.py script

python3 ora_test.py

note: the ora_test.py script was copied from the stackoverflow link given above
The connection string was modified for an actual oracle db
The table name was modified for an actual table on the said oracle db
The array indexes for the row array were modified for the number of columns in the table


import cx_Oracle

conn_str = '<< user id >>/<< password >>@//<< hostname or ip addr >>:<< port >>/<< service id >>'
conn = cx_Oracle.connect(conn_str)
c = conn.cursor()
c.execute('select * from << some table >>')
for row in c:
print(row[0], row[1], row[2], row[3], row[4], row[ << n >> ])
conn.close()

@eric314
Copy link

eric314 commented Jan 20, 2016

These instructions won't work exactly as written.

The key missing point is that the process of unzipping the basic and api .zip downloads will create its own folder which you must then rename to whatever you specified in $ORACLE_HOME.

I was unzipping into $ORACLE_HOME, which will not work, because the unzip process creates a new directory under it. It won't work this way.

If you wish $ORACLE_HOME to be /usr/local/lib/share/oracle, for example, do the following:

Unpack the archives into /usr/local/lib/share/
Rename the directory "instanttools" or whatever its called to "oracle"
Proceed with these directions and the pip install will work.

@floer32
Copy link

floer32 commented May 12, 2016

I may be repeating someone, but since this is such a mess and pain to deal with, I don't feel bad for being repetitious ... if it might help. :) All three of $ORACLE_HOME and $LD_LIBRARY_PATH and $DYLD_LIBRARY_PATH must be set, to successfully import and use the library. So put that in your rcfile etc. You'll know you missed this if you get the "image not found" error

@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