Thursday, June 12, 2008

cx_Oracle and Oracle XE on Ubuntu

Simplifying options have appeared since the last time I posted about installing cx_Oracle against Oracle XE on Ubuntu, so I thought I'd produce an updated, simplified summary.
  1. Install Oracle XE from Oracle's own repository of .deb files
  2. export ORACLE_HOME=/usr/lib/oracle/xe/app/oracle/product/10.2.0/server/
    export PATH=$ORACLE_HOME/bin:$PATH
  3. sudo apt-get install python-dev python-setuptools
    sudo -E easy_install cx_Oracle

    The -E flag carries your environment variables into your sudo session; if you omit it, you may get a cannot locate an Oracle software installation error.
  4. Test:
    $ python
    >>> import cx_Oracle
    >>> cnxn = cx_Oracle.connect('system/mysystempassword@xe')
    >>> crsr = cnxn.cursor()
    >>> crsr.execute('SELECT * FROM dual')


Kevin said...

oh god you rock so much....i appreciate the time that you take to put this stuff up there...there is nothing better than shared knowledge :)

Vakond1 said...

Dear Catherine,
Thank you for your instructions, but I have to make a small corrections, because the instructions don't work as posted:

Instead of:
export ORACLE_HOME=/usr/lib/oracle/xe/app/oracle/product/10.2.0/server/

you have to use:
export ORACLE_HOME=/usr/lib/oracle/xe/app/oracle/product/10.2.0/server

, because the following exports will have two backslashes in the paths they set.

Best wishes,

Kevin said...

Either one works. The filesystem will recognize both instances.

David said...

Wow - what a life-saver!

Thank you!!

David G.

Wyatt said...

Thanks for the info--it saved me a lot of time.

In my environment (Ubuntu 8.04), the 2nd and 3rd exports weren't needed. Also, I had to `sudo apt-get install libaio1` before I could install the Oracle deb.

Wyatt said...

OK, so I didn't need the 2nd and 3rd exports to INSTALL cx_Oracle, but I do need them to USE it.

william said...

Just as an FYI, the -E option to sudo does not seem to be supported anymore under Ubuntu. You can get around this by just running "sudo -s", getting a shell and adding your environment variable definitions within the root shell, and then running the easy_install.

sevenseeker said...

On Ubuntu 8.04 I just used the "-E" method and it worked perfectly. I uninstalled and tried without it and mean-ol-nasty Oracle yelled at me and said I was a fool without an Oracle install. *boo-hoo*
Ok, I am OK now.
So, I am not sure about what the issue is with Ubuntu not supporting it but they do on my box.
Note that I only had ORACLE_HOME pointing to my client.

As for installing the client, I found it helpful to add the following to my sources.list:
# oracle
deb unstable main non-free

Or you can use something cool like smart (smartpm) and just point to the file and it will pull down deps for you.

Geraint said...

sudo -E
did it.


Sunny Singh said...

I would like to point out that if you were using Ubuntu 64 bits with a 32 bit version of Oracle XE (since there is no 64 bit Oracle XE) the cx_oracle with fail with the following error:

ImportError: wrong ELF class

The fix is simple. Download the oracle instant client for your 64 bit OS and update your LD_LIBRARY_PATH=[path to instant client unzipped folder].

That solved it for me.

Tom B said...

Thank you for this post. I'm sure it saved me an hour at the least. The connection example you have posted did not work for me so I went and looked at the cx_Oracle doc. It seems that they may have changed the syntax. I had to use ...

cnxn = cx_Oracle.connect('system','mysystempassword','xe')

Hope that saves someone some time.

Hasanat Kazmi said...

You saved my 20% of grade, love you for this :-

Oren Tsur said...

Thanks for the guide - it saved me a big deal of time.

There is one thing, however.
it seems i need to repeat the whole export block for every terminal session I open. Is there a way to have it done automatically?

Catherine said...


Yes, absolutely - append these comments to your ~/.bashrc file. See

Oren Tsur said...

thanks, Catherine. you rock.
9the next project is learning to tweak the .bashrc.

Tony Diep said...

After many, many tries, I got it to work on 64 bit Ubuntu 10.04 64.

Problem was it would choke on libclntsh

After installing Oracle XE:

1. Downloaded and InstantClient 11_2 to $HOME/InstantClient_11_2
2. ln -s /usr/lib/oracle/xe/app/oracle/product/10.2.0/server $ORACLE_HOME/
3. sudo -E easy_install cx_Oracle

(setting LD_LIBRARY_PATH did not work for me)

Catherine said...

Thank you for the report, Tony. 64-bit is indeed a headache; even getting Oracle XE itself onto a 64-bit is a pain.

Rajendra said...

I have used the following post to install Oracle XE on Ubuntu 10.10 64-bit. Only thing I need to do extra is to change the oracle-xe deb package file names to latest ones.

kbir kbr said...

Hi Catherine,
Thank you for your valuable tutorial, i have followed these steps and successfully installed cx_Oracle. and i can import cx_oracle using terminal. but i got an error while import it using Python Integrated Development Environment (IDLE) as well as Eclipse.. Pls give me a solution..

Catherine Devlin said...

Hi kbir,

It sounds like your IDLE and Ecliplse editors are using a different Python environment than your terminal. It's possible to have Python installed multiple times on the same computer, and installing cx_Oracle into one of those Pythons won't help with the others.

I'll be that, if you enter "import sys; sys.executable", you'll get a different result in your system Python vs. in IDLE or Eclipse. I'm not sure how to control that (I don't use either one), but hopefully this hint will get you started toward a fix...