In this tutorial I’ll show you how to create a complete Ruby on Rails setup for DB2 on Ubuntu. Following my step-by-step instructions, you’ll be able to install the following components:
We are going to install Ruby 1.8.7 using the Debian packages that are available in the default repositories:
$ sudo apt-get install ruby-full
Next we’ll build Rubygems from scratch, so as to obtain a recent version and be able to update it to the latest one via the gem system itself:
$ cd /tmp/
$ wget http://production.cf.rubygems.org/rubygems/rubygems-1.7.2.tgz
$ tar xvfz rubygems-1.7.2.tgz
$ cd rubygems-1.7.2
$ sudo ruby setup.rb
$ sudo gem update --system
You can now verify that Rubygems is correctly installed:
$ gem -v
Note: If you plan to have multiple versions of Ruby, you’ll probably want to look into RVM.
We are not going to install the Rails package that ships with Ubuntu, because this is also quite obsolete at this stage. Instead, we’ll use the newly installed gem command:
$ sudo gem install rails
--no-rdoc --no-ri flags if you don’t care about the local documentation. This will speed up the installation process.)
You can verify the installed version by running:
$ rails -v
We can now download and install DB2 Express-C 9.7.4. Download the .tar.gz installation 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 Ruby 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
This ensures that even your regular, non-DB2, user will be able to connect and interact with the database.
Installing the ibm_db gem
The last step we need to take is to install the ibm_db gem, which includes the IBM released (and supported) open source driver for Ruby, as well as the adapter for Rails. You’ll need these in order to use Rails with DB2.
Open a new shell and run:
$ sudo -s
$ export IBM_DB_INCLUDE=/home/db2inst1/sqllib/include
$ export IBM_DB_LIB=/home/db2inst1/sqllib/lib
$ . /home/db2inst1/sqllib/db2profile
$ gem install ibm_db
At the time of writing, this installs ibm_db-2.5.6.
A quick sanity test
To ensure that all is well with your setup, run the following command:
$ rails new db2test -d ibm_db
$ cd db2test
config/database.yml so that the development section uses the same password you specified for the
db2inst1 user during the installation of DB2. Change the database name to something like
db2test. The section should look like the example below:
Create the database
db2test by running:
$ su - db2inst1
$ db2 create db db2test
Depending on your hardware specs, the infrequent operation of creating a database can take quite a long time (e.g., minutes).
We will now install/attach the required gems for this project:
$ bundle install
For the sake of a quick example, we’ll use scaffold to generate some super-basic code. We’ll then run migrations, and the built-in web server:
$ rails g scaffold Subject name:string
$ rake db:migrate
$ rails s
http://localhost:3000/subjects and you should see a scaffold interface you can use to create, edit, show, and destroy subjects, as I’ve done in the screenshot below.
Note: As you develop, you’ll probably want to use a better web server such as mongrel, unicorn, or thin. You can easily do so by editing your
Gemfile and installing the gem via
IBM is the only database vendor to officially provide and support its Ruby driver and Rails adapter. While commercial 24/7 DB2 support is certainly available and relatively inexpensive (i.e., cheaper than MySQL), your first line of defense is posing your questions in the support forum over at Rubyforge. Alternatively, if the question is DB2-specific and not related to Ruby or Rails, you can use the DB2 Express-C forum over at developerWorks instead.
Get more stuff like this
Subscribe to my mailing list to receive similar updates about programming.
Thank you for subscribing. Please check your email to confirm your subscription.
Something went wrong.