Installing Nokogiri gem

Fermín Alejandro Moreno C.
2 min readNov 3, 2016

--

If you read my last story, you already know that I messed up with my computer while I was installing PostgreSQL. Sadly for me, problems for me didn’t stop with my rails local server, I also couldn’t run my rails console, so I decided to reinstall ruby with rvm. Everything was going well until my computer tried to automatically install the gem nokogiri. I tried installing the gem manually with:

$ gem install nokogiri

Unfortunately this didn’t work and I just kept getting messages like this one:

ERROR: Error installing nokogiri:ERROR: Failed to build gem native extension.

After some research I found out this is an error pertaining to OSX and is often very common. This happens because Apple doesn’t have a system accesible installation of libxml2, which nokogiri requires to install properly. Luckily, Xcode comes with a version of libxml2 bundled, so all you need to do is to specify that and install nokogiri. The way to do this is with the following command:

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

The proper installation of this gem is very important because without it rails simply won’t work.

Voilà, nokogiri gem was successfully installed and my rails console ran as nothing had happened.

Sources:

--

--