16

I'm trying to install libxml-ruby. I have installed libxml2, libxslt and coreutils

I have also read other posts regarding the issue but none solved it for me.

brew list libxslt

/opt/boxen/homebrew/Cellar/libxslt/1.1.28_1/bin/xslt-config
/opt/boxen/homebrew/Cellar/libxslt/1.1.28_1/bin/xsltproc
/opt/boxen/homebrew/Cellar/libxslt/1.1.28_1/include/libexslt/ (3 files)
/opt/boxen/homebrew/Cellar/libxslt/1.1.28_1/include/libxslt/ (21 files)
/opt/boxen/homebrew/Cellar/libxslt/1.1.28_1/lib/libexslt.0.dylib
/opt/boxen/homebrew/Cellar/libxslt/1.1.28_1/lib/libxslt.1.dylib
/opt/boxen/homebrew/Cellar/libxslt/1.1.28_1/lib/pkgconfig/ (2 files)
/opt/boxen/homebrew/Cellar/libxslt/1.1.28_1/lib/ (5 other files)
/opt/boxen/homebrew/Cellar/libxslt/1.1.28_1/share/aclocal/libxslt.m4
/opt/boxen/homebrew/Cellar/libxslt/1.1.28_1/share/doc/ (90 files)
/opt/boxen/homebrew/Cellar/libxslt/1.1.28_1/share/man/ (3 files)

brew list libxml2

/opt/boxen/homebrew/Cellar/libxml2/2.9.4/bin/xml2-config
/opt/boxen/homebrew/Cellar/libxml2/2.9.4/bin/xmlcatalog
/opt/boxen/homebrew/Cellar/libxml2/2.9.4/bin/xmllint
/opt/boxen/homebrew/Cellar/libxml2/2.9.4/include/libxml2/ (47 files)
/opt/boxen/homebrew/Cellar/libxml2/2.9.4/lib/libxml2.2.dylib
/opt/boxen/homebrew/Cellar/libxml2/2.9.4/lib/cmake/libxml2/libxml2-config.cmake
/opt/boxen/homebrew/Cellar/libxml2/2.9.4/lib/pkgconfig/libxml-2.0.pc
/opt/boxen/homebrew/Cellar/libxml2/2.9.4/lib/ (3 other files)
/opt/boxen/homebrew/Cellar/libxml2/2.9.4/share/aclocal/libxml.m4
/opt/boxen/homebrew/Cellar/libxml2/2.9.4/share/doc/ (153 files)
/opt/boxen/homebrew/Cellar/libxml2/2.9.4/share/gtk-doc/ (55 files)
/opt/boxen/homebrew/Cellar/libxml2/2.9.4/share/man/ (4 files)

gem install libxml-ruby produces the following error:

Building native extensions.  This could take a while...
ERROR:  Error installing libxml-ruby:
    ERROR: Failed to build gem native extension.

    current directory: /opt/rubies/2.3.0/lib/ruby/gems/2.3.0/gems/libxml-ruby-2.9.0/ext/libxml
/opt/boxen/rbenv/versions/2.3.0/bin/ruby -r ./siteconf20160630-68248-1qaz7dk.rb extconf.rb
checking for libxml/xmlversion.h in /opt/include/libxml2,/opt/local/include/libxml2,/usr/local/include/libxml2,/usr/include/libxml2... no
*** 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=/opt/boxen/rbenv/versions/2.3.0/bin/$(RUBY_BASE_NAME)
    --with-xml2-config
    --without-xml2-config
    --with-xml2-dir
    --without-xml2-dir
    --with-xml2-include
    --without-xml2-include=${xml2-dir}/include
    --with-xml2-lib
    --without-xml2-lib=${xml2-dir}/lib
 extconf failure: need libxml2.

    Install the library or try one of the following options to extconf.rb:

      --with-xml2-config=/path/to/xml2-config
      --with-xml2-dir=/path/to/libxml2
      --with-xml2-lib=/path/to/libxml2/lib
      --with-xml2-include=/path/to/libxml2/include


To see why this extension failed to compile, please check the mkmf.log which can be found here:

  /opt/boxen/rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/extensions/x86_64-darwin-14/2.3.0-static/libxml-ruby-2.9.0/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in /opt/boxen/rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/libxml-ruby-2.9.0 for inspection.
Results logged to /opt/boxen/rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/extensions/x86_64-darwin-14/2.3.0-static/libxml-ruby-2.9.0/gem_make.out
Mecki
  • 125,244
  • 33
  • 244
  • 253
Cjmarkham
  • 9,484
  • 5
  • 48
  • 81
  • Did you confirm that `libxml/xmlversion.h` is in one of the four directories that it claimed it's checking `/opt/include/libxml2,/opt/local/include/libxml2,/usr/local/include/libxml2,/usr/include/libxml2`? – Mike S Jun 30 '16 at 23:02
  • I solved this by running `gem install libxml-ruby -v '3.0.0' -- --with-xml2-config=/usr/local/opt/libxml2/bin/xml2-config --with-xml2-dir=/usr/local/opt/libxml2 --with-xml2-lib=/usr/local/opt/libxml2/lib --with-xml2-include=/usr/local/opt/libxml2/include` – Korayem Jan 12 '19 at 00:45
  • Example which works for me (Please verify your libxml2 path) `gem install libxml-ruby -v '3.0.0' -- --with-xml2-config=/usr/local/Cellar/libxml2/2.9.9_2/bin/xml2-config --with-xml2-dir=/usr/local/Cellar/libxml2/2.9.9_2/ --with-xml2-lib=/usr/local/Cellar/libxml2/2.9.9_2/lib/ --with-xml2-include=/usr/local/Cellar/libxml2/2.9.9_2/include/` – KrunaL Aug 13 '19 at 11:14

7 Answers7

18

I had a similar issue after I had used brew to install libxml2

Brew link solved the issue brew link --force libxml2

Dan
  • 1,136
  • 10
  • 24
  • 1
    But you are aware that libxml2 is always part of macOS and now you just added a 2nd copy of it with brew in a 2nd location that could possibly be incompatible with the already installed one, are you? – Mecki Mar 06 '19 at 16:26
16

Life after upgrading to macOS Mojave.

xcode-select --install
brew install libxml2
brew link --force libxml2
gem install libxml-ruby -v '2.9.0' -- --use-system-libraries=true --with-xml2-include="$(xcrun --show-sdk-path)"/usr/include/libxml2

In my example I installed version 2.9.0 of the libxml-ruby gem. Change the version string to suit your needs. If available, examine the Gemfile.lock to identify which version of libxml-ruby Bundler is needs to install.

scarver2
  • 7,887
  • 2
  • 53
  • 61
  • 1
    Without specifying an arbitrary version, I succeeded with just `sudo gem update libxml-ruby -- --use-system-libraries=true --with-xml2-include="$(xcrun --show-sdk-path)"/usr/include/libxml2` – Cœur Jan 28 '19 at 11:24
  • 2
    This worked beautifully, thank you. The `brew link --force` gave me a warning that it refused to link it, but somehow I guess that didn't matter – user2490003 Feb 25 '19 at 18:54
  • 1
    That's creating a _really_ confusing situation. You're linking homebrew's libxml2 (breaking it out of its keg-only status) *and* explicitly listing OSX's include folder. What does the gem use now use as xml2's *lib* folder? – Noach Magedman May 06 '19 at 12:00
  • 1
    This worked for me without the dreaded: `brew link --force libxml2` – jpgeek May 12 '20 at 10:29
13

Homebrew:

libxml2 is keg-only, which means it was not symlinked into /usr/local, because macOS already provides this software and installing another version in parallel can cause all kinds of trouble.

So, we need to set libxml path directly

brew install libxml2

gem install libxml-ruby \
-- \
--with-xml2-config="$(brew --prefix libxml2)/bin/xml2-config"

P.S.

If you get the exception on lastest rubies:

Just ignore it. The gem was successfully installed. Just the Rdoc documentation generation failed.

The internal error was:

        (NoMethodError) undefined method `[]' for nil:NilClass

ERROR:  While executing gem ... (NoMethodError)
    undefined method `[]' for nil:NilClass
itsnikolay
  • 17,415
  • 4
  • 65
  • 64
7

macOS comes with libmxl2 installed, always, there is no need to use a utility like brew to install it first. Yet the headers are not found in /usr/include/libxml but in /usr/include/libxml2/libxml.

The configure script of the libxml-ruby gem tries to detect if libxml is installed by compiling code that looks like this:

#include <libxml/xmlversion.h>

But when compiling with the compiler shipped with Xcode, only this code would work:

#include <libxml2/libxml/xmlversion.h>

The trick is to let the configure script know where to find the headers. On a macOS system with Xcode installed, the following will work:

gem install libxml-ruby -v '3.1.0' -- --with-xml2-include=`xcrun --show-sdk-path`/usr/include/libxml2
Mecki
  • 125,244
  • 33
  • 244
  • 253
5

The high level issue is shown here:

checking for libxml/xmlversion.h in /opt/include/libxml2,/opt/local/include/libxml2,/usr/local/include/libxml2,/usr/include/libxml2... no

Basically, the installer is telling you it can't find libxml/xmlversion.h but more generally it most likely can't find your libxml2 location. It tells you which four directories it's looking in so the first step is to check those directories to see if libxml2 is there.

Assuming it's not in any of those four locations, you need to find it. Run find / -name xmlversion.h (might need sudo) to figure out where libxml2 got installed. Based on this answer I'm guessing your issue is that brew installed it to /usr/local/Cellar/libxml2 but you should confirm that. Once you have the location you can manually point your gem install command to it. Something like this: (but fill in path/to with your actual path)

gem install libxml-ruby --with-xml2-dir=/path/to/libxml2 --with-xml2-lib=/path/to/libxml2/lib --with-xml2-include=/path/to/libxml2/include
Community
  • 1
  • 1
Mike S
  • 11,329
  • 6
  • 41
  • 76
  • Thanks, I used the options to install the gem and it worked. But I need these options in the bundle config. I have tried the following config but it didnt work: `BUNDLE_BUILD__LIBXML-RUBY: "--with-xml2-lib=/opt/boxen/homebrew/opt/libxml2/lib --with-xml2-include=/opt/boxen/homebrew/opt/libxml2/include/libxml2 --with-xslt-lib=/opt/boxen/homebrew/opt/libxslt/lib --with-xslt-include=/opt/boxen/homebrew/opt/libxslt/include"` – Cjmarkham Jul 01 '16 at 10:11
  • @CarlMarkham I would recommend posting the bundle config issue as a separate question so you can past the full command terminal and output. – Mike S Jul 01 '16 at 21:00
  • 5
    Building on this answer. The following worked for me. Use "find" to locate your xmlversion.h file --> gem install libxml-ruby -- --with-xml2-include=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/libxml2/ --use-system-libraries – Jay Oct 23 '17 at 22:28
4

I also used brew to install libxml2 but needed the following options:

brew install libxml2 --with-xml2-config
gem install libxml-ruby -- --with-xml2-config=/usr/local/homebrew/opt/libxml2/bin/xml2-config
milkfarm
  • 308
  • 2
  • 7
  • 1
    Worked for me. `--with-xml2-config` is not needed (anymore?) for the brew installation. – Frizlab Oct 30 '18 at 09:44
  • But you are aware that libxml2 is always part of macOS and now you just added a 2nd copy of it with brew in a 2nd location that could possibly be incompatible with the already installed one, are you? – Mecki Mar 06 '19 at 16:26
  • 1
    This solution is definitely for an older setup. As Frizlab mentions, `--with-xml2-config` is no longer a valid option. On a newer setup, I used this [linked SO thread to solve](https://stackoverflow.com/questions/53222641/bundle-install-fails-to-install-libxml-ruby). Mecki's comment seems valid but I haven't tested. – milkfarm Mar 07 '19 at 17:51
1

One solution is to:

cd /Library/Developer/CommandLineTools/Packages/ $ 
open macOS_SDK_headers_for_macOS_10.14.pkg

To force the reinstallation of the headers for macOS 10.14.