Painless Installation of Ruby Open Babel 3

Posted by Rich Apodaca Mon, 09 Apr 2007 14:09:00 GMT

Open Babel 2.1.0 has just been released. Among its new features is a Ruby interface containing most of the functionality of the C++ library. Installation is quick and easy, as shown in this article.

Prerequisites

In addition to a working build system, you'll need Ruby and the Ruby development libraries. Although any recent version should do, this tutorial was written with version 1.8.5.

Step 0: Compile and Install Open Babel

Given the right tools on your system, compiling and and installing Open Babel from source is trivial. This page gives instructions for doing so on Linux, Windows, and Mac OS X.

Step 1: Create the Wrapper's Makefile

After unpacking, compiling, and installing Open Babel, change into the scripts/ruby directory of your source distribution. Next, run the extconf.rb script:

$ ruby extconf.rb
checking for main() in -lopenbabel... yes
creating Makefile

As you've probably guessed, the purpose of this script is to generate a Makefile specific to your platform. This script uses the standard Ruby library mkmf.

Step 2: Compile the Wrapper

After creating a Makefile, we're ready to compile the C++ Ruby wrapper, contained in openbabel_ruby.cpp:

$ make
g++ -I. -I. -I/usr/lib/ruby/1.8/x86_64-linux-gnu -I. -I../../include  -fPIC -O2 -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -Wall  -fPIC   -c openbabel_ruby.cpp

This output will be followed by other lines as the compiler builds the wrapper library.

Step 3: Install the Wrapper

After compiling the wrapper, we're ready to install it. You can probably guess that the next command will be (as root):

# make install
/usr/bin/install -c -m 0755 openbabel.so /usr/lib/ruby/site_ruby/1.8/x86_64-linux-gnu

Your install directory is chosen by Ruby to be appropriate for your platform and Ruby version.

Hello, Benzene!

Congratulations, you've installed Ruby Open Babel! You can verify that your new library works with interactive Ruby (irb):

$ irb
irb(main):001:0> require 'openbabel'
=> true
irb(main):002:0> c=OpenBabel::OBConversion.new
=> #<OpenBabel::OBConversion:0x2acedbadd020>
irb(main):003:0> c.set_in_format 'smi'
=> true
irb(main):004:0> benzene=OpenBabel::OBMol.new
=> #<OpenBabel::OBMol:0x2acedbacfa10>
irb(main):005:0> c.read_string benzene, 'c1ccccc1'
=> true
irb(main):006:0> benzene.num_atoms
=> 6
Comments

Leave a response

  1. Geoff Mon, 09 Apr 2007 15:34:57 GMT

    Thanks for the write-up Rich! I realized that I included a Darwin Makefile from my laptop in the distribution, but with your permission, I'll link to this article from the Open Babel wiki for instructions on building the Ruby interface.

    Thanks!

  2. Rich Mon, 09 Apr 2007 16:18:51 GMT

    I actually hadn't noticed the existing Makefile in the distriubution. Of course, this isn't a big deal because extconf.rb overwrites it. Please do link to this article on the Open Babel wiki.

  3. Rich Mon, 09 Apr 2007 16:40:35 GMT

    I just had another look at a fresh 2.1.0 source distribution and there is no Makefile - just the examples directory, the README, openbabel_ruby.cpp, and extconf.rb. So everything looks fine.