libxml-ruby: Failed to build gem native extension

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

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.


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

Brew link solved the issue brew link --force libxml2