Error installing nokogiri: Failed to build gem native extension & libiconv is missing (OSX)
Asked Answered
F

15

58

I try to clone this repo and run bundle install. The bundle process failed and throw this error:

    ...
    Installing nokogiri 1.6.2.1 with native extensions
    Building nokogiri using packaged libraries.

    Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

        /Users/zulhilmizainudin/.rvm/rubies/ruby-2.2.0/bin/ruby -r ./siteconf20151130-43880-pntnc6.rb extconf.rb
    Building nokogiri using packaged libraries.
    -----
    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=/Users/zulhilmizainudin/.rvm/rubies/ruby-2.2.0/bin/$(RUBY_BASE_NAME)
        --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

    extconf failed, exit code 1

    Gem files will remain installed in /Users/zulhilmizainudin/.rvm/gems/ruby-2.2.0/gems/nokogiri-1.6.2.1 for inspection.
    Results logged to /Users/zulhilmizainudin/.rvm/gems/ruby-2.2.0/extensions/x86_64-darwin-14/2.2.0-static/nokogiri-1.6.2.1/gem_make.out
    An error occurred while installing nokogiri (1.6.2.1), and Bundler cannot continue.
    Make sure that `gem install nokogiri -v '1.6.2.1'` succeeds before bundling.

Then I try to run this:

gem install nokogiri -v '1.6.2.1'

And now I get this error:

Building native extensions.  This could take a while...
Building nokogiri using packaged libraries.
ERROR:  Error installing nokogiri:
    ERROR: Failed to build gem native extension.

    /Users/zulhilmizainudin/.rvm/rubies/ruby-2.2.0/bin/ruby -r ./siteconf20151130-43942-1axq4sj.rb extconf.rb
Building nokogiri using packaged libraries.
-----
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=/Users/zulhilmizainudin/.rvm/rubies/ruby-2.2.0/bin/$(RUBY_BASE_NAME)
    --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

extconf failed, exit code 1

Gem files will remain installed in /Users/zulhilmizainudin/.rvm/gems/ruby-2.2.0/gems/nokogiri-1.6.2.1 for inspection.
Results logged to /Users/zulhilmizainudin/.rvm/gems/ruby-2.2.0/extensions/x86_64-darwin-14/2.2.0-static/nokogiri-1.6.2.1/gem_make.out

I tried to follow this answer but I still get the same error above.

I also try to uninstall and install my brew again (based on this answer) and it's still same.

Also, I try this solution but still get the error.

What should I do now to make sure this nokogiri gem installed successfully?

Note: I'm using OSX 10.11.1 El Capitan.

Follansbee answered 30/11, 2015 at 10:34 Comment(4)
Do you have already installed xcode ? What's the output of command xcode-select --install ? Also have you check the nokogiri OSX installation instruction?Dander
Yes, I already install xcode and run xcode-select --install. The xcode developer tools installed successfully.Follansbee
What led everyone to know the error involved xcode?Underwrite
@JeremyThomas I would like to know too.Southing
S
121

Run these commands:

gem uninstall nokogiri
xcode-select --install
gem install nokogiri

source: http://www.nokogiri.org/tutorials/installing_nokogiri.html#mac_os_x

sometimes mac updates can break xcode CLI so reinstalling can fix the issue: https://github.com/sparklemotion/nokogiri/issues/1445

Scintillant answered 10/6, 2016 at 21:49 Comment(3)
related answer: #19643653Potboiler
You are the best! Thank you for this!Imelda
Thanks for this. I actually has to update my MacOS version to Catalina before xcode-select --install was allowed to run!Gendron
D
32

What worked for me (on macOS Sierra) was a combination of Sam's answer and the following:

gem install nokogiri -- --with-xml2-include=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/usr/include/libxml2 --use-system-libraries
Discobolus answered 5/1, 2017 at 17:46 Comment(3)
This worked for me trying to install nokogiri -v 1.8.0Bomar
I used the same thing to install rails and it worked! Thanks.Caye
this worked for me, just make sure to confirm the folder because mine was /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/usr/include/libxml2Eosinophil
W
30

I had a similar issue with 1.6.7.2 when bundle installing on codeship with ruby 2.3.

What worked for me was adding:

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

before

bundle install

Wilonah answered 25/3, 2016 at 22:55 Comment(2)
This is the best way to fix the problem in a more permanent solution. gem install nokogiri -v 1.6.6.2 -- --use-system-librariesOverdose
Yes, gem install nokogiri -v 1.6.6 -- --use-system-libraries seems to workAnguilliform
H
20

If you're using bundle:

brew install libxml2

# If using Bundle
bundle config build.nokogiri --use-system-libraries \
  --with-xml2-include=$(brew --prefix libxml2)/include/libxml2
bundle install

from http://www.nokogiri.org/tutorials/installing_nokogiri.html

Hardpressed answered 11/5, 2017 at 21:25 Comment(3)
Excellent, this is what worked for me on High Sierra after trying everything above.Brettbretz
Wow. This is the only thing that worked for me. Thank you for sharing this Andy (all the way back in 2017 too!)Basildon
Brilliant! Thank you.Orthographize
A
13

Try this

sudo apt-get install build-essential patch ruby-dev zlib1g-dev liblzma-dev

And then install Nokogiri

sudo gem install nokogiri
Adhesive answered 11/4, 2019 at 7:42 Comment(2)
cha-ching! thx for this. got me over the hump on ubuntu 18.04Unopened
Avoid installing any ruby libraries. You should be using rvm and therefore ruby-dev is not needed to be installed.Undeceive
E
10

I did everything in the other answers, but had to do one more thing:

  1. Open Xcode
  2. Click "Agree"
  3. Wait a bit, close Xcode
  4. Rejoice
Expugnable answered 6/10, 2016 at 20:55 Comment(0)
F
6

Seems like this is due to nokogiri 1.6.2.1 specific problem.

I add this inside my Gemfile:

gem 'nokogiri', '~> 1.6', '>= 1.6.7'

And run bundle update nokogiri and then bundle. Everything now working fine.

Follansbee answered 30/11, 2015 at 10:58 Comment(0)
S
2

It may also be an issue with the anaconda version of some of the libs.

$ conda deactivate
$ gem uninstall nokogiri
$ gem install nokogiri
$ conda activate base

worked for me.

Sackey answered 29/4, 2019 at 18:42 Comment(0)
A
1

I had the similar issue. The reason was outdated xcode.

If you run brew doctor you might see it. Just open AppStore, got to updates tab and update your xcode.

Avraham answered 10/11, 2017 at 13:23 Comment(1)
brew doctor told me about newer Command Line Tools release and gave instructions on how to force update it. That solved the problem for me: sudo rm -rf /Library/Developer/CommandLineTools && sudo xcode-select --installWinery
H
1

Spencer's answer worked for me, but I also needed to use the -n parameter to overcome the permission issue documented here.

So in the end, the command that worked for me was:

gem install -n /usr/local/bin nokogiri -- --with-xml2-include=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/libxml2 --use-system-libraries
Hardfeatured answered 17/4, 2018 at 22:2 Comment(1)
NEVER install gems as sudoHardpressed
R
1

If nothing above works, Try deleting Gemfile.lock, and do reinstalling of gems.

Rowboat answered 10/3, 2019 at 11:10 Comment(2)
You should be careful deleting Gemfile.lock. Not only will you reinstall the gems. But executing bundle install will update the versions of those gemsJoon
Deleting the Gemfile.lock file helped me fix the issue after installing Nokogiri natively as instructed on Nokogiri official websiteArron
C
0

Deleting nokogiri in rbenv gem folder and reinstalling solved it for me.

Continuative answered 11/3, 2016 at 3:37 Comment(0)
P
0

I had the same issue while updating to xcode 10. I resolved it with the following steps :

  1. xcode-select --install
  2. bundle install --deployment
  3. gem install nokogiri
Pectize answered 19/9, 2018 at 16:24 Comment(0)
A
0

This helped me fix this

  • I reinstalled Xcode and directed xcode-select to its path
sudo xcode-select -s /Applications/Xcode.app/Contents/Developer

ref: https://mcmap.net/q/21858/-xcode-select-active-developer-directory-error

Airspace answered 18/3, 2019 at 8:17 Comment(0)
A
-1

Github Answer

sudo installer -pkg /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg -target /

bundle install
Aroid answered 1/10, 2019 at 15:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.