Ruby-on-rails – Snow Leopard upgrade -> reinstalling sqlite3-ruby gem problem

osx-snow-leopardruby-on-railssqlite3-ruby

I got ruby 1.8.7 (native compiled), rails 2.3.4, OSX 10.6.2 and also sqlite3-ruby.

The error I'm getting when accessing the rails app is

NameError: uninitialized constant SQLite3::Driver::Native::Driver::API

History:
I upgraded to snow leopard by migrating my apps with a FW-cable from my old macbook to the new one. Everything was running perfectly for months, but Yesterday I needed to install watir, which was dependant on rb-appscript, which didn't build due a "wrong architecture" error in libsqlite3.dylib. I figured the build was made on the old machine, so i wanted to rebuild sqlite3-ruby:

$ sudo gem uninstall sqlite3-ruby

$ sudo gem install sqlite3-ruby

Building native extensions. This could take a while…
ERROR: Error installing sqlite3-ruby:
ERROR: Failed to build gem native extension.

/usr/local/bin/ruby extconf.rb
checking for fdatasync() in -lrt… no
checking for sqlite3.h… yes
checking for sqlite3_open() in -lsqlite3… 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.

It seems like the sqlite3 libs are not working properly. I've tried to install macports sqlite3 (sudo port install sqlite3) and use that instead, but with same result… so i rebuild sqlite3 from scratch.. download->configure->make->make install. After that, the gem now builds perfectly, but doesn't work in rails, giving the error in the top of this article.

I'm not really sure where to go from here because I've tried the following

  1. Rebuild sqlite3 from newest source (http://www.sqlite.org/download.html)
  2. Reinstalled sqlite3-ruby (sudo gem uninstall sqlite3-ruby && sudo gem install sqlite3-ruby)
  3. Used sqlite3 from macports (sudo port install sqlite3 && sudo gem install sqlite3-ruby)
  4. Reinstalled rails (sudo gem install rails sqlite3-ruby ) and updated environment.rb to rails 2.3.5.

No avail, I still get this error:

NameError: uninitialized constant SQLite3::Driver::Native::Driver::AP
from /usr/local/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:105:in const_missing'
from /usr/local/lib/ruby/gems/1.8/gems/sqlite3-ruby-1.2.5/lib/sqlite3/driver/native/driver.rb:76:in
open'
from /usr/local/lib/ruby/gems/1.8/gems/sqlite3-ruby-1.2.5/lib/sqlite3/database.rb:76:in `initialize'

Btw, I have Xcode installed from the Snow Leopard CD.

What can i do to solve the problem?

Best Answer

My problem was slightly different, and in fact non of the solutions I found on-line worked.

When trying to install sqlite3-ruby after upgrading to Snow Leopard and XCode 4.0 trial, I got the message

checking for sqlite3.h... yes
checking for sqlite3_libversion_number() in -lsqlite3... no
sqlite3 is missing. Try 'port install sqlite3 +universal' or 'yum install sqlite3-devel'

however sqlite3 was installed, and also re-installing did not help. I already had the troub le before with 64-bit and universal versions, but that I had cleared as well. In fact, I could work with sqlite3.

So gem install should also tell you something along these lines:

Gem files will remain installed in /Library/Ruby/Gems/1.8/gems/sqlite3-ruby-1.3.1 for inspection.

So cd to that directory and there look for extconf.rb, mine was in ./ext/sqlite3/extconf.rb I found that ruby was checking for the the sqlite3 library using

asplode('sqlite3') unless find_library 'sqlite3', 'sqlite3_libversion_number'

So I fired up irb and checked why this didn't work:

  require 'mkmf'
   find_library 'sqlite3', 'sqlite3_libversion_number'

Well, in fact this works and my ruby find the library. So why doesn't it work from the setup? Inspecting extconf.rb closely showed the following line:

  sqlite = dir_config('sqlite3', ['/usr/local', '/opt/local', '/usr'])

When I execute this in irb:

require 'mkmf'
  sqlite = dir_config('sqlite3', ['/usr/local', '/opt/local', '/usr']) 
  find_library 'sqlite3', 'sqlite3_libversion_number'

I will surprisingly not find the library anymore. In fact I do not understand how this can be, but that's what happens.

So this is the cure: comment out the line

sqlite = dir_config('sqlite3', ['/usr/local', '/opt/local', '/usr'])

in extconf.rb

Then from /Library/Ruby/Gems/1.8/gems/sqlite3-ruby-1.3.1 I issued

 sudo ruby ./setup.rb

This went through with no problems (I tried before commenting out the sqlite= line, and it did not work)

Restarted the ruby application that had the problems with sqlite. Works fine.

Hope this will help someone.

Icecream