163

I've been trying to install Nokogiri on Mac OS 10.9.3 and whatever I try, the install fails in the end with the following error message:

$ sudo gem install nokogiri -- --with-xml2-include=/usr/local/Cellar/libxml2/2.9.1/include/libxml2 --with-xml2-lib=/usr/local/Cellar/libxml2/2.9.1/lib --with-xslt-dir=/usr/local/Cellar/libxslt/1.1.28 --with-iconv-include=/usr/local/Cellar/libiconv/1.14/include --with-iconv-lib=/usr/local/Cellar/libiconv/1.14/lib
Building native extensions with: '--with-xml2-include=/usr/local/Cellar/libxml2/2.9.1/include/libxml2 --with-xml2-lib=/usr/local/Cellar/libxml2/2.9.1/lib --with-xslt-dir=/usr/local/Cellar/libxslt/1.1.28 --with-iconv-include=/usr/local/Cellar/libiconv/1.14/include --with-iconv-lib=/usr/local/Cellar/libiconv/1.14/lib'
This could take a while...
Building nokogiri using packaged libraries.
ERROR:  Error installing nokogiri:
    ERROR: Failed to build gem native extension.

    /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby extconf.rb --with-xml2-include=/usr/local/Cellar/libxml2/2.9.1/include/libxml2 --with-xml2-lib=/usr/local/Cellar/libxml2/2.9.1/lib --with-xslt-dir=/usr/local/Cellar/libxslt/1.1.28 --with-iconv-include=/usr/local/Cellar/libiconv/1.14/include --with-iconv-lib=/usr/local/Cellar/libiconv/1.14/lib
Building nokogiri using packaged libraries.
checking for iconv.h... yes
checking for iconv_open() in iconv.h... no
checking for iconv_open() in -liconv... no
checking for libiconv_open() in iconv.h... no
checking for libiconv_open() in -liconv... no
-----
libiconv 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.

Provided configuration options:
    --with-opt-dir
    --without-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=/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/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


Gem files will remain installed in /Library/Ruby/Gems/2.0.0/gems/nokogiri-1.6.2.1 for inspection.
Results logged to /Library/Ruby/Gems/2.0.0/gems/nokogiri-1.6.2.1/ext/nokogiri/gem_make.out

After following a lot of help found around the web including building and installing libxml2 and libxslt using brew and building libiconv from the sources (as described in "Installing Nokogiri"), the error remains the same.

When trying to run the installation for Nokogiri, libxml2 and libxslt seem to be found fine, but not libiconv.

Anyone with a better knowledge of these things know how to get Nokogiri installed?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
polarblau
  • 17,649
  • 7
  • 63
  • 84
  • You don't show the exact commands you're trying to use, so we'll be shooting in the dark trying to answer. I always use `gem install nokogiri`, but then I use RVM to manage my Mac OS Ruby installs. libxml and libxslt are not related to iconv, so I'd recommend asking your question on [Nokogiri-talk](http://groups.google.com/group/nokogiri-talk) which is their support forum. I don't remember having any iconv issues when installing Nokogiri, and I have it on a bunch of machines. – the Tin Man Apr 03 '11 at 23:49
  • 1
    I've in the meantime opened a ticket at github: https://github.com/tenderlove/nokogiri/issues/442 which contains a little bit more information. – polarblau Apr 04 '11 at 20:57
  • 2
    Here's a link to solve the same problem on OSX El Capitan, http://estebantorr.es/blog/2015/10/02/Nokogiri-in-El-Capitan/ – sbs Oct 15 '15 at 18:10
  • "`sudo gem install nokogiri`". Don't use `sudo` to install gems or modify the default Ruby. Instead use rbenv or RVM to manage a separate Ruby. This is covered multiple times on Stack Overflow. – the Tin Man Dec 28 '15 at 17:34
  • Have you tried [uninstalling xz](https://github.com/sparklemotion/nokogiri/issues/1483) before nokogiri install? – lifeisfoo Jul 23 '16 at 08:14

33 Answers33

241

I had the same issue. Unfortunately the "Installing Nokogiri" doesn't cover Iconv issues. Here's how I resolved the issue.

First install homebrew, it'll make your life easier. If you already have it installed, be sure to grab the latest formulae by updating like so:

brew update

Note: In OSX 10.9+ you may need to install xCode command tools to allow you to install libiconv.

xcode-select --install

then install a newer version of libiconv

brew install libiconv

then install your gem

gem install nokogiri -- --with-iconv-dir=/usr/local/Cellar/libiconv/1.14
Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Cory
  • 5,645
  • 3
  • 28
  • 30
  • 5
    Once I did the "brew install libiconv" and "brew link libiconv" I was able to install nokogiri successfully. I'm hoping that means I don't have a hard dependency to the specific version of libiconv installed by brew. – Steven Chanin Jan 29 '12 at 23:54
  • I think you do. https://github.com/mxcl/homebrew/wiki/The-brew-command : brew link symlinks to that specific version – Cory Feb 02 '12 at 01:15
  • thanks alot, i have a feeling that you saved me a lot of research! – manmal Feb 14 '12 at 16:28
  • 88
    When I attempt `brew install libiconv` it spits out: **Error: No available formula for libiconv Apple distributes libiconv with OS X, you can find it in /usr/lib. Some build scripts fail to detect it correctly, please check existing formulae for solutions.** – Seth Bro Mar 21 '12 at 15:05
  • 1
    I should note that a system-wide gem install worked fine for me, but running bundler in the context of my project failed. I finally got it to work by following the advice here: https://gist.github.com/746966. I was unaware of `bundle config` commands that allow you to set switches on individual gems in your bundle. – Seth Bro Mar 21 '12 at 15:13
  • Thanks Seth! Just in case that Gist goes AWOL -> you can also configure bundler to use library specific build flags. In this case: bundle config build.nokogiri --with-xml2-include=/usr/local/Cellar/libxml2/2.7.7/include/libxml2 --with-xml2-lib=/usr/local/Cellar/libxml2/2.7.7/lib --with-xslt-dir=/usr/local/Cellar/libxslt/1.1.26 – Cory May 03 '12 at 17:01
  • Do i have to be somewhere specific in the file system before I do that stuff? Can't get this to work... – Will Jan 24 '13 at 04:40
  • 63
    If you're running Mavericks, you may need to install Xcode's command line tools: `xcode-select --install`. This fixed the problem for me. – Michael Stalker Dec 13 '13 at 18:08
  • I agree with Michael Stalker, installing Xcode's command line tools did the trick for me too. – jAmi Jul 06 '14 at 09:17
  • and if you're like @SethBro and have trouble with `brew install libiconv`, you may need to install libiconv manually: `curl -O 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` – Alien Life Form Jul 16 '14 at 00:55
  • 1
    As @MichaelStalker noted, running `xcode-select --install` fixed it for me. – DaniG2k Aug 06 '14 at 08:27
  • 1
    @Cory this is incorrect. You want to 'brew unlink libiconv' and then simply 'gem install nokogiri' in order to build against the system libiconv instead of the homebrew one. – lamont Sep 01 '14 at 21:27
  • @lamont: I'm not sure I'm following you. The old version of the system lib was the one giving trouble in the first place. – Cory Sep 06 '14 at 14:16
  • When I debugged this on my Mac (10.9 + Xcode 5.1.1) the problem was that /usr/local/include/iconv.h was being picked up and #define'ing iconv_open() to libiconv_open(). It then attempted to link against /usr/lib/libiconv.dylib which has iconv_open() and not libiconv_open() so the extconf.rb fails. This could be viewed as a problem with the system iconv, the homebrew iconv or the nokogiri extconf.rb. However, homebrew warns you twice that this is a bad idea, and requires you to use --force: https://gist.github.com/lamont-granquist/a6c231eb2e0b2d45641d -- so clearly brew link'ing it is wrong. – lamont Sep 07 '14 at 19:01
  • Also if you unlink homebrew and re-install xcode it works fine. I did initially see a problem where /usr/include/iconv.h was missing for me, but reinstalling xcode fixed that. There's no need to complicate things by trying to link nokogiri against homebrew's libiconv when you can fix the system libiconv and it works fine. – lamont Sep 07 '14 at 19:04
  • 34
    If you still can't get `brew install libiconv` to get the right formula, use this: `brew tap homebrew/dupes` – jamesdlivesinatree Sep 12 '14 at 16:18
  • 15
    OS X `Yosemite` (10.10) I didn't have to do the `brew` stuff; just the `xcode-select --install` fixed this problem for me. – bjfletcher Nov 03 '14 at 12:16
  • 1
    on OSX 10.8.5 `brew unlink libiconv` and then `gem install nokogiri` worked for me. It failed if I tried to `brew link libiconv` prior to `gem install nokogiri` – cman77 Mar 14 '15 at 14:49
  • 1
    this. is. it. `--with-iconv-dir=/usr/local/Cellar/libiconv/1.14` – Andre Zimpel Jun 11 '15 at 12:01
  • This solution worked for me, with the exception that I got 1.16 instead of 1.14. – Jack R-G Oct 13 '20 at 00:07
100

Try using the system libraries. OSX comes with libiconv in newer versions, but the defaults install script seems to have a problem

gem install nokogiri -- --use-system-libraries

Edit: If using bundler, as mentioned by Geoff you can do:

bundle config build.nokogiri --use-system-libraries
Community
  • 1
  • 1
Filip Kis
  • 1,650
  • 2
  • 14
  • 18
36

@Cory's solution contains the correct answer, but the solution on Mavericks is actually much simpler than the top solution so I'm reposting with only the necessary steps.

On Mavericks (OSX 10.9+):

Install Xcode Command Line Tools:

xcode-select --install

then install your gem:

gem install nokogiri
Micah Winkelspecht
  • 1,805
  • 1
  • 16
  • 7
  • 4
    Works on Yosemite. So glad I scrolled down and didn't use older answers. – yuяi Nov 17 '14 at 14:55
  • huge timesaver, thanks for this tip! worked a charm for me on mavericks – ice cream Dec 27 '14 at 20:11
  • Did you try using my solution? It does not require xcode intsalation. – Filip Kis Feb 14 '15 at 10:35
  • This is the official solution from the Nokogiri docs (http://www.nokogiri.org/tutorials/installing_nokogiri.html#mac_os_x), and the only one that worked on El Capitan. The accepted solution did not work there. – Johannes Oct 17 '15 at 20:11
  • Worked for me on El Capitan, so infuriating. Thanks for the link to Nokogiri docs @Johannes - really useful. Everyone should stay away from solutions that say to "use system libraries" - bad advice. – Polsonby Sep 24 '16 at 10:14
  • Anyone know why I might be getting this on Mojave after a recent update to 10.14.4? I've been building nokogiri via bundler without problems since at least Yosemite. Now this. Yes, XCode Command Line Tools are installed. Thx. – user24601 May 14 '19 at 00:36
20

I was finally able to solve this problem. None of the above solutions completely fixed it for me.

I was getting this error when trying to gem install nokogiri on OSX Lion 10.7.2. First of all, this error masks the real problem by saying libiconv is missing, because you will get the same error even if nokogiri can't find libxslt or libxml2, which in my case, it couldn't.

So I followed the instructions on http://nokogiri.org/tutorials/installing_nokogiri.html under the Homebrew section (slightly modified to account for a more current version of libxml2):

brew install libxml2
brew link libxml2

# install libxslt from source
wget ftp://xmlsoft.org/libxml2/libxslt-1.1.26.tar.gz
tar -zxvf libxslt-1.1.26.tar.gz
cd libxslt-1.1.26
./configure --prefix=/usr/local/Cellar/libxslt/1.1.26 --with-libxml-prefix=/usr/local/Cellar/libxml2/2.7.8
make
sudo make install

At this point I followed the directions on the nokogiri site and tried

gem install nokogiri -- --with-xslt-dir=/usr/local/Cellar/libxslt/1.1.26

However, this still failed because when building libxslt from source, it installs the /include folder in a funky place. So you need to specify the lib and include folders separately like so:

gem install nokogiri -- --with-xslt-lib=/usr/local/Cellar/libxslt/1.1.26/lib --with-xslt-include=/usr/local/Cellar/libxslt/1.1.26/include/libxslt

This still didn't work (same libiconv error), so I tried to specify all three required libraries (libxslt, libxml2 and libiconv):

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

Now I got a different error! It was still an error but at least it was different. The make process failed with:

in /opt/local/lib/libz.1.dylib, file was built for unsupported file format which is not the architecture being linked (x86_64) for architecture x86_64

Uhh, what? After a lot of googling, I came across this miracle post: http://www.refresherate.com/2010/01/08/fixing-ld-warning-in-usrlocalliblibz-dylib-file-is-not-of-required-architecture/

Apparently OSX Lion ships with some bad versions of the libz library (libz.dylib, libz.1.dylib, libz.1.2.4.dylib) and and they need to be replaced with the latest versions from the Xcode SDK. The article explains it better than I can so read the above link for specific instructions.

Once these were replaced, I ran

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

again and all was well. I hope this helps someone else.

Micah Winkelspecht
  • 1,805
  • 1
  • 16
  • 7
  • I had the same problem, but I found removing the following files from opt/local/lib: libz.dylib and libz.1.dylib and following the rest of these instructions did the trick. – Undistraction Dec 10 '11 at 11:51
  • 1
    I think /opt/local denotes a MacPorts installation. If you're running homebrew and Macports, you'll end up with some funkiness (as seen in your case). The best solution is to uninstall Macports, and use homebrew for your package manger. – Cory Feb 06 '12 at 18:35
  • I added a new simpler solution for Mavericks (OSX 10.9+) below. – Micah Winkelspecht Aug 29 '14 at 04:37
18

Just adding my voice to the crowd, but mkmf.log said something about not being able to find symbols for x86_64 architecture. I stumbled across this solution:

sudo env ARCHFLAGS="-arch x86_64" gem install nokogiri -- --use-system-libraries

May not solve anything for the original asker, but this may help someone.

Side Note: nokogiri has been my highest barrier to using ruby applications. Every time someone depends on a different version I have to figure out how to build it. And my problem is different every time.

nbering
  • 2,725
  • 1
  • 23
  • 31
  • 1
    Agreed with your nokogiri sentiment. Every. Single. Time. – jbnunn Dec 15 '15 at 13:32
  • Didn't work for me in this form, but `ARCHFLAGS="-arch x86_64" sudo gem install nokogiri -- --use-system-libraries` worked ... – bwoebi Feb 18 '16 at 18:15
  • This worked for me as well, much easier solution, I didnt use the ARCHFLAGS, just sudo gem install nokogiri -- --use-system-libraries – ryudice May 20 '16 at 03:28
15

gem install nokogiri -v '1.6.6.2' -- --use-system-libraries --with-xml2-include=$(brew --prefix libxml2)/include/libxml2

this worked for me on macos. It works also with version 1.6.6.4

Neodelf
  • 185
  • 1
  • 14
Prashant Kajale
  • 473
  • 4
  • 8
6

I'm using.. OS X 10.9.4 Homebrew 0.9.4

Here is my summery from this thread to successfully install nokogiri, fixing missing libiconv.

Install Homebrew http://brew.sh/ Or update to latest using command below

brew update

Install libxml2 libxslt

brew install libxml2 libxslt

Link both libxml2 libxslt

brew link libxml2 libxslt

If you get warning to use --force, just use command below

brew link --force libxml2 libxslt

Install xCode command tools to enable you to install libiconv

xcode-select --install

Install libiconv

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

Final step, install nokogiri!

gem install nokogiri -- --with-xml2-include=/usr/local/Cellar/libxml2/2.8.0/include/libxml2 --with-xml2-lib=/usr/local/Cellar/libxml2/2.8.0/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
Kevin Ng
  • 448
  • 5
  • 13
5

I solved this on Yosemite running Ruby 2.1.4 by

  1. ensure you have xCode 6.1 installed, then
  2. xcode-select --install, then
  3. In the AppStore click on updates and install the most recent version of the command-line tools (which apparently xcode-select --install does't do - sigh)
  4. then bundle install worked as normal.

I also did a brew install libiconv too but I am not convinced that step was needed.

Dave Sag
  • 13,266
  • 14
  • 86
  • 134
  • I don't believe `brew install libiconv` is necessary -- I never bothered finding the proper package and this worked fine for me. Granted, it may have been on my machine from when I installed nokogiri with ruby 2.1.1 before. – jmstone617 Oct 30 '14 at 23:18
  • "In the AppStore click on updates and install the most recent version of the command-line tools (which apparently xcode-select --install does't do - sigh)" – no updates are available. How do I update my command-line tools from Xcode? They don't show up in the "Downloads" section of the preferences pane. – chadoh Nov 05 '14 at 22:48
  • This worked for me without going to the app store. I updated command line tools and the OS (to Yosemite), then ran `xcode-select --install` and I was able to install Nokogiri. – steel Dec 06 '14 at 00:25
  • I had to do: xcode-select --install .. brew install libxml2 .. bundle config build.nokogiri --use-system-libraries .. bundle install – Zack Burt Dec 17 '14 at 18:47
4

I tried many things but nothing worked for me. Then I finally found the iconv documentation and it saved my day!

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Kamil Sarna
  • 5,993
  • 1
  • 32
  • 22
3

libiconv was removed from Homebrew 0.9 Now it's recommended to compile libiconv from source, then reference the install when you install the nokogiri gem. See the Nokogiri install instructions under the Homebrew 0.9 section on the Nokogiri install page

SupaIrish
  • 766
  • 8
  • 17
  • As of today I was able to run $ brew install libiconv and it downloaded and installed libiconv-1.14.tar.gz – Evolve Jul 25 '14 at 15:10
3

Looking in the mkmf file, it apear that nokogiri (or gem, I don't know) try to find dependencies in /op/local/. For me it's not the right path to search them for.

Forcing nokogiri to find the libs at the right place (I use homebrew) did the trick for me :

$ gem install nokogiri -- --with-xslt-dir=/usr/local/Cellar/libxslt/1.1.26 --with-xml2-dir=/usr/local/Cellar/libxml2/2.7.8/

Maybe there is something to fix in nokogiri...

HTH,

2

I had similar trouble on Mountain Lion. Turns out I had installed libiconv via rvm package previously and this is no longer necessary for ruby 1.9.3/Mountain Lion/nokogiri.

Following the advice of https://rvm.io/packages/, I deleted my $rvm_path/usr directory and rebuilt ruby 1.9.3. Thereafter installing nokogiri was a simple gem install. No messing with brew/macports/manual source installs!

Mike Park
  • 1,965
  • 1
  • 14
  • 10
2

You could also do this on Mavericks:

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

Just make sure you have xcode installed

Sam Rosen
  • 83
  • 8
  • same solution is also works for 10.7.5: gem install nokogiri -- --use-system-libraries=true --with-xml2-include=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.7.sdk/usr/include/libxml2 – tolginho Sep 30 '14 at 12:31
2

I was fighting with Nokogiri for a while today on OS X 10.10 Yosemite

My environment was messed up for some reason.

which bundle and which gem were giving me /usr/bin/bundle and /usr/bin/gem instead of ~/.rbenv/shims/gem

What helped correct it for me was sudo rm -i /usr/bin/gem /usr/bin/bundle

After that I: 1. switched back to my project directory 2. uninstalled the dependent 3. (re)-installed the dependent libs: did a brew install libxml2 libiconv libxslt 4. installed my ruby version fresh (with rbenv) 5. did gem install bundler 6. and bundle install ran without any problems.

Nokogiri was fine after that.

For reference:

╰─% cat .bundle/config           
---
BUNDLE_PATH: vendor/bundle
BUNDLE_DISABLE_SHARED_GEMS: '1'
BUNDLE_JOBS: 4

╰─% which ruby bundle gem
~/.rbenv/shims/ruby
~/.rbenv/shims/bundle
~/.rbenv/shims/gem
Sam Figueroa
  • 2,301
  • 22
  • 21
  • 1
    In hindsight(which is always 20/20) I could have probably just adjusted my PATH env-variable to prepend rbenv first. – Sam Figueroa Nov 04 '14 at 10:15
2

This one worked for me

sudo env ARCHFLAGS="-arch x86_64" gem install nokogiri:1.6.6.2 -- --use-system-libraries --with-xml=/usr/local/Cellar/libxml2/2.9.3/ --with-iconv-dir=/usr/local/Cellar/libiconv/1.14

Chen Kinnrot
  • 20,609
  • 17
  • 79
  • 141
2

Look in the mkmf.log file in the gem's build directory (e.g. /Library/Ruby/Gems/1.8/gems/nokogiri-1.4.4/ext/nokogiri/mkmf.log). That has a lot more info. In my case when I hit this it was that Nokogiri specifically adds /opt/local/lib to the library search path and GNU Backgammon had installed an incompatible libiconv there.

scotchi
  • 2,359
  • 2
  • 19
  • 21
2

Assuming you installed libxml2 and libxslt with MacPorts, then you might still be getting this error due to a mismatch of the order of include paths and link paths used by the call to have_func('iconv_open', 'iconv.h')

Simple (patch) solution: delete libiconv.* in /usr/local/lib

Mick West
  • 21
  • 2
2
$ gem install nokogiri -- --with-xslt-dir=/usr/local/Cellar/libxslt/1.1.26 --with-xml2-dir=/usr/local/Cellar/libxml2/2.7.8/

same as above, this is the solution for homebrew and using gem install (apply changes depends on your version)

however, if you use Gemfile and bundle install, you should apply the bundle config before you install, here the code

$ bundle config build.nokogiri --with-xslt-dir=/usr/local/Cellar/libxslt/1.1.26 --with-xml2-dir=/usr/local/Cellar/libxml2/2.7.8/ --with-iconv-dir=/usr/local/Cellar/libiconv/1.14

again, apply changes depends on your version

hope this will help you.

credit: https://gist.github.com/1344331

RacsO
  • 61
  • 2
1

You need to upgrade your homebrew to 0.9

then follow these steps

brew install libxml2 libxslt
brew link libxml2 libxslt
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

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

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 --with-iconv-include=/usr/local/Cellar/libiconv/1.13.1/include --with-iconv-lib=/usr/local/Cellar/libiconv/1.13.1/lib

You should double check the folder library version.

Ilake Chang
  • 1,542
  • 1
  • 14
  • 19
  • Link and sudo will break things. They're keg-only for several reasons. Use brew formulas because there's way easier to maintain. `brew create http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.13.1.tar.gz && brew install libiconv` –  Feb 13 '14 at 04:01
1

HOMEBREW USERS

Correct solution to fix this problem if you are using homebrew:

xcode-select --install

Choose "Get Xcode" from the dialog box.

brew unlink libiconv
gem install nokogiri

The xcode-select step fixes your Xcode installation and Xcode Command Line Utils installation. I found that I didn't have /usr/include/iconv.h due to some kind of problem with O/S upgrades or restores from backups. If you don't have that header file and /usr/lib/libconv.dylib after running xcode-select then you most likely need to drag Xcode from the Application folder to the trash and reinstall and then manually download the Command Line Tools for Xcode from https://developer.apple.com/downloads/index.action and install that.

Then you need to unlink the libiconv from homebrew. You don't need this. And clang will actually pick up /usr/local/include/iconv.h over /usr/include/iconv.h and will #define iconv_open to libiconv_open but then link against /usr/lib/libiconv.dylib which does not have libiconv_open which is what causes the mkmf failures to find libiconv. What you need to do is remove the linking in of iconv.h so that nokogiri will not find it.

Then just build nokogiri normally.

For new users, all you should need to do is install xcode with xcode-select and install nokogiri, but if you've found this question then presumably you've got a botched installation and not a fresh install of Mavericks.

Some of the other answers here are definitely incorrect. Most of them attempt to use libiconv out of homebrew which is entirely unnecessary. The answers that brew link libiconv are actually causing the problem where clang becomes confused and tries to read a homebrew header file and link against the system libraries. The answers which suggest --use-system-libraries are poor because nokogiri needs to be linked against its bundled libxml2 and libxslt libraries because other versions of those libraries are incompatible with it. The answers which want you to compile from sources are just hopelessly overly complicated.

TL;DR:

  • Upgrade and/or repair your Xcode installation
  • Unlink your homebrew libiconv since trying to use that only causes problems
  • Build nokogiri normally

RVM USERS

Old RVM installations may have a libiconv hiding in your rvm directories somewhere which are conflicting with your system libraries. See https://stackoverflow.com/a/11809261/506908 for more information.

MacPorts USERS

If you have /opt/local/lib/libiconv.dylib then nokogiri adds that path to the list of directories that it searches for and it will find the MacPorts installation similar to how it picks up the homebrew libiconv and conflicts with the system libraries. You can try:

sudo port uninstall libiconv

If that fails due to dependencies then you can try linking against the MacPorts version directly (untested):

gem install nokogiri -- --with-iconv-dir=/opt/local
Community
  • 1
  • 1
lamont
  • 3,854
  • 1
  • 20
  • 26
  • Confirming `gem install nokogiri -- --with-iconv-dir=/opt/local` works just fine with MacPorts `libiconv` installed – PartialOrder Jul 01 '17 at 16:30
0

I ran into this, this morning... after an upgrade to Mavericks. We did many things. However if anyone is having this issue here are some things to try.

I ran 'xcode-select --install' then got the command line tools installed (surprised this didn't update on the OS ugrade). I ran 'rvm implode' (I will build it back up later).

We pieced together two error output files:

~/.vagrant.d/gems/gems/nokogiri-1.6.3.1/ext/nokogiri/gem_make.out

~/.vagrant.d/gems/gems/nokogiri-1.6.3.1/ext/nokogiri/tmp/x86_64-apple-darwin12.5.0/ports/libxml2/2.8.0/configure.log

Which seemed to indicate that the c compiler was using a surprising setting.

I ran 'env'

Output contained:

...
CXX=/usr/local/opt/apple-gcc42/bin/g++-4.2
...
CC=/usr/local/opt/apple-gcc42/bin/gcc-4.2
...

These files didn't exist on the file system...

After all of these changes moving to a new terminal windows (so everything was fresh). Installation of vagrant-berkshelf (which in turn installs nokogiri) worked fine.

NOTE: when running 'env' in the fresh window there was no longer a setting for CC or CXX...

Unsure on the key part of this, or if order matters, but tried to recreate the parts that seemed to play a role in getting this to work.

aaron blythe
  • 324
  • 1
  • 7
  • Are you sure that the command `gem install nokogiri` succeeds? Wither with "already installed" message or a successful installation? – Spundun Aug 04 '14 at 16:39
  • Yes. I have reinstalled rvm and oh-my-zsh as I build things back up on my Mac. What I have not been able to figure out is how to get nokogiri installed on an rvm gemset. When I switch back to 'rvm use system' I can install nokogiri 1.6.3.1 over top of the 1.5.6 that comes on the OS X ruby 2.0.0p451. – aaron blythe Aug 06 '14 at 05:18
  • In the end for me it was http://stackoverflow.com/questions/19643153/error-to-install-nokogiri-on-osx-10-9-maverick brew uninstall apple-gcc42 brew install apple-gcc42 – aaron blythe Aug 06 '14 at 06:14
0

According to the documentation, as of OSX 10.9 and Homebrew 9.5+, you are probably missing the development tools.

Nokogiri installation

Troubleshooting

If you have problems mentioning libiconv missing that looks something like this:

Installing nokogiri (1.6.2.1) Building nokogiri using packaged libraries.

Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.

    /usr/local/rvm/rubies/ruby-2.0.0-p0/bin/ruby extconf.rb
Building nokogiri using packaged libraries.
checking for iconv.h... yes
checking for iconv_open() in iconv.h... no
checking for iconv_open() in -liconv... no
checking for libiconv_open() in iconv.h... no
checking for libiconv_open() in -liconv... no
-----
libiconv is missing.  please visit http://nokogiri.org/tutorials/installing_nokogiri.html for          help with installing dependencies.
-----
*** extconf.rb failed ***

Then you are probably missing the right developer tools. This is a really easy fix:

brew unlink gcc-4.2      # you might not need this step
gem uninstall nokogiri
xcode-select --install
gem install nokogiri

This is verified working on OSX 10.9 w/ xcode’s clang compiler.

rexmadden
  • 266
  • 4
  • 7
0

I got this issue when I upgraded my Mac OS to Yosemite. I was able to solve this issue by doing:

xcode-select --install
brew uninstall libiconv
brew install libiconv
gem install nokogiri
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Lalu
  • 732
  • 3
  • 8
  • 20
0

I had a similar issue and the accepted answer used to work for me. Now, however I have been seeing a new error message where a gmkdir command is missing as in this question:

gem install nokogiri -v '1.5.11' failed due to make: /usr/local/bin/gmkdir: No such file or directory

What worked for me was first a little cleanup:

brew uninstall libiconv
brew uninstall libxml2
brew uninstall libxslt

And then brew unlink libiconv and whatever else you need to unlink (check with brew doctor). Then, the magic two lines (copied from the linked answer):

brew install coreutils
gem install nokogiri
Community
  • 1
  • 1
skensell
  • 1,421
  • 12
  • 21
0

Simple steps to follow before you start uninstalling and installing anything. Check if you have CLT (command line tools installed):

brew config

see the CLT version here if its installed no need for reinstallation.

This means the issue is with permission you have reinstall only bundler gem make its not sudo. Uninstall bundler with gem uninstall bundler reinstall gem bundler gem install bundler

Most important point here is the permission under which bundler gem is installed it should never be sudoed.

Pikachu-go
  • 3,058
  • 4
  • 20
  • 27
-1

In my case, (as with scotchi) the build failed because of an incompatible iconv library in /opt/local/lib. By default, theNokogiri build process looks at /opt/local first. To force it to use a different install directory, /usr/local for example, do:

gem install nokogiri -- --with-xml2-lib=/usr/local/lib --with-xml2-include=/usr/local/include/libxml2 --with-xslt-lib=/usr/local/lib --with-xslt-include=/usr/local/include
TsenYing
  • 280
  • 4
  • 10
-1

This blog post proposes to install libiconv manually.

Subsequently, nokogiri can be installed with a number of switches telling it where to find libiconv (see the blog post).

As a side note: After installing nokogiri, I managed to install gollum (installation of which also failed because it could not find iconv). Now I am still facing problems though, because when I start up gollum, Python crashes.

gaston
  • 507
  • 4
  • 5
-1
$ gem install iconv # works but it is missing an iconv.so file in ruby 2.0.0-p247

$ ls -1 2.0.0-p0/lib/ruby/gems/2.0.0/gems/iconv-1.0.3/*/*/*.so
2.0.0-p0/lib/ruby/gems/2.0.0/gems/iconv-1.0.3/ext/iconv/iconv.so*
2.0.0-p0/lib/ruby/gems/2.0.0/gems/iconv-1.0.3/lib/iconv/iconv.so*

$ ls -1 2.0.0-p247/lib/ruby/gems/2.0.0/gems/iconv-1.0.3/*/*/*.so
2.0.0-p247/lib/ruby/gems/2.0.0/gems/iconv-1.0.3/ext/iconv/iconv.so*

NOTE THE MISSING /lib/iconv/iconv.so file in 2.0.0-p247 that is present in 2.0.0-p0 gems installation.

$ rbenv version
2.0.0-p247 (set by /home/XXX/tmp/.ruby-version)
$ rbenv which gem
/home/XXX/.rbenv/versions/2.0.0-p247/bin/gem
$ gem --version
1.8.25

$ rbenv which pry
/home/XXX/.rbenv/versions/2.0.0-p247/bin/pry
$ pry
[1] pry(main)> require 'iconv'
LoadError: cannot load such file -- iconv/iconv.so
from /home/XXX/.rbenv/versions/2.0.0-p247/lib/ruby/site_ruby/2.0.0/rubygems/custom_require.rb:36:in `require'

Copy the file over

$ pushd ~/.rbenv/versions/
$ cp 2.0.0-p247/lib/ruby/gems/2.0.0/gems/iconv-1.0.3/ext/iconv/iconv.so 2.0.0-p247/lib/ruby/gems/2.0.0/gems/iconv-1.0.3/lib/iconv/

NOW IT WORKS!

$ pry
[1] pry(main)> require 'iconv'
=> true
Darren Weber
  • 1,537
  • 19
  • 20
  • Note: I'm working with rbenv, but this problem is specific to the gem installation and it should be fixed up stream! – Darren Weber Sep 19 '13 at 03:48
  • this answer has nothing to do with the C libiconv library that nokogiri's build depends upon. answer should really be removed. – lamont Sep 01 '14 at 21:44
-1

I had to install gcc first before running ./configure --prefix=/usr/local/Cellar/libiconv/1.13.1

Cody Barr
  • 621
  • 1
  • 5
  • 9
-1

I followed instructions from nokogiri installation page and also installed xcode tools. But it still did not work for me.

Then I saw that brew install libxml2 libxslt command outputs the following (among other different stuff):

To allow the nokogiri gem to link against this libxslt run:
  gem install nokogiri -- --with-xslt-dir=/usr/local/opt/libxslt

So, I run this and nokogiri was installed succesfully.

m8labs
  • 3,671
  • 2
  • 30
  • 32
-1

This works for me:

gem install nokogiri -- --use-system-libraries

If you are using Bundler, tell it to use the option:

bundle config build.nokogiri --use-system-libraries
bundle install

ref

Community
  • 1
  • 1
Muntasim
  • 6,689
  • 3
  • 46
  • 69
-1
  • As a self-contained solution, I've put together this ruby script as a gist that should work with any recent Mac OS X / Homebrew installation.
  • You're basically just on on relying Homebrew's libraries instead of the outdated Apple ones. Simple.
  • You can configure and install with Bundler (instead of gem) by passing bundle as an argument.

Check it out, and please feel free to contribute--maybe we can boil this down to one script to fix them all!

Steve Benner
  • 1,679
  • 22
  • 26
  • If this was downvoted becuase of an error in the code, the fix was just one line and I retested the script; it is working on my system. – Steve Benner Sep 06 '14 at 20:31
-1

First of all make sure you follow the installation guide on nokogiri: http://nokogiri.org/tutorials/installing_nokogiri.html

After having followed the guide I still had this issue. This is how I solved it:

First of all I installed iconv using homebrew: brew install iconv

Then I uninstalled ruby, luckily this is very easy with rvm:

rvm uninstall 1.9.2        

Then I had to reinstall ruby with the following options:

CC=gcc-4.2 rvm install 1.9.2-p290 --with-iconv-dir=/usr/local/Cellar/libiconv/1.13.1

Then I create a gemset with the new ruby version:

rvm use 1.9.2@coolproject

Then I can finally install nokogiri like this:

gem install nokogiri -v=1.4.4 -- --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