Why does installing Nokogiri on Mac OS fail with libiconv is missing?

77,007

Solution 1

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

Solution 2

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

Solution 3

@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

Solution 4

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.

Solution 5

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.

Share:
77,007

Related videos on Youtube

polarblau
Author by

polarblau

Updated on September 19, 2021

Comments

  • polarblau
    polarblau over 2 years

    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?

    • PJP
      PJP about 13 years
      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 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.
    • polarblau
      polarblau about 13 years
      I've in the meantime opened a ticket at github: github.com/tenderlove/nokogiri/issues/442 which contains a little bit more information.
    • sbs
      sbs over 8 years
      Here's a link to solve the same problem on OSX El Capitan, estebantorr.es/blog/2015/10/02/Nokogiri-in-El-Capitan
    • PJP
      PJP over 8 years
      "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.
    • lifeisfoo
      lifeisfoo almost 8 years
      Have you tried uninstalling xz before nokogiri install?
  • Undistraction
    Undistraction over 12 years
    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.
  • Steven Chanin
    Steven Chanin over 12 years
    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.
  • Cory
    Cory over 12 years
    I think you do. github.com/mxcl/homebrew/wiki/The-brew-command : brew link symlinks to that specific version
  • Cory
    Cory about 12 years
    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.
  • manmal
    manmal about 12 years
    thanks alot, i have a feeling that you saved me a lot of research!
  • Seth Bro
    Seth Bro about 12 years
    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
    Seth Bro about 12 years
    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: gist.github.com/746966. I was unaware of bundle config commands that allow you to set switches on individual gems in your bundle.
  • Cory
    Cory about 12 years
    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
  • Pikachu
    Pikachu almost 12 years
    Thanks very much. It saved my day too.
  • Will
    Will over 11 years
    Do i have to be somewhere specific in the file system before I do that stuff? Can't get this to work...
  • Darren Weber
    Darren Weber over 10 years
    Note: I'm working with rbenv, but this problem is specific to the gem installation and it should be fixed up stream!
  • Michael Stalker
    Michael Stalker over 10 years
    If you're running Mavericks, you may need to install Xcode's command line tools: xcode-select --install. This fixed the problem for me.
  • Admin
    Admin about 10 years
    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
  • jAmi
    jAmi almost 10 years
    I agree with Michael Stalker, installing Xcode's command line tools did the trick for me too.
  • Alien Life Form
    Alien Life Form almost 10 years
    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
  • Geoff Evason
    Geoff Evason almost 10 years
    If using bundler - you can run "bundle config build.nokogiri --use-system-libraries"
  • Evolve
    Evolve almost 10 years
    As of today I was able to run $ brew install libiconv and it downloaded and installed libiconv-1.14.tar.gz
  • Spundun
    Spundun almost 10 years
    Are you sure that the command gem install nokogiri succeeds? Wither with "already installed" message or a successful installation?
  • aaron blythe
    aaron blythe almost 10 years
    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
    aaron blythe almost 10 years
    In the end for me it was stackoverflow.com/questions/19643153/… brew uninstall apple-gcc42 brew install apple-gcc42
  • DaniG2k
    DaniG2k almost 10 years
    As @MichaelStalker noted, running xcode-select --install fixed it for me.
  • Micah Winkelspecht
    Micah Winkelspecht over 9 years
    I added a new simpler solution for Mavericks (OSX 10.9+) below.
  • lamont
    lamont over 9 years
    @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
    lamont over 9 years
    this answer has nothing to do with the C libiconv library that nokogiri's build depends upon. answer should really be removed.
  • Cory
    Cory over 9 years
    @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.
  • Steve Benner
    Steve Benner over 9 years
    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.
  • lamont
    lamont over 9 years
    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: gist.github.com/lamont-granquist/a6c231eb2e0b2d45641d -- so clearly brew link'ing it is wrong.
  • lamont
    lamont over 9 years
    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.
  • jamesdlivesinatree
    jamesdlivesinatree over 9 years
    If you still can't get brew install libiconv to get the right formula, use this: brew tap homebrew/dupes
  • tolginho
    tolginho over 9 years
    same solution is also works for 10.7.5: gem install nokogiri -- --use-system-libraries=true --with-xml2-include=/Applications/Xcode.app/Contents/Develop‌​er/Platforms/MacOSX.‌​platform/Developer/S‌​DKs/MacOSX10.7.sdk/u‌​sr/include/libxml2
  • nils petersohn
    nils petersohn over 9 years
    finally the "bundle config build.nokogiri --use-system-libraries" worked. I hate when that happens
  • jmstone617
    jmstone617 over 9 years
    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.
  • bjfletcher
    bjfletcher over 9 years
    OS X Yosemite (10.10) I didn't have to do the brew stuff; just the xcode-select --install fixed this problem for me.
  • Sam Figueroa
    Sam Figueroa over 9 years
    In hindsight(which is always 20/20) I could have probably just adjusted my PATH env-variable to prepend rbenv first.
  • Valerio Schiavoni
    Valerio Schiavoni over 9 years
    I've libxml2 and libxslt installed with MacPorts, but there are no libiconv.* files in /usr/local/lib
  • chadoh
    chadoh over 9 years
    "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.
  • Lloyd Dewolf
    Lloyd Dewolf over 9 years
    Worked for me on Mac OS 10.10 Yosemite.
  • yuяi
    yuяi over 9 years
    Works on Yosemite. So glad I scrolled down and didn't use older answers.
  • steel
    steel over 9 years
    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.
  • Zack Burt
    Zack Burt over 9 years
    I had to do: xcode-select --install .. brew install libxml2 .. bundle config build.nokogiri --use-system-libraries .. bundle install
  • ice cream
    ice cream over 9 years
    huge timesaver, thanks for this tip! worked a charm for me on mavericks
  • Rembrandt Q. Einstein
    Rembrandt Q. Einstein about 9 years
    why do I need the extra "--" in the middle of the gem install command?
  • Filip Kis
    Filip Kis about 9 years
    @RembrandtQ.Einstein because that is how gem install command works when you have extra options. Check guides.rubygems.org/command-reference/#gem-install
  • Filip Kis
    Filip Kis about 9 years
    Did you try using my solution? It does not require xcode intsalation.
  • Dave Munger
    Dave Munger about 9 years
    Thank you! It was a long search to get to this, and it doesn't involve adding a package managers on my mac. FWIW, I'm on Yosemite and this still works!
  • cman77
    cman77 about 9 years
    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
  • Rivera
    Rivera almost 9 years
    Failed for me on 10.10.3. Xcode 6.3 with libxml2 version 2.6.21 or later is required!.
  • Andre Zimpel
    Andre Zimpel almost 9 years
    this. is. it. --with-iconv-dir=/usr/local/Cellar/libiconv/1.14
  • 2upmedia
    2upmedia over 8 years
    Worked for me on 10.10.4. I also installed libxml2, libxslt, and libiconv through MacPorts.
  • Johannes
    Johannes over 8 years
    This is the official solution from the Nokogiri docs (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.
  • jbnunn
    jbnunn over 8 years
    Agreed with your nokogiri sentiment. Every. Single. Time.
  • bwoebi
    bwoebi about 8 years
    Didn't work for me in this form, but ARCHFLAGS="-arch x86_64" sudo gem install nokogiri -- --use-system-libraries worked ...
  • ryudice
    ryudice almost 8 years
    This worked for me as well, much easier solution, I didnt use the ARCHFLAGS, just sudo gem install nokogiri -- --use-system-libraries
  • aaaarrgh
    aaaarrgh almost 8 years
    it was the combo of --use-system-libraries and --with-iconv-dir that did it for me
  • Polsonby
    Polsonby over 7 years
    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.
  • PartialOrder
    PartialOrder almost 7 years
    Confirming gem install nokogiri -- --with-iconv-dir=/opt/local works just fine with MacPorts libiconv installed
  • user24601
    user24601 almost 5 years
    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.
  • mentalic
    mentalic over 4 years
    gem install nokogiri -- --use-system-libraries worked like a charm
  • ryanc
    ryanc about 4 years
    Worked without the --with-xml2-include option for me on Catalina... gem install nokogiri --version=1.6.6.2 -- --use-system-libraries
  • Jack R-G
    Jack R-G over 3 years
    This solution worked for me, with the exception that I got 1.16 instead of 1.14.
  • LucasM
    LucasM over 3 years
    Worked on BigSur and Nokogiri 1.6.3.1! Thanks
  • Евгений Масленков
    Евгений Масленков over 3 years
    This works for me in 2021 on Catalina. Thank you!
  • devjme
    devjme almost 3 years
    THANK YOU THANK YOU THANK YOU!!! This is the only thing that worked for me on Big Sur!!!!!!!
  • babie
    babie about 2 years
    this worked for me on mac m1