【问题标题】:Getting nokogiri to use a newer version of libxml2让 nokogiri 使用更新版本的 libxml2
【发布时间】:2013-04-05 19:50:01
【问题描述】:

我一直在尝试在我的计算机(Mountain Lion)上安装 Nokogiri,以便与 rspec 和 capybara 一起使用,但对于我的生活,我无法让它正常运行。

据我所知,问题在于 nokogiri 使用了错误版本的 libxml2。到目前为止,我已经尝试使用 Homebrew 卸载和重新安装 libxml2(确保它是最新的),使用 bundle 卸载和重新安装 nokogiri,并指定 Homebrew 在安装 nokogiri gem 时安装的 libxml2 文件的确切路径。我最近的安装说明是这样的

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

所有这些位置都正确对应于安装工具的位置。然而,在运行bundle exec rspec spec/requests/static_pages.rb 时,我仍然得到这个输出:

/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'

我已经尝试了我在网上找到的所有步骤(大多数参考构建和加载不同版本的 libxml2,例如 this one,但还没有成功。我的 Gemfile 和 Gemfile.lock 可以在 @987654322 找到@. 如果有人可以帮助我,我将不胜感激。我无法在网上找到任何其他有此问题的案例。

【问题讨论】:

  • brew --prefix libxml2 的输出是什么?请参阅 nokogiri/ext/nokogiri/ 中的 extconf.rb 文件以获取有关 Nokogiri 在何处查找库和头文件的线索。
  • 这是输出:/usr/local/opt/libxml2 我找到了您所指的文件,我想我明白这一切的含义,尽管我真的不确定。我应该将 Homebrew 的路径添加到要检查的位置列表吗?如果我这样做,是否会停止使用旧的错误版本的 libxml2?
  • 太棒了!那行得通!我之前尝试过这些步骤,除了最后一个。我还使用了 Macports 而不是 Homebrew——也许这有帮助。不管怎样,它做到了。非常感谢!
  • alexlafroscia 那么解决方案是什么?请将答案标记为已接受。

标签: ruby-on-rails ruby rspec nokogiri libxml2


【解决方案1】:

在 Mavericks 中,使用 brew 安装库并在安装 gem 之前设置 NOKOGIRI_USE_SYSTEM_LIBRARIES=1 对我来说很有效。

总结:

  • 如果之前已安装,请卸载 gem:
    $ gem uninstall nokogiri

  • 使用 Homebrew 安装 libxml2libxsltlibiconv:
    $ brew install libxml2 libxslt libiconv

  • 安装 gem,指定要链接的库的路径:
    $ 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】:

    这有助于我更新 libxml 并正确安装 nokogiri...

    1. gem 卸载 nokogiri libxml-ruby
    2. 酿造更新
    3. brew 卸载 libxml2
    4. brew install libxml2 --with-xml2-config
    5. 酿造链接--force libxml2
    6. brew 安装 libxslt
    7. brew 链接 --force libxslt
    8. 捆绑配置 build.nokogiri -- --with-xml2-dir=/usr --with-xslt-dir=/opt/local --with-iconv-dir=/opt/local
    9. 捆绑安装

    这里是来源:http://www.kormoc.com/2013/12/22/nokogiri-libxml2/

    希望这对某人有所帮助...

    【讨论】:

    • 不幸的是,在 OS X 10.10.5 上对我不起作用 :( 仍在抱怨 libxml2。
    【解决方案3】:

    为了清楚起见,Phrogz 的评论对我有很大帮助。我按照他链接到的说明进行操作,即 Nokogiri 的 Github 页面上的 What to do if libxml2 is being a jerk? 页面。我最终使用了 Macports 而不是 Homebrew,我不确定这是否是造成差异的原因(我已经尝试过其中的许多步骤),但不管怎样,它现在似乎工作正常。

    【讨论】:

    • 好吧,我想看看 Homebrew 解决方案。
    【解决方案4】:

    在升级 OsX 后遇到问题时用 Google 搜索了这个问题:

    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)

    这对我有用

    brew uninstall libxml2 libxslt libiconv brew install libxml2 libxslt libiconv

    【讨论】:

    • 在 OS X Mavericks 上运行 install 命令时的输出: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.
    猜你喜欢
    • 2011-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-10
    • 1970-01-01
    • 2015-01-12
    • 1970-01-01
    相关资源
    最近更新 更多