Sunday, February 17, 2008

Building mysql gem CentOS5: hell

Usually, I'm not too fond of RPM's. I'm more of a deb-kinda guy, I suppose. Sometimes though, it's nice to have RPM's. For example, if you are trying to get a working Rails development environment on your Centos box. omg.

It's all easy while you're installing ruby and mysql. It's just some yum stuff until there. You pull in rdoc, ri and the lot through yum too. But then the hurting starts. First of all: rails does not exist on CentOS / RHEL. Neither does rubygems. Luckily, installing rubygems can be done simply by downloading the package from here, untarring it and running 'ruby setup.rb'. Still easy? Yes, it is.

Then, of course, you need to install rails itself. This you do, by typing 'sudo gem install rails' (or 'gem install rails -v 1.2.3' if you're stuck on using an older version, like me). This all worked like a charm, even though it would have been nice to have an RPM for rubygems and rails.

But then you're on your way to pain: there is no ruby-libmysql package on CentOS or RHEL. The mysql gem I wanted to install instead didn't like me very much. When I ran 'gem install mysql', I received this error:
checking for mysql_query() in -l/usr/lib/mysql/libmysqlclient.so.15.0.0... no
checking for main() in -lm... yes
checking for mysql_query() in -l/usr/lib/mysql/libmysqlclient.so.15.0.0... no
checking for main() in -lz... yes
checking for mysql_query() in -l/usr/lib/mysql/libmysqlclient.so.15.0.0... no
checking for main() in -lsocket... no
checking for mysql_query() in -l/usr/lib/mysql/libmysqlclient.so.15.0.0... no
checking for main() in -lnsl... yes
checking for mysql_query() in -l/usr/lib/mysql/libmysqlclient.so.15.0.0... no
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers. Check the mkmf.log file for more
details. You may need configuration options.

Provided configuration options:
--with-opt-dir
--without-opt-dir
--with-opt-include=${opt-dir}/include
--with-opt-lib
--wiotherthout-opt-lib=${opt-dir}/lib

[...]
Ouch.

It seems I wasn't the only one having trouble installing the mysql gem. At least a couple of sites talked about more or less the same problem. The last one (Mr. Matt) was the most helpful, though the post there did not completely solve my problem. Mr. Matt suggests running 'gem install mysql -- --with-mysql-config=/usr/bin/mysql_config' in order to fix the build error. This was not enough for me to do it. I don't know on what version of CentOS he runs, but for me, on CentOS 5.1, I needed 'gem install mysql -- --with-mysql-conf=/usr/bin/mysql --with-mysql-lib=/usr/lib/mysql'. For some reason, the buildscript kept trying to load libraries from /usr/local/lib instead of /usr/lib (or so it seemed, anyway).

The extra option fixed my problems. Hopes this helps.