戦場のプログラマー

名前は誰も知らない。

Macでvagrant-omnibusプラグインのインストール中にnokogiriのインストールでコケるというお話

Mac OS X Mavericks(10.9.4)です。他のMacでは vagrant-omnibus プラグインをインストールできるのですが、 メインで使ってるマシンでだけインストールできないという現象に遭遇。

どうやら nokogiri のインストールで失敗している模様。

% vagrant plugin install vagrant-omnibus
Installing the 'vagrant-omnibus' plugin. This can take a few minutes...
Building nokogiri using packaged libraries.
Bundler, the underlying system Vagrant uses to install plugins,
reported an error. The error is shown below. These errors are usually
caused by misconfigured plugin installations or transient network
issues. The error from Bundler is:

An error occurred while installing nokogiri (1.6.3.1), and Bundler cannot continue.
Make sure that `gem install nokogiri -v '1.6.3.1'` succeeds before bundling.

ためしに nokogiri 単体で入るか Gemfile に書いてインストールしてみたらこんなエラーが・・・

Building nokogiri using packaged libraries.

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

    /Users/wnoguchi/.rbenv/versions/2.0.0-p481/bin/ruby extconf.rb
Building nokogiri using packaged libraries.
-----
libiconv 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.

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/wnoguchi/.rbenv/versions/2.0.0-p481/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/wnoguchi/Documents/work/vendor/bundle/ruby/2.0.0/gems/nokogiri-1.6.3 for inspection.
Results logged to /Users/wnoguchi/Documents/work/vendor/bundle/ruby/2.0.0/gems/nokogiri-1.6.3/ext/nokogiri/gem_make.out
An error occurred while installing nokogiri (1.6.3), and Bundler cannot continue.
Make sure that `gem install nokogiri -v '1.6.3'` succeeds before bundling.

libiconv is missing. と。他のMacでは入れた記憶がないんだけどなあ。
入れましょう。

osx - Why does installing nokogiri on Mac OS fail with libiconv is missing? - Stack Overflow

によると

% brew update
% brew install libiconv

でいけるらしいですが、3年前の記事なのですでに情報が古くなってる可能性があります。
Lion とかの頃の話じゃないですか・・・。

% brew install libiconv
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.

失敗しました。
なんか下の方には自力でソース持ってきてビルドしてるっぽいけど、それはやりたくない。
なにか欠けているものがあるはず。

bash - Nokogiri gem fails to install in OS X Mavericks - Ask Different

これは違う。

ruby - Error to install Nokogiri on OSX 10.9 Maverick? - Stack Overflow

これっぽい。

After navigating the animated GIFs here, all that I had to do was simply xcode-select --install and the gem install nokogiri worked fine.

緑チェックが入っている複雑な手順の方じゃなくて

xcode-select --install

とすればよかった。
するとダイアログが開いて「"xcode-select"コマンドを実行するには、コマンドラインデベロッパツールが必要です。ツールを今すぐインストールしますか?」と聞いてくるので[インストール]をクリック。

f:id:wnoguchi0727:20140812175721p:plain

そしてCommand Line Tools 使用許諾契約に同意してインストール。

f:id:wnoguchi0727:20140812175756p:plain

そういえば他のMacでも同じことやってた気がする。
なるほど。

そしてもう一度トライ。

% vagrant plugin install vagrant-omnibus
Installing the 'vagrant-omnibus' plugin. This can take a few minutes...
Building nokogiri using packaged libraries.
Building libxml2-2.8.0 for nokogiri with the following patches applied:
    - 0001-Fix-parser-local-buffers-size-problems.patch
    - 0002-Fix-entities-local-buffers-size-problems.patch
    - 0003-Fix-an-error-in-previous-commit.patch
    - 0004-Fix-potential-out-of-bound-access.patch
    - 0005-Detect-excessive-entities-expansion-upon-replacement.patch
    - 0006-Do-not-fetch-external-parsed-entities.patch
    - 0007-Enforce-XML_PARSER_EOF-state-handling-through-the-pa.patch
    - 0008-Improve-handling-of-xmlStopParser.patch
    - 0009-Fix-a-couple-of-return-without-value.patch
    - 0010-Keep-non-significant-blanks-node-in-HTML-parser.patch
    - 0011-Do-not-fetch-external-parameter-entities.patch
************************************************************************
IMPORTANT!  Nokogiri builds and uses a packaged version of libxml2.

If this is a concern for you and you want to use the system library
instead, abort this installation process and reinstall nokogiri as
follows:

    gem install nokogiri -- --use-system-libraries

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

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

However, note that nokogiri does not necessarily support all versions
of libxml2.

For example, libxml2-2.9.0 and higher are currently known to be broken
and thus unsupported by nokogiri, due to compatibility problems and
XPath optimization bugs.
************************************************************************
Building libxslt-1.1.28 for nokogiri with the following patches applied:
    - 0001-Adding-doc-update-related-to-1.1.28.patch
    - 0002-Fix-a-couple-of-places-where-f-printf-parameters-wer.patch
    - 0003-Initialize-pseudo-random-number-generator-with-curre.patch
    - 0004-EXSLT-function-str-replace-is-broken-as-is.patch
    - 0006-Fix-str-padding-to-work-with-UTF-8-strings.patch
    - 0007-Separate-function-for-predicate-matching-in-patterns.patch
    - 0008-Fix-direct-pattern-matching.patch
    - 0009-Fix-certain-patterns-with-predicates.patch
    - 0010-Fix-handling-of-UTF-8-strings-in-EXSLT-crypto-module.patch
    - 0013-Memory-leak-in-xsltCompileIdKeyPattern-error-path.patch
    - 0014-Fix-for-bug-436589.patch
    - 0015-Fix-mkdir-for-mingw.patch
************************************************************************
IMPORTANT!  Nokogiri builds and uses a packaged version of libxslt.

If this is a concern for you and you want to use the system library
instead, abort this installation process and reinstall nokogiri as
follows:

    gem install nokogiri -- --use-system-libraries

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

    bundle config build.nokogiri --use-system-libraries
    bundle install
************************************************************************
Installed the plugin 'vagrant-omnibus (1.4.1)'!

今度はうまくいきました。

vagrant-omnibusを使うと何が美味しいかというと、vagrant upvagrant provision したときに Chef が入ってなかったら自動的にインストールしてくれるというシロモノです。
Vagrantfileには以下の記述を追記しておきます。

config.omnibus.chef_version = :latest

まあでもこうしちゃうと Vagrantfile に依存関係が増えてなんだか複雑な気分になりますが・・・。

References

  1. schisamo/vagrant-omnibus
  2. sparklemotion/nokogiri