Getting nokogiri to use a newer version of libxml2
Asked Answered
G

4

6

I've been trying to get Nokogiri installed on my computer (Mountain Lion) to use with rspec and capybara, but for the life of me, I can't get it to run properly.

From what I can tell, the issue is with nokogiri using the wrong version of libxml2. I've so far tried uninstalling and reinstalling libxml2 using Homebrew (making sure it's the most recent one), uninstalling and reinstalling nokogiri using bundle, and specifying the exact path to the libxml2 files that Homebrew installed when installing the nokogiri gem. My most recent install instructions looked like this

sudo gem install nokogiri -- --with-xml2-include=/usr/local/Cellar/libxml2/2.9.0/include/libxml2 --with-xml2-lib=/usr/local/Cellar/libxml2/2.9.0/lib --with-xslt-dir=/usr/local/Cellar/libxslt/1.1.28

where all of those locations do correctly correspond to the locations where the tools are installed. However, upon running bundle exec rspec spec/requests/static_pages.rb, I still get this output:

/Users/alex/.rvm/gems/ruby-1.9.3-p286/gems/nokogiri-1.5.9/lib/nokogiri.rb:28:in `require': dlopen(/Users/alex/.rvm/gems/ruby-1.9.3-p286/gems/nokogiri-1.5.9/lib/nokogiri/nokogiri.bundle, 9): Library not loaded: /usr/local/lib/libxml2.2.dylib (LoadError)
Referenced from: /Users/alex/.rvm/gems/ruby-1.9.3-p286/gems/nokogiri-1.5.9/lib/nokogiri/nokogiri.bundle
Reason: Incompatible library version: nokogiri.bundle requires version 12.0.0 or later, but libxml2.2.dylib provides version 10.0.0 - /Users/alex/.rvm/gems/ruby-1.9.3-p286/gems/nokogiri-1.5.9/lib/nokogiri/nokogiri.bundle
    from /Users/alex/.rvm/gems/ruby-1.9.3-p286/gems/nokogiri-1.5.9/lib/nokogiri.rb:28:in `<top (required)>'
    from /Users/alex/.rvm/gems/ruby-1.9.3-p286@global/gems/bundler-1.3.4/lib/bundler/runtime.rb:72:in `require'
    from /Users/alex/.rvm/gems/ruby-1.9.3-p286@global/gems/bundler-1.3.4/lib/bundler/runtime.rb:72:in `block (2 levels) in require'
    from /Users/alex/.rvm/gems/ruby-1.9.3-p286@global/gems/bundler-1.3.4/lib/bundler/runtime.rb:70:in `each'
    from /Users/alex/.rvm/gems/ruby-1.9.3-p286@global/gems/bundler-1.3.4/lib/bundler/runtime.rb:70:in `block in require'
    from /Users/alex/.rvm/gems/ruby-1.9.3-p286@global/gems/bundler-1.3.4/lib/bundler/runtime.rb:59:in `each'
    from /Users/alex/.rvm/gems/ruby-1.9.3-p286@global/gems/bundler-1.3.4/lib/bundler/runtime.rb:59:in `require'
    from /Users/alex/.rvm/gems/ruby-1.9.3-p286@global/gems/bundler-1.3.4/lib/bundler.rb:132:in `require'
    from /Users/alex/Sites/harbingernews/config/application.rb:7:in `<top (required)>'
    from /Users/alex/Sites/harbingernews/config/environment.rb:2:in `require'
    from /Users/alex/Sites/harbingernews/config/environment.rb:2:in `<top (required)>'
    from /Users/alex/Sites/harbingernews/spec/spec_helper.rb:3:in `require'
    from /Users/alex/Sites/harbingernews/spec/spec_helper.rb:3:in `<top (required)>'
    from /Users/alex/Sites/harbingernews/spec/requests/static_pages_spec.rb:1:in `require'
    from /Users/alex/Sites/harbingernews/spec/requests/static_pages_spec.rb:1:in `<top (required)>'
    from /Users/alex/.rvm/gems/ruby-1.9.3-p286/gems/rspec-core-2.9.0/lib/rspec/core/configuration.rb:746:in `load'
    from /Users/alex/.rvm/gems/ruby-1.9.3-p286/gems/rspec-core-2.9.0/lib/rspec/core/configuration.rb:746:in `block in load_spec_files'
    from /Users/alex/.rvm/gems/ruby-1.9.3-p286/gems/rspec-core-2.9.0/lib/rspec/core/configuration.rb:746:in `map'
    from /Users/alex/.rvm/gems/ruby-1.9.3-p286/gems/rspec-core-2.9.0/lib/rspec/core/configuration.rb:746:in `load_spec_files'
    from /Users/alex/.rvm/gems/ruby-1.9.3-p286/gems/rspec-core-2.9.0/lib/rspec/core/command_line.rb:22:in `run'
    from /Users/alex/.rvm/gems/ruby-1.9.3-p286/gems/rspec-core-2.9.0/lib/rspec/core/runner.rb:69:in `run'
    from /Users/alex/.rvm/gems/ruby-1.9.3-p286/gems/rspec-core-2.9.0/lib/rspec/core/runner.rb:10:in `block in autorun'

I've tried all the steps that I've found online (most reference building and loading different versions of libxml2, like this one, but have had no success yet. My Gemfile and Gemfile.lock can be found here. If anyone can help me out at all, I would really appreciate it. I haven't been able to find any other cases online with this problem.

Grantinaid answered 5/4, 2013 at 19:50 Comment(6)
What's the output of brew --prefix libxml2? See the extconf.rb file in nokogiri/ext/nokogiri/ for clues on where Nokogiri is looking for libs and header files.Obnubilate
Here's the output: /usr/local/opt/libxml2 I found the file you were referring to and I think I understand what it all means, although I'm really unsure. Should I add the paths from Homebrew to the list of locations to check? If I do this, will is stop using the old, wrong version of libxml2?Grantinaid
Have you read What to do if libxml2 is being a jerk?Sleepy
Awesome!! That worked! I had tried those steps before, except for the last one. I also used Macports instead of Homebrew -- maybe that helped. Either way, that did it. Thanks so much!Grantinaid
alexlafroscia so what was the solution? Please mark an answer as accepted.Sonorous
Sorry, what ultimately helped me was a comment and I can't mark that as the "right" solutionGrantinaid
G
4

Just to make it clear, Phrogz's comment helped me out quite a bit. I followed the instructions that he linked to, the What to do if libxml2 is being a jerk? page on Nokogiri's Github page. I ended up using Macports instead of Homebrew, and I'm not sure if that's what made the difference (I had tried many of those steps already) but one way or another, it seems to be working alright now.

Grantinaid answered 6/4, 2013 at 5:37 Comment(1)
Well I'd like to see a Homebrew solution.Sonorous
C
9

In Mavericks, installing the libraries with brew and setting NOKOGIRI_USE_SYSTEM_LIBRARIES=1 before installing the gem did the trick for me.

Summarising:

  • If previously installed, uninstall the gem:
    $ gem uninstall nokogiri

  • Use Homebrew to install libxml2, libxslt and libiconv:
    $ brew install libxml2 libxslt libiconv

  • Install the gem specifying the paths to the libraries to be linked against:
    $ NOKOGIRI_USE_SYSTEM_LIBRARIES=1 gem install nokogiri -- --use-system-libraries --with-iconv-dir="$(brew --prefix libiconv)" --with-xml2-config="$(brew --prefix libxml2)/bin/xml2-config" --with-xslt-config="$(brew --prefix libxslt)/bin/xslt-config"

Canicular answered 10/5, 2014 at 18:40 Comment(0)
P
8

This helps me to update libxml and install nokogiri correctly...

  1. gem uninstall nokogiri libxml-ruby
  2. brew update
  3. brew uninstall libxml2
  4. brew install libxml2 --with-xml2-config
  5. brew link --force libxml2
  6. brew install libxslt
  7. brew link --force libxslt
  8. bundle config build.nokogiri -- --with-xml2-dir=/usr --with-xslt-dir=/opt/local --with-iconv-dir=/opt/local
  9. bundle install

Here is the source: http://www.kormoc.com/2013/12/22/nokogiri-libxml2/

Hope this helps somebody...

Preliminary answered 8/10, 2014 at 9:46 Comment(1)
Did not work for me on OS X 10.10.5 unfortunately :( Still complaining about libxml2.Silber
G
4

Just to make it clear, Phrogz's comment helped me out quite a bit. I followed the instructions that he linked to, the What to do if libxml2 is being a jerk? page on Nokogiri's Github page. I ended up using Macports instead of Homebrew, and I'm not sure if that's what made the difference (I had tried many of those steps already) but one way or another, it seems to be working alright now.

Grantinaid answered 6/4, 2013 at 5:37 Comment(1)
Well I'd like to see a Homebrew solution.Sonorous
P
1

Googled this question after upgrading OsX when I had problem:

require': dlopen(/Users/tomi/.rvm/gems/ruby-2.1.1@my-gemset/extensions/x86_64-darwin-12/2.1.0-static/nokogiri-1.5.10/nokogiri/nokogiri.bundle, 9): Library not loaded: /usr/local/opt/libxml2/lib/libxml2.2.dylib (LoadError)

and this worked for me

brew uninstall libxml2 libxslt libiconv brew install libxml2 libxslt libiconv

Proportioned answered 1/6, 2014 at 20:18 Comment(1)
Output when running the install command on OS X Mavericks: Error: No available formula for libiconv Apple distributes libiconv with OS X, you can find it in /usr/lib. Some build scripts fail to detect it correctly, please check existing formulae for solutions.Incertitude

© 2022 - 2024 — McMap. All rights reserved.