Getting nokogiri to use a newer version of libxml2

2019-04-07 21:07发布

问题:

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.

回答1:

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"



回答2:

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...



回答3:

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.



回答4:

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