Satya's blog - Sybase and Rails, with ODBC

Feb 20 2010 10:45 Sybase and Rails, with ODBC

I've been trying to get Sybase working with Rails, as detailed before in my blog. Here's what eventually worked for me, on Ubuntu 9.10 and Rails 2.3.5, using Phusion Passenger.

Install the rubygems package. Install rails and stuff via gem, and some ruby libraries via apt-get. Note: We apt-get install rubygems, and all the other rails stuff is installed as a gem. Because we wanted latest stuff. *Ruby* libraries are apt-get installed where possible, otherwise via gem. freetds and sybct libraries are compiled on the box. Yuck.

The following gems are installed (I've taken out ones, like calendar_date_select, that are obviously irrelevant for this purpose):

activerecord (2.3.5, 2.3.4, 2.3.3, 2.3.2, 1.15.6)
activerecord-jdbc-adapter (0.9.2, 0.9.1)
activerecord-odbc-adapter (2.0)
activerecord-oracle-adapter (
activerecord-oracle_enhanced-adapter (1.2.3)
activerecord-sybase-adapter (
activeresource (2.3.5, 2.3.4, 2.3.3, 2.3.2)
activesupport (2.3.5, 2.3.4, 2.3.3, 2.3.2, 1.4.4)
dbd-odbc (0.2.5)
dbi (0.4.3)
deprecated (2.0.1)
fastthread (1.0.7)
rack (1.1.0, 1.0.1, 1.0.0)
rails (2.3.5, 2.3.4, 2.3.3, 2.3.2)
rake (0.8.7)
ruby-oci8 (1.0.7)
ruby-odbc (0.9999)
rubygems-update (1.3.5)

I'd gem install rails, dbd-odbc, and rubygems-update. That should pull in most of the others as dependencies. Don't install ruby-odbc and ruby-oci8 right off. For ruby-odbc, see below, and for ruby-oci8, See another article (to be written). The oci8 is for Oracle.

Additional stuff to install (again, only relevant stuff listed):

sudo gem install activerecord-sybase-adapter -s
sudo gem install activerecord-oracle-adapter -s

sudo apt-get install libiodbc2 libodbcinstq1c2 odbcinst1debian1 freetds-common freetds-dev tdsodbc unixodbc unixodbc-dev libdbi-ruby

Your /etc/freetds/freetds.conf file (not sure you need this for ODBC):

        host = somehost1
        port = 4100
        tds version = 5.0
        host = somehost2
        port = 4100
        tds version = 5.0

Begin activerecord-sybase-adapter instructions. If you want to use ODBC only, skip this section.

At some point, I got sybase-ctlib ruby library (sybct-ruby) from here: (sybase-ctlib project page). Also download freetds-stable, ./configure and make, and then edit sybct's extconf.rb file:

sybase = "/usr/local/"
$CFLAGS = "-g -Wall -DFREETDS -I#{sybase}/include"
$LDFLAGS = " -L#{sybase}/lib -L/freetds-0.82/src/tds/.libs" 
$LOCAL_LIBS = "-lct  -lsybdb -ltds -rdynamic -ldl -lnsl -lm"

Note the ldflags line — you have to tell it where libtds is located. You can find freetdsdirectory -name "libtds*" to find the libtds location. Then in the sybase-ctlib directory, run:

ruby extconf.rb
sudo cp sybct.rb sybsql.rb /usr/local/lib/site_ruby/1.8/i486-linux/

Fix this destination according to your system. The command "make install" tries to put it in the right place, but doesn't copy all three files.

At this point you should be able to use the native sybase adapter in Rails. Your config/database.xml:

  adapter: sybase
  host: a
  database: whatever
  username: heh
  password: youwish

End activerecord-sybase-adapter instructions. If you want to use ODBC only, skip the above section.

For the ODBC connection, I stuck this in ~/odbc and ran "odbcinst -i -s -d ~/odbc" (I have tdsodbc and unixodbc, dunno how much of that is required). Actually, I just stuck the following stuff in /etc/odbc.ini:

Description     = d server, name elided
Driver      = /usr/lib/odbc/
TDS Version      = 5.0
Description     = a server, name elided
Driver      = /usr/lib/odbc/
TDS Version      = 5.0

Test with this command: isql -v d user pass where d is the 'd' from the odbc file, and user and pass are the actual ones.

For ODBC with Rails: Per instructions from RubyODBC maintainer (Christian Werner, whom I hereby thank again). First line removes libdbd-odbc-ruby libdbd-odbc-ruby1.8 libodbc-ruby1.8 if they were installed:

sudo apt-get purge libodbc-ruby1.8
sudo gem install ruby-odbc -- --build-flags --disable-dlopen

And that got Sybase over ODBC to work. disable-dlopen is kinda important. Important information that I was looking for but couldn't find: "-- --build-flags" is how you pass stuff to extconf.rb when the gem install command is going to compile the gem. Now you should be able to use ODBC in Rails with this config/database.yml file:

  adapter: odbc
  dsn: a
  database: whatever
  username: heh
  password: youwish


Update: I don't think you need the sybse-ctlib library, nor the freetds source/compile, if you're only doing the ODBC thing. You can get by with the freetds package installed by apt-get. If you're using the activerecord sybase adapter, that's when you need sybase-ctlib and therefore the freetds source.

# freetds and odbc packages (the last two may auto-install as dependencies):
sudo apt-get install freetds-common tdsodbc unixodbc \ odbcinst1debian1 libodbcinstq1c2
# for ruby-odbc to compile correctly:
sudo apt-get purge libodbc-ruby1.8
# to build ruby-odbc:
sudo apt-get install unixodbc-dev
sudo gem install ruby-odbc -- --build-flags --disable-dlopen

Last updated: Mar 23 2010 14:55

Tag: rails