$ 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 ~/.bashrc file:
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.
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
Now, edit 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:
$ 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
Visit https://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 bundle.
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.
Antonio Cangiano is a Software Development Manager at IBM. He authored Ruby on Rails for Microsoft Developers (Wrox, 2009) and Technical Blogging (The Pragmatic Bookshelf, 2012, 2019). He is also the Marketing Lead for Cognitive Class, an educational initiative which he helped grow from zero to over 1 Million students. You can follow him on Twitter.