libxml-ruby:无法构建 gem 本机扩展

libxml-ruby: Failed to build gem native extension

我正在尝试安装 libxml-ruby。 我已经安装了 libxml2libxsltcoreutils

我也阅读了有关此问题的其他帖子,但 none 为我解决了它。

brew list libxslt

/opt/boxen/homebrew/Cellar/libxslt/1.1.28_1/bin/xslt-config
/opt/boxen/homebrew/Cellar/libxslt/1.1.28_1/bin/xsltproc
/opt/boxen/homebrew/Cellar/libxslt/1.1.28_1/include/libexslt/ (3 files)
/opt/boxen/homebrew/Cellar/libxslt/1.1.28_1/include/libxslt/ (21 files)
/opt/boxen/homebrew/Cellar/libxslt/1.1.28_1/lib/libexslt.0.dylib
/opt/boxen/homebrew/Cellar/libxslt/1.1.28_1/lib/libxslt.1.dylib
/opt/boxen/homebrew/Cellar/libxslt/1.1.28_1/lib/pkgconfig/ (2 files)
/opt/boxen/homebrew/Cellar/libxslt/1.1.28_1/lib/ (5 other files)
/opt/boxen/homebrew/Cellar/libxslt/1.1.28_1/share/aclocal/libxslt.m4
/opt/boxen/homebrew/Cellar/libxslt/1.1.28_1/share/doc/ (90 files)
/opt/boxen/homebrew/Cellar/libxslt/1.1.28_1/share/man/ (3 files)

brew list libxml2

/opt/boxen/homebrew/Cellar/libxml2/2.9.4/bin/xml2-config
/opt/boxen/homebrew/Cellar/libxml2/2.9.4/bin/xmlcatalog
/opt/boxen/homebrew/Cellar/libxml2/2.9.4/bin/xmllint
/opt/boxen/homebrew/Cellar/libxml2/2.9.4/include/libxml2/ (47 files)
/opt/boxen/homebrew/Cellar/libxml2/2.9.4/lib/libxml2.2.dylib
/opt/boxen/homebrew/Cellar/libxml2/2.9.4/lib/cmake/libxml2/libxml2-config.cmake
/opt/boxen/homebrew/Cellar/libxml2/2.9.4/lib/pkgconfig/libxml-2.0.pc
/opt/boxen/homebrew/Cellar/libxml2/2.9.4/lib/ (3 other files)
/opt/boxen/homebrew/Cellar/libxml2/2.9.4/share/aclocal/libxml.m4
/opt/boxen/homebrew/Cellar/libxml2/2.9.4/share/doc/ (153 files)
/opt/boxen/homebrew/Cellar/libxml2/2.9.4/share/gtk-doc/ (55 files)
/opt/boxen/homebrew/Cellar/libxml2/2.9.4/share/man/ (4 files)

gem install libxml-ruby 产生以下错误:

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

    current directory: /opt/rubies/2.3.0/lib/ruby/gems/2.3.0/gems/libxml-ruby-2.9.0/ext/libxml
/opt/boxen/rbenv/versions/2.3.0/bin/ruby -r ./siteconf20160630-68248-1qaz7dk.rb extconf.rb
checking for libxml/xmlversion.h in /opt/include/libxml2,/opt/local/include/libxml2,/usr/local/include/libxml2,/usr/include/libxml2... 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.

Provided configuration options:
    --with-opt-dir
    --without-opt-dir
    --with-opt-include
    --without-opt-include=${opt-dir}/include
    --with-opt-lib
    --without-opt-lib=${opt-dir}/lib
    --with-make-prog
    --without-make-prog
    --srcdir=.
    --curdir
    --ruby=/opt/boxen/rbenv/versions/2.3.0/bin/$(RUBY_BASE_NAME)
    --with-xml2-config
    --without-xml2-config
    --with-xml2-dir
    --without-xml2-dir
    --with-xml2-include
    --without-xml2-include=${xml2-dir}/include
    --with-xml2-lib
    --without-xml2-lib=${xml2-dir}/lib
 extconf failure: need libxml2.

    Install the library or try one of the following options to extconf.rb:

      --with-xml2-config=/path/to/xml2-config
      --with-xml2-dir=/path/to/libxml2
      --with-xml2-lib=/path/to/libxml2/lib
      --with-xml2-include=/path/to/libxml2/include


To see why this extension failed to compile, please check the mkmf.log which can be found here:

  /opt/boxen/rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/extensions/x86_64-darwin-14/2.3.0-static/libxml-ruby-2.9.0/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in /opt/boxen/rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/libxml-ruby-2.9.0 for inspection.
Results logged to /opt/boxen/rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/extensions/x86_64-darwin-14/2.3.0-static/libxml-ruby-2.9.0/gem_make.out

这里显示了高级问题:

checking for libxml/xmlversion.h in /opt/include/libxml2,/opt/local/include/libxml2,/usr/local/include/libxml2,/usr/include/libxml2... no

基本上,安装程序会告诉您找不到 libxml/xmlversion.h,但更一般地说,它很可能找不到您的 libxml2 位置。它会告诉您它正在查找哪四个目录,因此第一步是检查这些目录以查看是否存在 libxml2

假设它不在这四个位置中的任何一个,您需要找到它。 运行 find / -name xmlversion.h(可能需要 sudo)找出 libxml2 的安装位置。基于 this answer 我猜你的问题是 brew 将它安装到 /usr/local/Cellar/libxml2 但你应该确认这一点。获得位置后,您可以手动将 gem install 命令指向它。像这样的东西:(但用你的实际路径填写path/to

gem install libxml-ruby --with-xml2-dir=/path/to/libxml2 --with-xml2-lib=/path/to/libxml2/lib --with-xml2-include=/path/to/libxml2/include

我在使用 brew 安装 libxml2 后遇到了类似的问题

Brew link 解决了问题 brew link --force libxml2

我也使用 brew 安装 libxml2 但需要以下选项:

brew install libxml2 --with-xml2-config
gem install libxml-ruby -- --with-xml2-config=/usr/local/homebrew/opt/libxml2/bin/xml2-config

升级到 macOS Mojave 后的生活。

xcode-select --install
brew install libxml2
brew link --force libxml2
gem install libxml-ruby -v '2.9.0' -- --use-system-libraries=true --with-xml2-include="$(xcrun --show-sdk-path)"/usr/include/libxml2

在我的示例中,我安装了版本 2.9.0 的 libxml-ruby gem。更改版本字符串以满足您的需要。如果可用,请检查 Gemfile.lock 以确定需要安装哪个版本的 libxml-ruby Bundler。

macOS 自带 libmxl2 安装,总是不需要先使用像 brew 这样的实用程序来安装它。然而 headers 不在 /usr/include/libxml 中,而是在 /usr/include/libxml2/libxml 中。

libxml-ruby gem 的配置脚本试图通过编译代码来检测 libxml 是否安装,如下所示:

#include <libxml/xmlversion.h>

但是当使用 Xcode 附带的编译器进行编译时,只有这段代码可以工作:

#include <libxml2/libxml/xmlversion.h>

诀窍是让配置脚本知道在哪里可以找到 headers。在安装了 Xcode 的 macOS 系统上,以下将起作用:

gem install libxml-ruby -v '3.1.0' -- --with-xml2-include=`xcrun --show-sdk-path`/usr/include/libxml2

自制软件:

libxml2 is keg-only, which means it was not symlinked into /usr/local, because macOS already provides this software and installing another version in parallel can cause all kinds of trouble.

所以,我们需要直接设置libxml路径

brew install libxml2

gem install libxml-ruby \
-- \
--with-xml2-config="$(brew --prefix libxml2)/bin/xml2-config"

P.S.

如果您在最新的红宝石上遇到异常:

无视就好。 gem 已成功安装。只是 Rdoc 文档生成失败。

The internal error was:

        (NoMethodError) undefined method `[]' for nil:NilClass

ERROR:  While executing gem ... (NoMethodError)
    undefined method `[]' for nil:NilClass

一个解决方案是:

cd /Library/Developer/CommandLineTools/Packages/ $ 
open macOS_SDK_headers_for_macOS_10.14.pkg

强制重新安装 macOS 10.14 headers。