OS X Lion, Attempting Nokogiri install - libxml2 is missing

霸气de小男生 提交于 2019-11-27 18:08:57

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"

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
Rememberer

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

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.

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
brew install libxml2 libxslt 
gem install nokogiri -v '1.6.5' -- --use-system-libraries

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

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

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

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!

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

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!

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

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)

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
Danny

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
iGbanam

For me, I simply did the following

xcode-select --install
gem install nokogiri

For more in-depth troubleshooting, checkout Installing Nokogiri

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!