Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mac user and getting WARNING: Nokogiri was built against LibXML version 2.7.8, but has dynamically loaded 2.7.3

I have done all kinds of research and tried many different things. I know this question has been answered many times, but none of the suggested solutions are working for me.

After upgrading to Lion I am getting segmentation faults in Ruby. I'm fairly confident it's Nokogiri. So I installed libxml2 via Homebrew. I ran brew link libxml2. Then I reinstalled Nokogiri using that version of the library.

For proof:

$ nokogiri -v
# Nokogiri (1.5.0)
---
warnings: []
nokogiri: 1.5.0
ruby:
  version: 1.9.2
  platform: x86_64-darwin11.0.0
  description: ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-darwin11.0.0]
  engine: ruby
libxml:
  binding: extension
  compiled: 2.7.8
  loaded: 2.7.8

I've already included Nokogiri at the top of my gemfile and I've also required it in my environment file. I have no idea why I am still getting that warning.

Any suggestions or ideas to make sure it's loading the right version libxml2?

like image 522
Binary Logic Avatar asked Jul 23 '11 18:07

Binary Logic


3 Answers

If you installed Nokogiri with gem install nokogiri, you can resolve this warning by running gem pristine nokogiri to recompile the gem's C extension.

If you installed Nokogiri with bundle install, you can resolve this warning by running bundle exec gem pristine nokogiri to recompile the C extension of the gem wherever Bundler installed it.

like image 123
indirect Avatar answered Oct 21 '22 01:10

indirect


To fix this if you're using homebrew and bundler, add gem 'nokogiri' to the top of your Gemfile, then run these commands:

gem uninstall nokogiri libxml-ruby
brew update
brew uninstall libxml2
brew install libxml2 --with-xml2-config
brew install libxslt
bundle config build.nokogiri --with-xml2-include=/usr/local/Cellar/libxml2/2.9.1/include/libxml2 --with-xml2-lib=/usr/local/Cellar/libxml2/2.9.1/lib --with-xslt-dir=/usr/local/Cellar/libxslt/1.1.26/
bundle install

If you don't use bundler, run these commands instead:

gem uninstall nokogiri libxml-ruby
brew update
brew uninstall libxml2
brew install libxml2 --with-xml2-config
brew install libxslt
gem install nokogiri -- --with-xml2-include=/usr/local/Cellar/libxml2/2.9.1/include/libxml2 --with-xml2-lib=/usr/local/Cellar/libxml2/2.9.1/lib --with-xslt-dir=/usr/local/Cellar/libxslt/1.1.26/

In your app, you should require nokogiri first, to force the app to load the dynamic libxml2 library instead of the older system version of libxml2 loaded by gems that failed to specify which library to load.

like image 28
Michiel de Mare Avatar answered Oct 21 '22 02:10

Michiel de Mare


I just spent the better part of the morning working through this warning. This fix is for people using Mac OS Lion. The fix above using

bundle config build.nokogiri --with-xml2-include=/opt/local/include/libxml2 --with-xml2-lib=/opt/local/lib --with-xslt-dir=/opt/local

is for Snow Leopard with libxml2 installed via MacPorts.

With Lion, libxml2 is loaded as part of the bootstrap process. Regardless of which libxml2 Nokogiri is pointing to, the Lion system default library for libxml2 will be used at runtime. Lion uses libxml2.2.7.3 found in /usr (not /usr/local).

As mentioned many other places, one can just ignore the warning. If, like me, the warning drives you crazy, you can do this:

bundle config build.nokogiri --with-xml2-dir=/usr --with-xslt-dir=/opt/local --with-iconv-dir=/opt/local

Interestingly, if you type nokogiri -v at the command line you will get the opposite warning:

WARNING: Nokogiri was built against LibXML version 2.7.3, but has dynamically loaded 2.7.8

This suggests there is more to how libxml2 is being loaded, with Ruby and Rails using the system loaded libxml2 and the command line using libxml2 from the environment path. Anyway, this silences the error for me.

I’ll say it again – this is only for Lion. The previous fix will work for Snow Leopard.

This is the end of the answer. Stop reading here.


OK, you didn’t stop reading... well...

NOT RECOMMENDED!!!!!!

You have been warned. You can verify that Mac OSX is loading the libxml2 library in its bootstrap by disabling libxml2 found in /usr/lib. Do something like copying all versions of libxml2*.dylib to libxml2*.dylib.old (on my machine this was libxml2.2.7.3, libxml2.2 and libxml2).

After you have done this, running Nokogiri will not produce any errors. That is because it can’t find the loaded libxml2 and will now follow the environment path, eventually finding libxml2.2.7.8 in /opt/local.

BUT you won’t be able to copy the old libxml files back. This is because the OS needs the libxml2 that was loaded in the bootstrap.

Powering off and powering on again will brick your machine. The login screen will hang and hang and hang. Power off and power on again in single-user mode (hold Command-S while rebooting). You can watch the bootstrap occur. Low and behold, it throws an error that it can’t load libxml2 and then stops working.

Power off and power on again. This time boot into recovery mode (either hold Command-R or hold Option and then select the recovery disk). In recovery mode open the terminal (utilities/terminal). Mount /usr/lib on your HD (try /Volumes/Macintosh\ HD/usr/lib) and copy the libxml2 files back. Reboot and all will be fine.

like image 39
Kenton Avatar answered Oct 21 '22 03:10

Kenton