TechnoBlog

Random Technology I'm working with

Friday, October 24, 2008

Install Oracle Instant Client 11.1 and cx_Oracle 4.4 on Ubuntu 8.04

Purpose
I'm working on building a python app that needs to connect to an existing Oracle Database. The server it will be deployed on is Ubuntu 8.04 Server - 32 bit. No gui is installed. When I started down the road of installing Oracle Instant Client and cx_Oracle, I ran into several problems. I was able to find bits and pieces on the web that helped me along, but I did not find an all in one document. I'm sure there are some out there, but my Google-fu failed me.

Disclaimer

This works with Ubuntu 8.04, Oracle Instant Client 11.1, and cx_Oracle 4.4. It should work in a similar manner with other versions, but I have not tested anything but this configuration.

Step 1 - Install Prerequisite Software

Start out by becoming root. If you don't like to be root, that's fine, just prepend all commands with sudo.

bmorgan@muddy:~$ sudo su -


You will need to have the following packages installed to get Oracle Instant Client and cx_Oracle installed.
  • build-essential

  • unzip

  • python-dev

  • libaio-dev
root@muddy:~# apt-get install build-essential unzip python-dev libaio-dev


Step 2 - Get, Install and Configure Oracle Instant Client 11.1

Make a directory to install Oracle Instant Client into. I like to use /usr/local/oracle, but feel free to use whatever you want.

root@muddy:~# mkdir /usr/local/oracle


Download the Basic and the SDK packages from http://www.oracle.com/technology/tech/oci/instantclient/instantclient.html.

extract the files to the directory you created earlier

root@muddy:~# unzip -d /usr/local/oracle basic.zip
root@muddy:~# unzip -d /usr/local/oracle sdk.zip


Make a couple of symbolic links in the instantclient directory that you unzipped the instant client into.
Don't skip this step. If you do, cx_Oracle will not compile correctly

root@muddy:~# cd /usr/local/oracle/instantclient_11_1
root@muddy:/usr/local/oracle/instantclient_11_1# ln -s libclntsh.so.11.1 libclntsh.so
root@muddy:/usr/local/oracle/instantclient_11_1# ln -s libocci.so.11.1 libocci.so


Create a file called oracle.conf in the /etc/ld.so.conf.d directory and add the path that the instant client installed into. For example, mine is /usr/local/oracle/instantclient_11_1

root@muddy:~# echo /usr/local/oracle/instantclient_11_1 > /etc/ld.so.conf.d/oracle.conf
root@muddy:~# ldconfig


Step 3 - Get, Install and Configure cx_Oracle 4.4

Download cx_Oracle source from http://sourceforge.net/projects/cx-oracle/
Be sure to grab the source file. It will end in .tar.gz

Extract the file once downloaded.

root@muddy:~# tar -zxvf cx_Oracle-4.4.tar.gz


Change into the extracted directory.

root@muddy:~# cd cx_Oracle-4.4


set your ORACLE_HOME environment var to the directory that you installed the instant client into

root@muddy:~/cx_Oracle-4.4# export ORACLE_HOME=/usr/local/oracle/instantclient_11_1


Compile cx_Oracle.

root@muddy:~/cx_Oracle-4.4# python setup.py build


You might get an error like this:

/usr/bin/ld: cannot find -lclntsh
collect2: ld returned 1 exit status
error: command 'gcc' failed with exit status 1


If you did, it's because you skipped the symlinks I told you about in step 2.

root@muddy:/usr/local/oracle/instantclient_11_1# ln -s libclntsh.so.11.1 libclntsh.so
root@muddy:/usr/local/oracle/instantclient_11_1# ln -s libocci.so.11.1 libocci.so


run the build again, and it should complete.

Install it

root@muddy:~/cx_Oracle-4.4# python setup.py install


If the build completes without error, fire up the python interactive interpreter

root@muddy:~/cx_Oracle-4.4# python

>>> import cx_Oracle

if you get an error like the following, it's because you forgot to install the libaio-dev package in step 1.

Traceback (most recent call last):
File "", line 1, in
ImportError: libaio.so.1: cannot open shared object file: No such file or directory

root@muddy:~/cx_Oracle-4.4# apt-get install libaio-dev


Step 4 - Final System Configuration

Create a file in /etc/profile.d called oracle.sh and set the environment variables required by Oracle in it.

root@muddy:~# echo export ORACLE_HOME=/usr/local/oracle/instantclient_11_1 >> /etc/profile.d/oracle.sh
root@muddy:~# echo export TNS_ADMIN=\$ORACLE_HOME >> /etc/profile.d/oracle.sh

If you don't want ORACLE_HOME and TNS_ADMIN to bet set at a system wide level, you can skip this step and set those values in your ~/.profile file, or export them some other way.
They **DO** have to be set or cx_Oracle will not be happy.

Put your tnsnames.ora file in the directory that you assigned to TNS_ADMIN above. I set that to be the same directory I installed the instant client in, but it's totally up to you.
Just be sure that the TNS_ADMIN environment variable matches the path that you put it in.

If you created the /etc/profile.d/oracle.sh file above, reboot - This is so the new profile will take effect system wide

Once your machine is back up and you have your tnsnames.ora file in place test it out by opening the interactive python shell and making a connection to your database.

root@muddy:/usr/local/oracle/instantclient_11_1# python
Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cx_Oracle
>>> con = cx_Oracle.Connection('username','password','SID')
>>> con
>>>


Conclusion

That's it. Now you can use cx_Oracle in your python project.
If you run into problems using this guide, please let me know. I'd like to build an all in one comprehensive doc that can get a complete newbie up and running with no fuss.

Monday, October 13, 2008

Convert SLES to OpenSUSE

I am not a SUSE Linux fan. Don't get me wrong, it is very easy to install and just as reliable as any of the other distros -- but there are things about it that really burn me. Let's talk about the most recent thing to bite me: The fact that the Enterprise Server (SLES) and Enterprise Desktop (SLED) are two separate products.

The biggest problem with this approach is that if I install SLES, but later need a package that Novell doesn't consider to be a Server package, I'm mostly out of luck. I manage a web server running SLES 10 that needed a package that isn't included with the SLES disks. To get around this, I downloaded the SLED 10 DVD from Novell and added it as an install source on the server. I was then able to install the packages that I needed from the SLED DVD. Keep in mind that I am licensed for SLES, but not for SLED.

Around the time that I did that, I stopped getting notifications about updates. But that was also the time that SP1 came out, so I figured that I would need to upgrade to SP1 before it would give me any new updates. This is a fairly high profile machine so I was unable to apply SP1 because I couldn't afford the reboot.

We very recently had a scheduled down time to install a new generator, and I was going to take this time to apply SP1. But to my dismay, it wouldn't apply the service pack. The problem was at the point where it switched update sources. Every time I would run the switch-update-server script, it would fail, and I would get an email telling me that I needed to register my SLED (notice the D) install. So those few packages that I installed from the SLED disk were keeping me from updating.

I ended up pulling down the SP1 DVD iso from Novell's site and did the upgrade from the DVD, but I'm still not able to get any additional updates because of the SLED packages. I'm sorry, but I don't feel like I should have to buy a SLED license for 1 or 2 packages. And the whole idea of having a Server edition that has X packages and a Desktop edition that has Y packages is just silly. Especially when Redhat and Ubuntu both ship an all in one product.

Now, on to the interesting bits. Since I refuse to buy a desktop license, I've decided to convert the install from SLE(S/D) 10 SP1 to OpenSUSE 11, which includes both server and desktop packages. The really nice thing about this is that it is a simple upgrade. Download the OpenSUSE 11 iso, burn to DVD, pop it in the drive, reboot and do an upgrade.





At the Installation Settings screen, be sure to click the Update Options link, and within that window choose "Only Update Installed Packages".



Hit the Update button and get a cup of coffee, or in my case, Diet Pepsi. You should come back to a shiny new OpenSUSE 11 install.

Disclaimer: I did this from SLES 10 SP 1 to OpenSUSE 11. I have not tested on any other version combinations. I am not guaranteeing this will work for you. Be sure to back up anything of value before attempting something like detailed above. Help control the pet population. Be sure to Spay or Neuter your pets.

Introductions

Let me clear my throat...

I've been wanting to get a blog going for quite a while now. I had grand plans of building another friggin' blogging engine with Django, but I've given up on that. There just aren't enough hours in the day.

Between my wife and kids, I'm much too busy for writing a blogging engine.

I'm currently employed as a Linux sys admin. It is the greatest job in the world for a computer geek like me. I'm able to do the OS related stuff, like installs, backups, security, etc. But I also get to do some scripting for automation. Originally part of my job was web development, which is still a huge part of my life, just not so much at work. I'm still able to put out a web app now and then if it relates to automation.

I started out using PHP for web apps. PHP was a good way to get started with web development, but it wasn't too long before my code started getting really complex and hard to debug. In 2005 I started reading about Django. I had started learning Python a few months before, mostly out of curosity, and fell in love. I went through the Django tutorial and I haven't looked back. I have all but thrown out PHP and now use Python as my Swiss Army Language. It does everything I ask it to do.

I hope to contribute some of my experiences with Django, Linux, Python, etc. Maybe I will be able to help people avoid my mistakes.

Throat is now fully clear. Until Next time....