OS X Lion,尝试安装Nokogiri - libxml2缺失

33
sudo gem install nokogiri
Building native extensions.  This could take a while...
ERROR:  Error installing nokogiri:
    ERROR: Failed to build gem native extension.

        /Users/sajeev86/.rvm/rubies/ruby-1.8.7-p352/bin/ruby extconf.rb
checking for libxml/parser.h... no
-----
libxml2 is missing.  please visit http://nokogiri.org/tutorials/installing_nokogiri.html for help with installing dependencies.
-----
*** 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.

我无法安装Nokogiri。这个问题困扰了我一段时间。我已经通过MacPorts和Homebrew安装了依赖项。

我感觉可能是目录没有正确指向彼此?但是我不知道如何修复。

17个回答

43
在Mavericks中,使用Homebrew安装库并在安装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"


5
export NOKOGIRI_USE_SYSTEM_LIBRARIES=1 也帮助了我的 bundle install,谢谢。 - Patru
2
我不必卸载nokogiri,因为我使用port只是尝试了NOKOGIRI_USE_SYSTEM_LIBRARIES=1 gem install nokogiri,这对我起了作用。 - Andrew Schwartz
谢谢。完美地工作了。 - Sharn White

32

在这里的所有其他答案对我都没有用。

在最新版本的Mavericks上,唯一成功的方法是以下方法:

$ brew install libxml2 libxslt libiconv
$ sudo gem install nokogiri -v '1.6.5' -- --use-system-libraries --with-xml2-include=/usr/include/libxml2 --with-xml2-lib=/usr/lib

至少截至今天,由于“libiconv没有可用的配方”(并建议只使用苹果提供的副本),因此该brew命令失败。然后,建议的gem行也会出现相同的“构建gem本机扩展失败”。 - Tommy
好的,汤米...没问题。尝试手动安装libiconv,然后从brew install命令中删除那个单词。 wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.13.1.tar.gz; tar xvfz libiconv-1.13.1.tar.gz; cd libiconv-1.13.1; ./configure --prefix=/usr/local/Cellar/libiconv/1.13.1; make; sudo make install; - Crates
这在High Sierra /w noko版本1.8.2上也能正常工作。 - Brian

16

在Lion系统上我遇到了同样的问题。花了好几个小时读帖子,尝试homebrew、macports等方法。当我发现了这篇帖子并阅读了.rvm/gems/ruby-1.8.7-p358/gems/nokogiri-1.4.4/ext/nokogiri/mkmf.log文件的内容,我看到了以下信息:

find_header: checking for libxml/parser.h... -------------------- no

"/usr/bin/gcc-4.2 -E -I. -I/Users/me/.rvm/rubies/ruby-1.8.7-p358/lib/ruby/1.8/i686-darwin10.8.0 -I. -I-I-I/opt/local/include -I-I-I/usr/local/include -I-I-I/Users/me/.rvm/rubies/ruby-1.8.7-p358/include -I-I-I/usr/include -I-I-I/usr/include/libxml2 -I/opt/local/include/libxml2 -I/usr/local/include/libxml2 -I/Users/me/.rvm/rubies/ruby-1.8.7-p358/include/libxml2 -I-I/opt/local/include -I-I/usr/local/include -I-I/Users/me/.rvm/rubies/ruby-1.8.7-p358/include -I-I/usr/include -I-I/usr/include/libxml2 -I/opt/local/include -I/usr/local/include -I/Users/me/.rvm/rubies/ruby-1.8.7-p358/include -I/usr/include -I/usr/include/libxml2  -D_XOPEN_SOURCE -D_DARWIN_C_SOURCE   -g -O2  -fno-common -pipe -fno-common    -g -DXP_UNIX -O3 -Wall -Wcast-qual -Wwrite-strings -Wconversion -Wmissing-noreturn -Winline  conftest.c -o conftest.i"
checked program was:
/* begin */
1: #include <libxml/parser.h>
/* end */

然而,gcc-4.2不存在:

$ ls /usr/bin/gcc*
/usr/bin/gcc
因此,我通过将gcc-4.2建立为指向gcc的符号链接来解决了问题,并成功安装了nokogiri。
sudo ln -s gcc gcc-4.2

10

我在Maverick上遇到了相同的问题,解决方法是

xcode-select --install

然而,由于苹果下载页面上没有公开提供该安装程序,因此那并不起作用。如果您想要Mavricks的命令行工具,则需要拥有付费帐户,然后才能安装它。一旦安装完成,您将不会遇到Nokogiri的这个问题。


9

检查错误信息中给定的网址(http://nokogiri.org/tutorials/installing_nokogiri.html),我认为问题是您使用了哪个版本的Homebrew?

brew -v

我想更新到0.8+版本。

brew update
brew upgrade

然后再尝试安装:

brew install libxml2 libxslt
brew link libxml2 libxslt
gem install nokogiri

1
我以前没有遇到过这个问题,你可以先尝试执行 "brew unlink libxml2" 然后再执行 "brew link libxml2"。 - knutsel
链接 /usr/local/Cellar/libxml2/2.7.8... 创建了 21 个符号链接 链接 /usr/local/Cellar/libxslt/1.1.26... 创建了 22 个符号链接,但仍然返回 libxml2 丢失的消息。 - Sajeev Shanmuganandarajah
当我尝试运行 brew link libxml2 libxslt 时,出现错误提示:它“拒绝链接macOS提供/遮蔽的软件”。 - Qasim
安装成功了吗?没有奇怪的错误信息吗? (brew install libxml2 libxslt) - knutsel

7

对我而言,它有助于:

brew install libxml2 libxslt

如果出现权限拒绝错误,可以使用以下命令:sudo chown -R $USER /Library/Caches/Homebrew

sudo env ARCHFLAGS="-arch x86_64"

export NOKOGIRI_USE_SYSTEM_LIBRARIES=1

NOKOGIRI_USE_SYSTEM_LIBRARIES=1 gem install nokogiri

6
brew install libxml2 libxslt 
gem install nokogiri -v '1.6.5' -- --use-system-libraries

1
如果您向该问题的提出者解释这里正在发生什么以及它为什么有效,那就太好了。您知道,“授人以鱼不如授人以渔”。 - zxq9

3

如果您正在使用MacPorts并遇到此问题,请按照以下步骤操作:

将/include目录添加到您的.profile文件中,这样nokogiri就知道在哪里找到它。

例如:

MacPorts会自动向您的.profile文件中写入以下行:

export PATH=/opt/local/bin:/opt/local/sbin:$PATH

首先,像这样添加/include路径:
export PATH=/opt/local/bin:/opt/local/sbin:/opt/local/include/:$PATH

接着,在同一个终端窗口中运行以下命令:

source ~/.profile

这条命令告诉您的终端重新检查.profile文件中是否有任何更改。

最后,尝试从同一个终端窗口安装gem。

sudo gem install nokogiri

3
在Yosemite上,我使用了:
brew install libxml2 libxslt
sudo gem install nokogiri -v '1.6.5' -- --use-system-libraries --with-xml2-include=/usr/include/libxml2 --with-xml2-lib=/usr/lib

非常接近上述命令,但不需要使用libconv。

2

对于我的有点过时的系统,遇到了上述问题,具体来说:

FENG-SHUI:demo Eric$ brew install libxml2
==> Downloading ftp://xmlsoft.org/libxml2/libxml2-2.9.0.tar.gz
######################################################################## 100.0%
==> Downloading patches
######################################################################## 100.0%
######################################################################## 100.0%
==> Patching patching file threads.c patching file xpath.c
==> ./configure --prefix=/usr/local/Cellar/libxml2/2.9.0 --without-python
==> make
==> make install
==> Caveats This formula is keg-only: so it was not symlinked into /usr/local.

Mac OS X already provides this software and installing another version in parallel can cause all kinds of trouble.

Generally there are no consequences of this for you. If you build your own software and it requires this formula, you'll need to add to your build variables:

    LDFLAGS:  -L/usr/local/opt/libxml2/lib
    CPPFLAGS: -I/usr/local/opt/libxml2/include

Error: Failed to create: /usr/local/opt/libxml2 
Things that depend on libxml2 will probably not build.
    ==> Summary /usr/local/Cellar/libxml2/2.9.0: 273 files, 11M, built in 2.0 minutes 

FENG-SHUI:demo Eric$ brew install libxml2 libxslt 
Error: libxml2-2.9.0 already installed 
Error: /usr/local/opt/libxml2 not present or broken Please reinstall libxml2. Sorry :(

这是让libxml2和libxslt顺利运行的神奇公式:
# latest version 2.9.0
brew install libxml2

# installing libxslt from source code
# get latest libxslt from ftp://xmlsoft.org/libxml2/libxslt-git-snapshot.tar.gz..
# then
./configure --prefix=/usr/local/Cellar/libxslt/1.1.28 --with-libxml-prefix=/usr/local/Cellar/libxml2/2.9.0
make
sudo make install

# installing nokogiri with this new compiled libs
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

感谢这位大佬:https://gist.github.com/fabioyamate/443160

希望这篇文章能够帮助到某些人!


这个方法适用于在具有libxslt 1.1.28和libxml2 2.9.1的OSX 10.8.4上安装nokogiri 1.6.0 - 谢谢! - Gon Zifroni
threads.c:918:20: error: expected expression once_control = PTHREAD_ONCE_INIT; ^ /usr/include/pthread.h:203:27: note: expanded from macro 'PTHREAD_ONCE_INIT' #define PTHREAD_ONCE_INIT {_PTHREAD_ONCE_SIG_init, {0}} - A H K

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接