33
sudo gem install nokogiri
Building native extensions.  This could take a while...
ERROR:  Error installing nokogiri:
    ERROR: Failed to build gem native extension.

        /Users/sajeev86/.rvm/rubies/ruby-1.8.7-p352/bin/ruby extconf.rb
checking for libxml/parser.h... no
-----
libxml2 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.

I can't get Nokogiri to install. This has been taxing me for a while now. I've installed the dependencies via MacPorts and Homebrew as well.

I've got a feeling the right directories are not pointed to each other? But haven't a clue how to fix.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303

17 Answers17

43

In Mavericks, installing the libraries with Homebrew and setting NOKOGIRI_USE_SYSTEM_LIBRARIES=1 before installing the gem did the trick for me.

Summarising:

  • If previously installed, uninstall the gem:
    $ gem uninstall nokogiri

  • Use Homebrew to install libxml2, libxslt and libiconv:
    $ brew install libxml2 libxslt libiconv

  • Install the gem specifying the paths to the libraries to be linked against:
    $ NOKOGIRI_USE_SYSTEM_LIBRARIES=1 gem install nokogiri -- --use-system-libraries --with-iconv-dir="$(brew --prefix libiconv)" --with-xml2-config="$(brew --prefix libxml2)/bin/xml2-config" --with-xslt-config="$(brew --prefix libxslt)/bin/xslt-config"

Eduardo
  • 2,704
  • 18
  • 17
32

Absolutely none of the other answers here worked for me.

On the latest version of Mavericks, the only method that succeeded for me was the following:

$ brew install libxml2 libxslt libiconv
$ sudo gem install nokogiri -v '1.6.5' -- --use-system-libraries --with-xml2-include=/usr/include/libxml2 --with-xml2-lib=/usr/lib
Crates
  • 2,362
  • 2
  • 19
  • 15
  • At least as of today, that brew command fails as there is "No available formula for libiconv" (plus a recommendation just to use the copy Apple supplies). The suggested gem line then fails with the same "failed to build gem native extension". – Tommy Oct 13 '15 at 19:06
  • OK, Tommy... no problem. Try installing libiconv manually, and then remove that one word from the brew install line. `wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.13.1.tar.gz; tar xvfz libiconv-1.13.1.tar.gz; cd libiconv-1.13.1; ./configure --prefix=/usr/local/Cellar/libiconv/1.13.1; make; sudo make install; ` – Crates Oct 14 '15 at 13:26
  • This worked on High Sierra /w noko version 1.8.2 as well. – Brian May 04 '18 at 02:32
16

Same issue happened to me on Lion. Took hours of reading posts, trying homebrew, macports, etc. When I found this post and read the contents of .rvm/gems/ruby-1.8.7-p358/gems/nokogiri-1.4.4/ext/nokogiri/mkmf.log, I saw:

find_header: checking for libxml/parser.h... -------------------- no

"/usr/bin/gcc-4.2 -E -I. -I/Users/me/.rvm/rubies/ruby-1.8.7-p358/lib/ruby/1.8/i686-darwin10.8.0 -I. -I-I-I/opt/local/include -I-I-I/usr/local/include -I-I-I/Users/me/.rvm/rubies/ruby-1.8.7-p358/include -I-I-I/usr/include -I-I-I/usr/include/libxml2 -I/opt/local/include/libxml2 -I/usr/local/include/libxml2 -I/Users/me/.rvm/rubies/ruby-1.8.7-p358/include/libxml2 -I-I/opt/local/include -I-I/usr/local/include -I-I/Users/me/.rvm/rubies/ruby-1.8.7-p358/include -I-I/usr/include -I-I/usr/include/libxml2 -I/opt/local/include -I/usr/local/include -I/Users/me/.rvm/rubies/ruby-1.8.7-p358/include -I/usr/include -I/usr/include/libxml2  -D_XOPEN_SOURCE -D_DARWIN_C_SOURCE   -g -O2  -fno-common -pipe -fno-common    -g -DXP_UNIX -O3 -Wall -Wcast-qual -Wwrite-strings -Wconversion -Wmissing-noreturn -Winline  conftest.c -o conftest.i"
checked program was:
/* begin */
1: #include <libxml/parser.h>
/* end */

However, gcc-4.2 didn't exist:

$ ls /usr/bin/gcc*
/usr/bin/gcc

Hence, I was able to solve the issue and get nokogiri to install by making a gcc-4.2 a symlink to gcc.

sudo ln -s gcc gcc-4.2
Community
  • 1
  • 1
Rememberer
  • 185
  • 1
  • 4
10

I ran into same issue for Mavrick and solution was the

xcode-select --install

However that was not working as apple download page does not have that installer available publically. So if you want to have the command-line tools for Mavricks you need to have a paid account then only you will be able to install it. Once you install it you will not face this issue of Nokogiri.

shinesecret
  • 1,402
  • 12
  • 16
9

Checking the url given in the error message (http://nokogiri.org/tutorials/installing_nokogiri.html) I think the question is what version of Homebrew are you using?

brew -v

I'd update to 0.8+

brew update
brew upgrade

and then try the installation again:

brew install libxml2 libxslt
brew link libxml2 libxslt
gem install nokogiri
knutsel
  • 1,281
  • 9
  • 22
  • 1
    I have not encountered that problem before, can you try to first "brew unlink libxml2" and then "brew link libxml2" – knutsel Feb 03 '12 at 20:58
  • `Linking /usr/local/Cellar/libxml2/2.7.8... 21 symlinks created Linking /usr/local/Cellar/libxslt/1.1.26... 22 symlinks created` But it still returns the libxml2 is missing message. – Sajeev Shanmuganandarajah Feb 03 '12 at 21:04
  • When I try `brew link libxml2 libxslt`, I get an error saying it "Refused to link macOS provided/shadowed sooftware" – Qasim Feb 10 '22 at 08:09
  • Does the install succeed? no weird error messages? (`brew install libxml2 libxslt`) – knutsel Feb 10 '22 at 11:43
7

As for me, it helps:

brew install libxml2 libxslt

if permission denied sudo chown -R $USER /Library/Caches/Homebrew

sudo env ARCHFLAGS="-arch x86_64"

export NOKOGIRI_USE_SYSTEM_LIBRARIES=1

NOKOGIRI_USE_SYSTEM_LIBRARIES=1 gem install nokogiri
shilovk
  • 11,718
  • 17
  • 75
  • 74
6
brew install libxml2 libxslt 
gem install nokogiri -v '1.6.5' -- --use-system-libraries
zires
  • 554
  • 6
  • 16
  • 1
    It would be nice if you explained to the OP what is going on here, and why it works. You know, "teach a man to fish" and all that. – zxq9 Dec 03 '14 at 04:40
3

Late to the party, but in case anyone is still having this issue while using MacPorts:

Add the /include dir to your PATH in your .profile file so nokogiri knows where to find it.

e.g.

MacPorts writes to your .profile file and adds the following line by default:

export PATH=/opt/local/bin:/opt/local/sbin:$PATH

First, add the /include path like so:

export PATH=/opt/local/bin:/opt/local/sbin:/opt/local/include/:$PATH

Then, run this from the same terminal window:

source ~/.profile

(this tells your terminal to recheck the .profile file for any changes)

Finally, try installing the gem from the same terminal window.

sudo gem install nokogiri
user1136698
  • 123
  • 1
  • 5
3

On Yosemite I used:

brew install libxml2 libxslt
sudo gem install nokogiri -v '1.6.5' -- --use-system-libraries --with-xml2-include=/usr/include/libxml2 --with-xml2-lib=/usr/lib

Very close to some of the above commands but without the libconv

Union find
  • 7,759
  • 13
  • 60
  • 111
2

For my somewhat outdated system encountering the issues above, specifically:

FENG-SHUI:demo Eric$ brew install libxml2
==> Downloading ftp://xmlsoft.org/libxml2/libxml2-2.9.0.tar.gz
######################################################################## 100.0%
==> Downloading patches
######################################################################## 100.0%
######################################################################## 100.0%
==> Patching patching file threads.c patching file xpath.c
==> ./configure --prefix=/usr/local/Cellar/libxml2/2.9.0 --without-python
==> make
==> make install
==> Caveats This formula is keg-only: so it was not symlinked into /usr/local.

Mac OS X already provides this software and installing another version in parallel can cause all kinds of trouble.

Generally there are no consequences of this for you. If you build your own software and it requires this formula, you'll need to add to your build variables:

    LDFLAGS:  -L/usr/local/opt/libxml2/lib
    CPPFLAGS: -I/usr/local/opt/libxml2/include

Error: Failed to create: /usr/local/opt/libxml2 
Things that depend on libxml2 will probably not build.
    ==> Summary /usr/local/Cellar/libxml2/2.9.0: 273 files, 11M, built in 2.0 minutes 

FENG-SHUI:demo Eric$ brew install libxml2 libxslt 
Error: libxml2-2.9.0 already installed 
Error: /usr/local/opt/libxml2 not present or broken Please reinstall libxml2. Sorry :(

This was the magic formula for getting libxml2 and libxslt happily running:

# latest version 2.9.0
brew install libxml2

# installing libxslt from source code
# get latest libxslt from ftp://xmlsoft.org/libxml2/libxslt-git-snapshot.tar.gz..
# then
./configure --prefix=/usr/local/Cellar/libxslt/1.1.28 --with-libxml-prefix=/usr/local/Cellar/libxml2/2.9.0
make
sudo make install

# installing nokogiri with this new compiled libs
gem install nokogiri -- --with-xml2-include=/usr/local/Cellar/libxml2/2.9.0/include/libxml2 --with-xml2-lib=/usr/local/Cellar/libxml2/2.9.0/lib --with-xslt-dir=/usr/local/Cellar/libxslt/1.1.28

Thanks to this guy: https://gist.github.com/fabioyamate/443160

Hope this helps someone!

ericpeters0n
  • 668
  • 7
  • 12
  • This worked for installing `nokogiri 1.6.0` on `OSX 10.8.4` with `libxslt 1.1.28` and `libxml2 2.9.1` – thanks! – Gon Zifroni Aug 13 '13 at 21:58
  • `threads.c:918:20: error: expected expression once_control = PTHREAD_ONCE_INIT; ^ /usr/include/pthread.h:203:27: note: expanded from macro 'PTHREAD_ONCE_INIT' #define PTHREAD_ONCE_INIT {_PTHREAD_ONCE_SIG_init, {0}}` – A H K Sep 28 '16 at 17:40
1

I struggled with this for hours until I found this blogpost: http://www.markhneedham.com/blog/2010/07/08/installing-ruby-1-9-2-with-rvm-on-snow-leopard

Running the command

file /usr/local/lib/libxml2.2.dylib

in the terminal returned

/usr/local/lib/libxml2.2.dylib: Mach-O dynamically linked shared library i386

which means my libxml2.2 was compiled for a 32 bit system, so I had to recompile libxml2 for 64 bit OS (Lion)

First I downloaded LATEST_LIBXML2 from ftp://xmlsoft.org/libxml2 and ran the following commands:

tar xzvf LATEST_LIBXML2

cd libxml2-2.8.0

./configure --with-python=/System/Library/Frameworks/Python.framework/Versions/2.3/

make

Then I downloaded LATEST_LIBXSLT from the same site and ran these commands:

tar xzvf LATEST_LIBXSLT

cd libxslt-1.1.26

./configure

make

sudo make install

Running

file /usr/local/lib/libxml2.2.dylib

again returned

/usr/local/lib/libxml2.2.dylib: Mach-O 64-bit dynamically linked shared library x86_64

And I could finally install nokogiri!

Community
  • 1
  • 1
Ste
  • 11
  • 1
1

This worked for me on OSX - the same as @remeberer's answer, but with a full path.

sudo ln -s /usr/bin/gcc /usr/bin/gcc-4.2

Ziad Sawalha
  • 181
  • 1
  • 9
1

I had to use this command for it to work :

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

(source : http://www.virtlab.cx/ruby-fix-error-install-nokogiri-yosemite/ , many thanks to the author)

Dulgan
  • 6,674
  • 3
  • 41
  • 46
1

Try adding --with-xml2-include=/opt/local/include/libxml2 --with-xml2-lib=/opt/local/lib to your command, assuming that's where MacPorts libxml2.

noodl
  • 17,143
  • 3
  • 57
  • 55
0

ran into a similar install issue on mountain lion except the make command was looking for gcc-4.2 in /usr/local/opt/apple-gcc42/bin

this resolved the issue for me; no need to brew install libxml2 or libxslt or add a --with option:

mkdir -p /usr/local/opt/apple-gcc42/bin
ln -s /usr/bin/gcc /usr/local/opt/apple-gcc42/bin/gcc-4.2
dan_nl
  • 4,456
  • 1
  • 15
  • 7
0

If you're installing an older version of Nokogiri, i.e. pre 1.6, this may help:

In the Nokogiri installation guide, they mention:

Because Nokogiri needs to be compiled and dynamically linked against both libxml2 and libxslt, it has gained a reputation for being complicated to install.

As of Nokogiri 1.6, libxml2 and libxslt source code is bundled with Nokogiri, and compiled at gem-install-time. This document should work for all versions 1.6.4 and later.

(If you need support for installing earlier versions of Nokogiri, you may want to take a look at the git history for this document.)

Indeed, it is difficult to install, but there is a way. I went digging through the history of the readme, and decided on this one, with the well-written commit message:

"Homebrew, I straight hate you."

From that message and from the other answers here, I gleaned that I needed to be very specific about libxml2's lib, dir and include directories when installing Nokogiri. The following worked for me, but please update to match your version of the libxml2 software (mine was 2.9.2 and I was installing Nokogiri 1.5.0):

NOKOGIRI_USE_SYSTEM_LIBRARIES=1 gem install nokogiri -v '1.5.0' -- 
    --use-system-libraries 
    --with-xml2-lib=/usr/local/Cellar/libxml2/2.9.2/lib 
    --with-xml2-config="$(brew --prefix libxml2)/bin/xml2-config" 
    --with-xslt-config="$(brew --prefix libxslt)/bin/xslt-config" 
    --with-xml2-dir=/usr/local/Cellar/libxml2/2.9.2/ 
    --with-xml2-include=/usr/local/Cellar/libxml2/2.9.2/include/libxml2
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Danny
  • 3,982
  • 1
  • 34
  • 42
0

For me, I simply did the following

xcode-select --install
gem install nokogiri

For more in-depth troubleshooting, checkout Installing Nokogiri

Rob
  • 26,989
  • 16
  • 82
  • 98
Igbanam
  • 5,904
  • 5
  • 44
  • 68