Showing posts with label CX_Oracle. Show all posts
Showing posts with label CX_Oracle. Show all posts

Tuesday, May 10, 2016

Installing CX_oracle in CentOS 7.1

Before you begin, make sure you have "gcc" installed in CentOS box by typing:

# yum install gcc

Installing Python's Oracle client CX_oracle  is not as easy as:

$ pip install CX_oracle

If you run the command on a fresh CentOS 7.1 installation you will get an error like this:


distutils.errors.DistutilsSetupError: cannot locate an Oracle software installation

So we need to install Oracle's client on our linux box. Easiest way to do this, is to download & install Oracle's Instant Client (you will need an Oracle login to do that). Pick the "Instant Client for Linux x86-64" for your CentOS installation. You will need "basic", "devel" (to get libraries to compile CX_oracle) and "sqlplus" (to do some testing that your client installation works). When presented with the options, get the RPM versions. As of this writing, latest version are:


oracle-instantclient12.1-basic-12.1.0.2.0-1.x86_64.rpm
oracle-instantclient12.1-devel-12.1.0.2.0-1.x86_64.rpm
oracle-instantclient12.1-sqlplus-12.1.0.2.0-1.x86_64.rpm

Then, install it as a root by running:

# rpm -ivh oracle-instantclient12.1-*.rpm 

You should get something like this:

Preparing...                          ################################# [100%]
Updating / installing...
   1:oracle-instantclient12.1-basic-12################################# [ 33%]
   2:oracle-instantclient12.1-devel-12################################# [ 67%]
   3:oracle-instantclient12.1-sqlplus-################################# [100%]

When done, Oracle's Instant Client will install here: 

/usr/lib/oracle/12.1/client64/

First time you try to run "sqlplus" by typing

# sqlplus64 

You will get an error like this:

sqlplus64: error while loading shared libraries: libsqlplus.so: cannot open shared object file: No such file or directory

This is Oracle telling you it can't find appropriate libraries. To fix this and setup additional Oracle stuff in your environment, create a file named "/etc/profile.d/oracle.sh" with this content:

ORACLE_HOME=/usr/lib/oracle/12.1/client64
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ORACLE_HOME/lib
PATH=$PATH:$ORACLE_HOME/bin
export ORACLE_HOME LD_LIBRARY_PATH PATH

Restar your session (logout and login again, no need to reboot) and try your client's installation by running sqlplus:

# sqlplus

SQL*Plus: Release 12.1.0.2.0 Production on Tue May 10 10:26:23 2016

Copyright (c) 1982, 2014, Oracle.  All rights reserved.

Enter user-name: system@"192.168.16.67:1521/XE"
Enter password: 

Connected to:
Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production

SQL> select 1 from dual;

1
----------
1

SQL> quit  
Disconnected from Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production

Replace "192.168.16.67:1521/XE" with the corresponding Oracle database connection parameters. If connection was successful, you are all set to install "CX_oracle".

Try again running:

$ pip install CX_oracle

VoilĂ !

# pip install CX_oracle
Downloading/unpacking CX-oracle
  Downloading cx_Oracle-5.2.1.tar.gz (113kB): 113kB downloaded
  Running setup.py egg_info for package CX-oracle
    
Installing collected packages: CX-oracle
  Running setup.py install for CX-oracle
    building 'cx_Oracle' extension
    gcc -pthread -fno-strict-aliasing -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -I/usr/include/oracle/12.1/client64 -I/usr/include/python2.7 -c cx_Oracle.c -o build/temp.linux-x86_64-2.7-12c/cx_Oracle.o -DBUILD_VERSION=5.2.1
    gcc -pthread -shared -Wl,-z,relro build/temp.linux-x86_64-2.7-12c/cx_Oracle.o -L/usr/lib/oracle/12.1/client64/lib -L/usr/lib64 -lclntsh -lpython2.7 -o build/lib.linux-x86_64-2.7-12c/cx_Oracle.so
    
Successfully installed CX-oracle
Cleaning up...

You can test your CX_oracle installation with this piece of code:

import cx_Oracle
db = cx_Oracle.connect('system', '******', '192.168.16.67:1521/XE')
cursor = db.cursor()
st = cursor.execute("select 1 from dual")
all = st.fetchall()
print(all)

When run, the above code should print:

[(1,)]