gem 错误安装 nokogiri 和捆绑安装中间人应用程序

Error FOR gem install nokogiri AND bundle install FOR Middleman app

我正在从头开始开发 Middleman 应用程序。每当我尝试 运行 服务器时,即 middleman server 我收到一个错误(如下),告诉我“安装 nokogiri (1.6.6.2) 时发生错误,Bundler 无法继续。”

但是,当我尝试安装 nokogiri (1.6.6.2) 时,我收到另一个错误(如下),提示我“错误:安装 nokogiri 时出错:错误:无法构建 gem 本机扩展。”

然后我尝试 bundle install 我的 gem 文件,但收到另一个错误。谁能告诉我我可能忽略了什么?

每当我尝试 运行 我的 middleman server 命令时,我得到这个:

/Library/Ruby/Site/2.0.0/rubygems/dependency.rb:315:in `to_specs': Could not find 'nokogiri' (>= 1.3.3) among 67 total gem(s) (Gem::LoadError)
Checked in 'GEM_PATH=/Users/Wilhelm/.gem/ruby/2.0.0:/Library/Ruby/Gems/2.0.0:/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/gems/2.0.0', execute `gem env` for more information
    from /Library/Ruby/Site/2.0.0/rubygems/specification.rb:1311:in `block in activate_dependencies'
    from /Library/Ruby/Site/2.0.0/rubygems/specification.rb:1300:in `each'
    from /Library/Ruby/Site/2.0.0/rubygems/specification.rb:1300:in `activate_dependencies'
    from /Library/Ruby/Site/2.0.0/rubygems/specification.rb:1282:in `activate'
    from /Library/Ruby/Site/2.0.0/rubygems/specification.rb:1314:in `block in activate_dependencies'
    from /Library/Ruby/Site/2.0.0/rubygems/specification.rb:1300:in `each'
    from /Library/Ruby/Site/2.0.0/rubygems/specification.rb:1300:in `activate_dependencies'
    from /Library/Ruby/Site/2.0.0/rubygems/specification.rb:1282:in `activate'
    from /Library/Ruby/Site/2.0.0/rubygems/core_ext/kernel_gem.rb:67:in `block in gem'
    from /Library/Ruby/Site/2.0.0/rubygems/core_ext/kernel_gem.rb:66:in `synchronize'
    from /Library/Ruby/Site/2.0.0/rubygems/core_ext/kernel_gem.rb:66:in `gem'
    from /usr/bin/middleman:22:in `<main>'

这是我的 Gemfile 的内容:

# If you do not have OpenSSL installed, update
# the following line to use "http://" instead
source 'https://rubygems.org'

gem "middleman", "~>3.4.0"

# Live-reloading plugin
gem "middleman-livereload", "~> 3.1.0"

# For faster file watcher updates on Windows:
gem "wdm", "~> 0.1.0", :platforms => [:mswin, :mingw]

# Windows does not come with time zone data
gem "tzinfo-data", platforms: [:mswin, :mingw, :jruby]

这是我的 bundle install:

的结果
Fetching gem metadata from https://rubygems.org/.............
Fetching version metadata from https://rubygems.org/...
Fetching dependency metadata from https://rubygems.org/..
Resolving dependencies...
Using i18n 0.7.0
Using json 1.8.3
Using minitest 5.8.0
Using thread_safe 0.3.5
Using tzinfo 1.2.2
Using activesupport 4.2.4
Using bundler 1.8.2
Using mime-types 2.6.1
Using mini_portile 0.6.2

Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby -r ./siteconf20150828-20513-wkpoce.rb extconf.rb --use-system-libraries
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.
libxml2 version 2.6.21 or later is required!
*** 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
    --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

extconf failed, exit code 1

Gem files will remain installed in /var/folders/zn/zh_8qnr573x4wgbjz6gqz72w0000gn/T/bundler20150828-20513-11hlulx/nokogiri-1.6.6.2/gems/nokogiri-1.6.6.2 for inspection.
Results logged to /var/folders/zn/zh_8qnr573x4wgbjz6gqz72w0000gn/T/bundler20150828-20513-11hlulx/nokogiri-1.6.6.2/extensions/universal-darwin-14/2.0.0/nokogiri-1.6.6.2/gem_make.out
An error occurred while installing nokogiri (1.6.6.2), and Bundler cannot
continue.
Make sure that `gem install nokogiri -v '1.6.6.2'` succeeds before bundling.

这是我的 sudo gem install nokogiri -v '1.6.6.2':

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

        /Users/Wilhelm/.rbenv/versions/1.9.3-dev/bin/ruby extconf.rb
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.
-----
The file "/usr/include/iconv.h" is missing in your build environment,
which means you haven't installed Xcode Command Line Tools properly.

To install Command Line Tools, try running `xcode-select --install` on
terminal and follow the instructions.  If it fails, open Xcode.app,
select from the menu "Xcode" - "Open Developer Tool" - "More Developer
Tools" to open the developer site, download the installer for your OS
version and run it.
-----
*** 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=/Users/Wilhelm/.rbenv/versions/1.9.3-dev/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


Gem files will remain installed in /Users/Wilhelm/.rbenv/versions/1.9.3-dev/lib/ruby/gems/1.9.1/gems/nokogiri-1.6.6.2 for inspection.
Results logged to /Users/Wilhelm/.rbenv/versions/1.9.3-dev/lib/ruby/gems/1.9.1/gems/nokogiri-1.6.6.2/ext/nokogiri/gem_make.out

我在几个系统配置中遇到过这个问题。它总是最终成为开发库依赖项。如果是我,一次一个,我会安装这些并尝试捆绑安装:

libxslt-dev
libxml2-dev
ruby-dev

取决于您的系统(我认为在 centos 上它们是 -devel 而不是 -dev)。

这个命令:

sudo gem install nokogiri -- --with-xml2-include=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/usr/include/libxml2 --use-system-libraries

this github issue 中找到,解决了我的问题。老实说,我不完全确定为什么要这样,如果有人可以附加评论,阐明到底发生了什么,我们将不胜感激。