How to `bundle' install or update with capybara include nokogiri

この記事は古くなりました。
以下を参照して下さい。

[Rails - How to install "nokogiri". - Qiita](http://qiita.com/maangie/items/6cf3a2ab2815c580343c)
***

capybara を使っているのですが…。

```rb:Gemfile
group :test do
  gem 'capybara'
end
```

bundle update すると以下の様なエラーになりました。

```shell-session
Using mini_portile 0.6.1

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

    /Users/maangie/.rvm/rubies/ruby-2.1.4/bin/ruby -r ./siteconf20141108-54032-maimfn.rb 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.
checking for iconv using --with-opt-* flags... yes
************************************************************************
IMPORTANT NOTICE:

Buidling Nokogiri with a packaged version of libxml2-2.9.2
with the following patches applied:
	- 0001-Revert-Missing-initialization-for-the-catalog-module.patch
	- 0002-Fix-missing-entities-after-CVE-2014-3660-fix.patch

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.2.tar.gz into tmp/x86_64-apple-darwin14.0.0/ports/libxml2/2.9.2... OK
Running patch with /Users/maangie/.rvm/gems/ruby-2.1.4/gems/nokogiri-1.6.4.1/ports/patches/libxml2/0001-Revert-Missing-initialization-for-the-catalog-module.patch...
Running 'patch' for libxml2 2.9.2... OK
Running patch with /Users/maangie/.rvm/gems/ruby-2.1.4/gems/nokogiri-1.6.4.1/ports/patches/libxml2/0002-Fix-missing-entities-after-CVE-2014-3660-fix.patch...
Running 'patch' for libxml2 2.9.2... OK
Running 'configure' for libxml2 2.9.2... OK
Running 'compile' for libxml2 2.9.2... ERROR, review '/Users/maangie/.rvm/gems/ruby-2.1.4/gems/nokogiri-1.6.4.1/ext/nokogiri/tmp/x86_64-apple-darwin14.0.0/ports/libxml2/2.9.2/compile.log' to see what happened.
*** 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/maangie/.rvm/rubies/ruby-2.1.4/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
/Users/maangie/.rvm/gems/ruby-2.1.4/gems/mini_portile-0.6.1/lib/mini_portile.rb:279:in `block in execute': Failed to complete compile task (RuntimeError)
	from /Users/maangie/.rvm/gems/ruby-2.1.4/gems/mini_portile-0.6.1/lib/mini_portile.rb:271:in `chdir'
	from /Users/maangie/.rvm/gems/ruby-2.1.4/gems/mini_portile-0.6.1/lib/mini_portile.rb:271:in `execute'
	from /Users/maangie/.rvm/gems/ruby-2.1.4/gems/mini_portile-0.6.1/lib/mini_portile.rb:70:in `compile'
	from /Users/maangie/.rvm/gems/ruby-2.1.4/gems/mini_portile-0.6.1/lib/mini_portile.rb:110:in `cook'
	from extconf.rb:268:in `block in process_recipe'
	from extconf.rb:167:in `tap'
	from extconf.rb:167:in `process_recipe'
	from extconf.rb:455:in `<main>'

extconf failed, exit code 1

Gem files will remain installed in /Users/maangie/.rvm/gems/ruby-2.1.4/gems/nokogiri-1.6.4.1 for inspection.
Results logged to /Users/maangie/.rvm/gems/ruby-2.1.4/extensions/x86_64-darwin-14/2.1.0/nokogiri-1.6.4.1/gem_make.out
An error occurred while installing nokogiri (1.6.4.1), and Bundler cannot
continue.
Make sure that `gem install nokogiri -v '1.6.4.1'` succeeds before bundling.
```

[nokogiri 1.6.4.1](https://www.ruby-forum.com/topic/6151812) が出た所為らしいです。

以下の様に、libxml2 をインストールした上で、環境変数を設定して実行するとうまくいきました。bundle install でコケる場合でも同様にすればオッケーです。

```shell-session
% brew install libxml2
% NOKOGIRI_USE_SYSTEM_LIBRARIES=1 bundle update
```

cf. http://qiita.com/suu_g/items/fcf549e16c797a9d7dc0

理由は上記に解説してありますが、Gemfile 内で環境変数を設定する方法はうまくいきませんでした。なんでやろ…。