Gem 关于 Nokogiri 的安装依赖 libxml2-2.9.4 报错的问题

runup · June 10, 2016 · Last by LiinNs replied at May 29, 2019 · 10956 hits

在自己的 os x 系统上一直运行正常,包括正常使用 nokogiri 这个 gem,今天在本地建立新项目,bundle install 报 nokogiri 安装异常。 单独 gem install nokogiri,报下面的错误:

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

    current directory: /Users/jayzen/.rvm/gems/ruby-2.3.0/gems/nokogiri-1.6.8/ext/nokogiri
/Users/jayzen/.rvm/rubies/ruby-2.3.0/bin/ruby -r ./siteconf20160610-32051-16q0bpc.rb extconf.rb
Using pkg-config version 1.1.7
checking if the C compiler accepts ... yes
checking if the C compiler accepts -Wno-error=unused-command-line-argument-hard-error-in-future... no
Building nokogiri using packaged libraries.
Using mini_portile version 2.1.0
checking for iconv.h... yes
checking for gzdopen() in -lz... yes
checking for iconv using --with-opt-* flags... yes
************************************************************************
IMPORTANT NOTICE:

Building Nokogiri with a packaged version of libxml2-2.9.4.

Team Nokogiri will keep on doing their best to provide security
updates in a timely manner, but if this is a concern for you and want
to use the system library instead; abort this installation process and
reinstall nokogiri as follows:

    gem install nokogiri -- --use-system-libraries
        [--with-xml2-config=/path/to/xml2-config]
        [--with-xslt-config=/path/to/xslt-config]

If you are using Bundler, tell it to use the option:

    bundle config build.nokogiri --use-system-libraries
    bundle install

Note, however, that nokogiri is not fully compatible with arbitrary
versions of libxml2 provided by OS/package vendors.
************************************************************************
Extracting libxml2-2.9.4.tar.gz into tmp/x86_64-apple-darwin15.2.0/ports/libxml2/2.9.4... OK
Running 'configure' for libxml2 2.9.4... OK
Running 'compile' for libxml2 2.9.4... ERROR, review '/Users/jayzen/.rvm/gems/ruby-2.3.0/gems/nokogiri-1.6.8/ext/nokogiri/tmp/x86_64-apple-darwin15.2.0/ports/libxml2/2.9.4/compile.log' to see what happened. Last lines are:
========================================================================
    unsigned short* in = (unsigned short*) inb;
                         ^~~~~~~~~~~~~~~~~~~~~
encoding.c:815:27: warning: cast from 'unsigned char *' to 'unsigned short *' increases required alignment from 1 to 2 [-Wcast-align]
    unsigned short* out = (unsigned short*) outb;
                          ^~~~~~~~~~~~~~~~~~~~~~
4 warnings generated.
  CC       error.lo
  CC       parserInternals.lo
  CC       parser.lo
  CC       tree.lo
  CC       hash.lo
  CC       list.lo
  CC       xmlIO.lo
xmlIO.c:1450:52: error: use of undeclared identifier 'LZMA_OK'
    ret =  (__libxml2_xzclose((xzFile) context) == LZMA_OK ) ? 0 : -1;
                                                   ^
1 error generated.
make[2]: *** [xmlIO.lo] Error 1
make[1]: *** [all-recursive] Error 1
make: *** [all] Error 2
========================================================================
*** 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
    --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=/Users/jayzen/.rvm/rubies/ruby-2.3.0/bin/$(RUBY_BASE_NAME)
    --help
    --clean
    --use-system-libraries
    --enable-static
    --disable-static
    --with-zlib-dir
    --without-zlib-dir
    --with-zlib-include
    --without-zlib-include=${zlib-dir}/include
    --with-zlib-lib
    --without-zlib-lib=${zlib-dir}/lib
    --enable-cross-build
    --disable-cross-build
/Users/jayzen/.rvm/gems/ruby-2.3.0/gems/mini_portile2-2.1.0/lib/mini_portile2/mini_portile.rb:366:in `block in execute': Failed to complete compile task (RuntimeError)
    from /Users/jayzen/.rvm/gems/ruby-2.3.0/gems/mini_portile2-2.1.0/lib/mini_portile2/mini_portile.rb:337:in `chdir'
    from /Users/jayzen/.rvm/gems/ruby-2.3.0/gems/mini_portile2-2.1.0/lib/mini_portile2/mini_portile.rb:337:in `execute'
    from /Users/jayzen/.rvm/gems/ruby-2.3.0/gems/mini_portile2-2.1.0/lib/mini_portile2/mini_portile.rb:111:in `compile'
    from /Users/jayzen/.rvm/gems/ruby-2.3.0/gems/mini_portile2-2.1.0/lib/mini_portile2/mini_portile.rb:150:in `cook'
    from extconf.rb:364:in `block (2 levels) in process_recipe'
    from extconf.rb:257:in `block in chdir_for_build'
    from extconf.rb:256:in `chdir'
    from extconf.rb:256:in `chdir_for_build'
    from extconf.rb:363:in `block in process_recipe'
    from extconf.rb:262:in `tap'
    from extconf.rb:262:in `process_recipe'
    from extconf.rb:555:in `<main>'

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

  /Users/jayzen/.rvm/gems/ruby-2.3.0/extensions/x86_64-darwin-15/2.3.0/nokogiri-1.6.8/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in /Users/jayzen/.rvm/gems/ruby-2.3.0/gems/nokogiri-1.6.8 for inspection.
Results logged to /Users/jayzen/.rvm/gems/ruby-2.3.0/extensions/x86_64-darwin-15/2.3.0/nokogiri-1.6.8/gem_make.out

按照提示写如下代码:gem install nokogiri -- --use-system-libraries,出现如下错误:

Building native extensions with: '--use-system-libraries'
This could take a while...
ERROR:  Error installing nokogiri:
    ERROR: Failed to build gem native extension.

    current directory: /Users/jayzen/.rvm/gems/ruby-2.3.0/gems/nokogiri-1.6.8/ext/nokogiri
/Users/jayzen/.rvm/rubies/ruby-2.3.0/bin/ruby -r ./siteconf20160610-36324-vvy8wk.rb extconf.rb --use-system-libraries
Using pkg-config version 1.1.7
checking if the C compiler accepts ... yes
checking if the C compiler accepts -Wno-error=unused-command-line-argument-hard-error-in-future... no
Building nokogiri using system libraries.
ERROR: cannot discover where libxml2 is located on your system. please make sure `pkg-config` is installed.
*** 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
    --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=/Users/jayzen/.rvm/rubies/ruby-2.3.0/bin/$(RUBY_BASE_NAME)
    --help
    --clean
    --use-system-libraries
    --with-zlib-dir
    --without-zlib-dir
    --with-zlib-include
    --without-zlib-include=${zlib-dir}/include
    --with-zlib-lib
    --without-zlib-lib=${zlib-dir}/lib
    --with-xml2-dir
    --without-xml2-dir
    --with-xml2-include
    --without-xml2-include=${xml2-dir}/include
    --with-xml2-lib
    --without-xml2-lib=${xml2-dir}/lib
    --with-libxml-2.0-config
    --without-libxml-2.0-config
    --with-pkg-config
    --without-pkg-config
    --with-xslt-dir
    --without-xslt-dir
    --with-xslt-include
    --without-xslt-include=${xslt-dir}/include
    --with-xslt-lib
    --without-xslt-lib=${xslt-dir}/lib
    --with-libxslt-config
    --without-libxslt-config
    --with-exslt-dir
    --without-exslt-dir
    --with-exslt-include
    --without-exslt-include=${exslt-dir}/include
    --with-exslt-lib
    --without-exslt-lib=${exslt-dir}/lib
    --with-libexslt-config
    --without-libexslt-config

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

  /Users/jayzen/.rvm/gems/ruby-2.3.0/extensions/x86_64-darwin-15/2.3.0/nokogiri-1.6.8/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in /Users/jayzen/.rvm/gems/ruby-2.3.0/gems/nokogiri-1.6.8 for inspection.
Results logged to /Users/jayzen/.rvm/gems/ruby-2.3.0/extensions/x86_64-darwin-15/2.3.0/nokogiri-1.6.8/gem_make.out

使用 brew 安装 pkg-config libxml2 libxslt,卸载,重新安装均无效,社区中相关的提问的解决方案采取无效,折腾了一天,在其他平台上也没有找到合适的解决方案,求问?

目前已经解决,解决方法参考7#楼意见,终端执行 xcode-select --install

你参考一下这个能不能解决你的问题

bundle config build.nokogiri --use-system-libraries --with-xml2-include=/usr/include/libxml2/

#1 楼 @zhulinpinyu 还是不行,执行这段代码,然后执行 gem install nokogiri,报上面同样的错误。

mac os x 的话试试: cd 到项目

brew install libxml2 
brew link --force libxml2
bundle config build.nokogiri --use-system-libraries
bundle install
4 Floor has deleted

#3 楼 @chenjau 还是不行,报的还是上面罗列出来的问题

google 了下貌似有人说是 xcode command line tools 的问题。反正试试吧 xcode-select --install, 然后再看看

#6 楼 @chenjau binggo ~感谢。查了文档,是这个问题

ERROR: cannot discover where libxml2 is located on your system. please make sure `pkg-config` is installed.

注意看 Nokogiri 的错误提示,经过那么多年,Nokogiri 的提示已经非常完善了,一半都会在错误里面准确说明你的系统到底缺什么

#8 楼 @huacnlee 有注意到这段提示,但是按照提示操作安装,没有成功,参考了6#的意见,执行 xcode-select --install ,后来这个问题没有了,谢谢指点。

遇到同样的问题,不过最后还是这样解决了 对于系统是的话 El Capitan,执行如下命令:

gem install nokogiri -v '1.6.8' -- --use-system-libraries=true --with-xml2-include=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/libxml2

如果按照 #1,执行了 bundle config build.nokogiri --use-system-libraries --with-xml2-include=/usr/include/libxml2/
你就会在当前用户目录下的 .bundle发现一个config文件,然后在这个文件里头有这么一行代码:

BUNDLE_BUILD__NOKOGIRI: "--use-system-libraries --with-xml2-include=/usr/include/libxml2/"

config 的内容这应该是 bundler 管理 gem 的 配置文件,一般我有这样一个习惯:
在项目根目录下新建一个 .bundle目录,该目录下面有 config文件,那么我们执行 bundle install时就会依据 config 文件中我们写的规则,例如:

---
BUNDLE_PATH: vendor/bundle
BUNDLE_WITHOUT: test:postgres
BUNDLE_DISABLE_SHARED_GEMS: true
BUNDLE_BUILD__NOKOGIRI: "--use-system-libraries --with-xml2-include=/usr/include/libxml2/"

  • 该项目所用到的 gem 将会 install 到 vendor/bundle 目录下

  • 然后执行到 Installing nokogiri 1.6.8 with native extensions时就会使用 config中配置的
    BUNDLE_BUILD__NOKOGIRI: "--use-system-libraries --with-xml2-include=/usr/include/libxml2/"

另,BUNDLE_PATH: vendor/bundle 有一个好处就是该项目中使用到的 gem 只会在该项目下,而不是 install 到 ruby 安装的目录下,这对强迫症来说是极好的。

我也遇到了同样的问题,但是用 xcode-select --install 无法解决,请问如何解决,谢谢!

报错误为

Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.

    /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby extconf.rb --use-system-libraries --with-xml2-include=/usr/local/opt/libxml2/include/libxml2
Using pkg-config version 1.1.7
checking if the C compiler accepts ... yes
checking if the C compiler accepts -Wno-error=unused-command-line-argument-hard-error-in-future... no
Building nokogiri using system libraries.
checking for xmlParseDoc() in libxml/parser.h... no
checking for xmlParseDoc() in -lxml2... no
checking for xmlParseDoc() in -llibxml2... no
-----
libxml2 is missing.  Please locate mkmf.log to investigate how it is failing.
-----
*** 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=/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby
    --help
    --clean
    --use-system-libraries
    --with-zlib-dir
    --without-zlib-dir
    --with-zlib-include
    --without-zlib-include=${zlib-dir}/include
    --with-zlib-lib
    --without-zlib-lib=${zlib-dir}/lib
    --with-xml2-dir
    --without-xml2-dir
    --with-xml2-include=${xml2-dir}/include
    --with-xml2-lib
    --without-xml2-lib=${xml2-dir}/lib
    --with-xslt-dir
    --without-xslt-dir
    --with-xslt-include
    --without-xslt-include=${xslt-dir}/include
    --with-xslt-lib
    --without-xslt-lib=${xslt-dir}/lib
    --with-libxslt-config
    --without-libxslt-config
    --with-pkg-config
    --without-pkg-config
    --with-exslt-dir
    --without-exslt-dir
    --with-exslt-include
    --without-exslt-include=${exslt-dir}/include
    --with-exslt-lib
    --without-exslt-lib=${exslt-dir}/lib
    --with-libexslt-config
    --without-libexslt-config
    --with-xml2lib
    --without-xml2lib
    --with-libxml2lib
    --without-libxml2lib


Gem files will remain installed in /var/folders/5t/b1mb50q14xsd3rb6_ljrc6x00000gn/T/bundler20160812-1269-1dhoqecnokogiri-1.6.8/gems/nokogiri-1.6.8 for inspection.
Results logged to /var/folders/5t/b1mb50q14xsd3rb6_ljrc6x00000gn/T/bundler20160812-1269-1dhoqecnokogiri-1.6.8/gems/nokogiri-1.6.8/ext/nokogiri/gem_make.out

我执行的命令为 bundle install 我又执行命令 sudo gem install nokogiri -v '1.6.8' 报错误

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

    /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby extconf.rb
Using pkg-config version 1.1.7
checking if the C compiler accepts ... yes
checking if the C compiler accepts -Wno-error=unused-command-line-argument-hard-error-in-future... no
Building nokogiri using packaged libraries.
Using mini_portile version 2.1.0
checking for iconv.h... yes
checking for gzdopen() in -lz... yes
checking for iconv... yes
************************************************************************
IMPORTANT NOTICE:

Building Nokogiri with a packaged version of libxml2-2.9.4.

Team Nokogiri will keep on doing their best to provide security
updates in a timely manner, but if this is a concern for you and want
to use the system library instead; abort this installation process and
reinstall nokogiri as follows:

    gem install nokogiri -- --use-system-libraries
        [--with-xml2-config=/path/to/xml2-config]
        [--with-xslt-config=/path/to/xslt-config]

If you are using Bundler, tell it to use the option:

    bundle config build.nokogiri --use-system-libraries
    bundle install

Note, however, that nokogiri is not fully compatible with arbitrary
versions of libxml2 provided by OS/package vendors.
************************************************************************
Extracting libxml2-2.9.4.tar.gz into tmp/x86_64-apple-darwin15/ports/libxml2/2.9.4... OK
Running 'configure' for libxml2 2.9.4... OK
Running 'compile' for libxml2 2.9.4... ERROR, review '/Library/Ruby/Gems/2.0.0/gems/nokogiri-1.6.8/ext/nokogiri/tmp/x86_64-apple-darwin15/ports/libxml2/2.9.4/compile.log' to see what happened. Last lines are:
========================================================================
    unsigned short* in = (unsigned short*) inb;
                         ^~~~~~~~~~~~~~~~~~~~~
encoding.c:815:27: warning: cast from 'unsigned char *' to 'unsigned short *' increases required alignment from 1 to 2 [-Wcast-align]
    unsigned short* out = (unsigned short*) outb;
                          ^~~~~~~~~~~~~~~~~~~~~~
4 warnings generated.
  CC       error.lo
  CC       parserInternals.lo
  CC       parser.lo
  CC       tree.lo
  CC       hash.lo
  CC       list.lo
  CC       xmlIO.lo
xmlIO.c:1450:52: error: use of undeclared identifier 'LZMA_OK'
    ret =  (__libxml2_xzclose((xzFile) context) == LZMA_OK ) ? 0 : -1;
                                                   ^
1 error generated.
make[2]: *** [xmlIO.lo] Error 1
make[1]: *** [all-recursive] Error 1
make: *** [all] Error 2
========================================================================
*** 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=/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby
    --help
    --clean
    --use-system-libraries
    --enable-static
    --disable-static
    --with-zlib-dir
    --without-zlib-dir
    --with-zlib-include
    --without-zlib-include=${zlib-dir}/include
    --with-zlib-lib
    --without-zlib-lib=${zlib-dir}/lib
    --enable-cross-build
    --disable-cross-build
/Library/Ruby/Gems/2.0.0/gems/mini_portile2-2.1.0/lib/mini_portile2/mini_portile.rb:366:in `block in execute': Failed to complete compile task (RuntimeError)
    from /Library/Ruby/Gems/2.0.0/gems/mini_portile2-2.1.0/lib/mini_portile2/mini_portile.rb:337:in `chdir'
    from /Library/Ruby/Gems/2.0.0/gems/mini_portile2-2.1.0/lib/mini_portile2/mini_portile.rb:337:in `execute'
    from /Library/Ruby/Gems/2.0.0/gems/mini_portile2-2.1.0/lib/mini_portile2/mini_portile.rb:111:in `compile'
    from /Library/Ruby/Gems/2.0.0/gems/mini_portile2-2.1.0/lib/mini_portile2/mini_portile.rb:150:in `cook'
    from extconf.rb:364:in `block (2 levels) in process_recipe'
    from extconf.rb:257:in `block in chdir_for_build'
    from extconf.rb:256:in `chdir'
    from extconf.rb:256:in `chdir_for_build'
    from extconf.rb:363:in `block in process_recipe'
    from extconf.rb:262:in `tap'
    from extconf.rb:262:in `process_recipe'
    from extconf.rb:555:in `<main>'


Gem files will remain installed in /Library/Ruby/Gems/2.0.0/gems/nokogiri-1.6.8 for inspection.
Results logged to /Library/Ruby/Gems/2.0.0/gems/nokogiri-1.6.8/ext/nokogiri/gem_make.out

#14 楼 @caishuhao2000 除了运行 xcode-select --install#3 楼 的命令也需要运行,你现在应该是少了 libxml2。

problem resolved, thx @huacnlee

#19 楼 @hooopo 哈哈哈,就算是经验丰富的老手,多年以后依然还是都会一样遇到 Nokogiri 安装的问题 😄 😆

@runup 试试执行这两句,当然前提是通过系统安装好所有的依赖库:

bundle config build.nokogiri --use-system-libraries
bundle install
Reply to huacnlee

问题解决,多谢

有时候试了很多种方法反复安装都不生效时,你应该尝试重启下电脑。—— 来自一个浪费了 4 个小时可怜虫 https://nokogiri.org/tutorials/installing_nokogiri.html#mac_os_x

You need to Sign in before reply, if you don't have an account, please Sign up first.