Installing Capybara gem in Rails

Some problems I encountered before when trying to setup capybara. (Rails 4.1.5).

Below is the error message you get when you do bundle install after adding gem capybara to your Gemfile.

Error message:

Using mini_portile 0.6.0
Building nokogiri using packaged libraries.

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

    /Users/roy/.rvm/rubies/ruby-2.1.2/bin/ruby -r ./siteconf20140819-26712-14kvhdl.rb extconf.rb
Building nokogiri using packaged libraries.
libiconv is missing.  please visit 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.

Solution: Install Nokogiri

Check out

I'm using home brew so these steps worked for me:


brew install libxml2 libxslt
brew link libxml2 libxslt --force

I had to use --force which is not stated in the official installation guide.


tar xvfz libiconv-1.13.1.tar.gz
cd libiconv-1.13.1
./configure --prefix=/usr/local/Cellar/libiconv/1.13.1
sudo make install


gem install nokogiri -- --with-xml2-include=/usr/local/Cellar/libxml2/2.7.8/include/libxml2 --with-xml2-lib=/usr/local/Cellar/libxml2/2.7.8/lib --with-xslt-dir=/usr/local/Cellar/libxslt/1.1.26 --with-iconv-include=/usr/local/Cellar/libiconv/1.13.1/include --with-iconv-lib=/usr/local/Cellar/libiconv/1.13.1/lib

Yes, the directory for libxml2 and libxslt points to an empty directory because the installed version is actually 2.9.1 and 1.1.28 respectively. But it installs fine, so we'll move on.

You'll also notice the message:

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.

Again, Nokogiri seems to be installed properly so we'll move on.

Once Nokogiri is installed, you should be able to do bundle install now.

You might need to add config.include Capybara::DSL to rails_helper.rb too.

If you have any idea to install libiconv and Nokogiri without any errors, please do drop a note.

I have not tested this in depth, but just noting down the steps first.

- Dependancies hurt -

Installing Capybara gem in Rails
Share this