Meditations on programming, startups, and technology
New Relic

Installing Python, Django, and DB2 on Ubuntu 11.04

Note: This is the Python version of the Ruby guide I just published.

In this brief tutorial I’ll show you how to create a complete Python and Django setup for DB2 on Ubuntu. By following my step-by-step instructions, you’ll be able to install the following components:

Installing Python and easy_install

Technically Python is already installed on Ubuntu, so we don’t have to install it. However, we need the python-dev package in order to build the DB2 driver from source. We’ll also need easy_install which is included in the python-setuptools Ubuntu .deb:

$ sudo apt-get install python-dev python-setuptools

Installing Django

Now that we have easy_install available, we can easily install Django by running:

$ sudo easy_install django

You can verify the installed version by running:

$ python

And then executing the following snippet in the REPL:

>>> import django
>>> django.VERSION
(1, 3, 0, 'final', 0)

At this time, version 1.3.0 should get installed as shown above.

Installing DB2

We can now download and install DB2 Express-C 9.7.4. Download the .tar.gz file to a location that’s convenient for you. Then proceed to unpack it:

$ cd ~/Downloads/
$ tar xvfz db2exc_974_LNX_x86.tar.gz
$ cd expc

We’ll install one required library and then proceed with the setup:

$ sudo apt-get install libaio1
$ sudo ./db2setup

Follow the GUI wizard on screen to continue with the installation. Pay close attention to two steps:

  • When prompted select a custom installation, and when choosing the components, select all of them. We’ll need the Application Development Tools in order to build the Python driver later on (and these are unchecked by default).
  • When asked if you’d like to create an instance user, go with that option. It greatly simplifies the setup process.

When the setup of DB2 is completed, you should receive a confirmation message informing you about the successful installation

For good measure, add the following line to your ~/.bashrc file:

. /home/db2inst1/sqllib/db2profile

This ensures that even your regular, non-DB2, user will be able to connect and interact with the database.

Installing the Python driver and Django adapter

The last step we need to take is to install the ibm_db Python driver and the ibm_db_dbi module, as well as the Django adapter for DB2. These are all IBM supported, open source releases.

Open a new shell and run:

$ sudo -s
$ export IBM_DB_DIR=/home/db2inst1/sqllib
$ export IBM_DB_LIB=/home/db2inst1/sqllib/lib
$ . /home/db2inst1/sqllib/db2profile
$ easy_install ibm_db ibm_db_django
$ exit

A quick sanity test

To ensure that all is well with your setup, run the following command:

$ django-admin.py startproject db2test
$ cd db2test

Now edit settings.py so ENGINE is set to ibm_db_django, NAME to a database like db2test, USER to db2inst1, PASSWORD to the same password you specified for the db2inst1 user during the installation of DB2. Use localhost for the HOST, and 50000 as the port number:

DATABASES = {
    'default': {
        'ENGINE': 'ibm_db_django',
        'NAME': 'db2test',
        'USER': 'db2inst1',
        'PASSWORD': 'secret',
        'HOST': 'localhost',
        'PORT': '50000',
    }
}

Create the database db2test by running:

$ su - db2inst1
$ db2start
$ db2 create db db2test
$ exit

Depending on your hardware specs, the infrequent operation of creating a database can take a bit of time (e.g., several minutes).

We will now run the actual test:

$ python manage.py test

Unless exceptions are raised during this last step, you should be all set to use Django with DB2.

If you need help

IBM is the only database vendor to officially provide and support its Python driver and Django adapter. While commercial DB2 support is available and relatively inexpensive, your first line of defense is posing your questions in the support forum over at Google. Alternatively, if the question is DB2-specific and not related to Python/Django, you can use the DB2 Express-C forum over at developerWorks instead.

No related posts.


If you enjoyed this post, then make sure you subscribe to my Newsletter and/or Feed.

receive my posts by email

One Response to “Installing Python, Django, and DB2 on Ubuntu 11.04”

  1. Page Hughey says:

    Great post!, Have definitely bookmarked this page for future reference, thanks

  2. [...] Posted by varun.chitre15 i downloaded it bt how to install it To install python Installing Python, Django, and DB2 on Ubuntu 11.04 See if this [...]

Leave a Reply

I sincerely welcome and appreciate your comments, whether in agreement or dissenting with my article. However, trolling will not be tolerated. Comments are automatically closed 15 days after the publication of each article.

Copyright © 2005-2012 Antonio Cangiano. All rights reserved.