Affectionately known among all non-masochists in the world of IT as The Seventh Circle of Hell (with real hell being preferable), working with Oracle is always a hair-tearing nightmarish fork-in-the-eye please-for-the-love-of-god-kill-me-now experience that none but those who look to Ted Bundy, Jeffrey Dahmer, and John Wayne Gacy for moral and spiritual guidance would wish upon even their most reviled enemies.
Yes, it’s that bad. And apparently nowhere is it worse than when one attempts to get Oracle working with Python on Ubuntu.
I’m not even talking about installing the Oracle database server itself here people, I’m just needing a Python application to talk to an existing Oracle database. One would think, as with every other database server on the planet (and yes, I’m including that other slice of hell SQL Server in that statement since it’s a damn sight simpler to get working — even on Linux — than Oracle), you’d simply apt-get and/or pip install a library or two and be done with it.
If you actually do think that, you’ve already forgotten that this is Oracle we’re talking about.
That said, one does what one has to do to keep the paychecks coming, so if you need to do this here’s the steps to make it all happen. (Note that on Step 1 I’m assuming you have already installed all the other Python packages you may need. I’m focusing on the stuff you may not have that you definitely need.)
- sudo apt-get install libaio1 alien
- Download the RPM of version 126.96.36.199.0 of the Oracle client from http://www.oracle.com/technetwork/topics/linuxx86-64soft-092277.html (note that as of the date of this writing the 12.x version doesn’t work, or at least didn’t for me)
- You have to have an Oracle Web Account and use that to log in and download this, which makes using wget on the target server itself or automating the process for use with something like a Vagrant provisioning script rather problematic. Short version, you’ll have to download this locally and then scp it up to the target server. What I did is downloaded and converted this RPM as well as the other necessary RPM, converted them once, and put them in a git repo from which I can clone in my Vagrant provisioning script. Whether or not that adheres to the licensing agreement, I don’t know and I don’t care. If you’re paranoid, check with a lawyer before repeating my solution on this.
- If the version number differs slightly from what I have here, adjust later steps accordingly.
- Download the RPM of the Python 2.7/Oracle 11g version of the cx_Oracle Python libraries from http://cx-oracle.sourceforge.net/
- scp the RPMs up to the target server as needed.
- Convert the RPMs to Debian packages using alien:
sudo alien -d FILENAME.rpm (where FILENAME is of course the name of each of the two RPM files)
- sudo dpkg -i oracle-installclient11.2-basic_188.8.131.52.0-2_amd64.deb
- sudo vim /etc/ld.so.conf.d/oracle.conf
- Note: this file won’t already exist, so you’ll be creating this as a new file in this step
- Enter the following in the newly created oracle.conf file and save it:
- export ORACLE_HOME=/usr/lib/oracle/11.2/client64
- export LD_LIBRARY_PATH=$ORACLE_HOME/lib
- sudo ldconfig
- sudo dpkg -i cx-oracle_5.1.2-2_amd64.deb
- cd /usr/lib/python2.7
- sudo mv site-packages/cx_Oracle* dist-packages
- sudo rmdir site-packages
- sudo ln -s dist-packages site-packages
- Verify installation by opening a Python interpreter and run the following:
- If you don’t get an import error, everything is working properly
As far as automating this for use with Vagrant, in my provisioning script I simply echoed the export statements in the steps above into /etc/environment, did source /etc/environment and followed that with ldconfig. Other than that the steps in the bash script are pretty much what’s above, but if people are interested in seeing the script let me know and I can post it.
And there you have it. A lot of trial and error and head bashing went into that final solution, and since I kind of cobbled together the steps from various resources I’ll post those below in case you want to see some of the other solutions and source material.
Happy Oracleing. Or not.