'Cannot install Ruby gem libxslt-ruby on Mac cause it does not find libxml2

I've installed libxml2 and libxslt through Brew on my Mac but

gem install libxslt-ruby -- --with-xml2-dir=/usr/local/opt/libxml2 --with-xml2-lib=/usr/local/opt/libxml2/lib --with-xml2-include=/usr/local/opt/libxml2/include --with-xslt-include=/usr/local/opt/libxslt/include --version=1.1.1

or

gem install libxslt-ruby -- --with-xml2-dir=/usr/local/Cellar/libxml2/2.9.1 --with-xml2-lib=/usr/local/Cellar/libxml2/2.9.1/lib --with-xml2-include=/usr/local/Cellar/libxml2/2.9.1/include/libxml2 --version=1.1.1

throws

    Building native extensions.  This could take a while...
    /Users/gawlim/.rbenv/versions/1.9.3-p484/lib/ruby/1.9.1/rubygems/ext/builder.rb:48: warning: Insecure world writable dir /usr/local/Cellar in PATH, mode 040777
    ERROR:  Error installing libxslt-ruby:
        ERROR: Failed to build gem native extension.

            /Users/gawlim/.rbenv/versions/1.9.3-p484/bin/ruby extconf.rb --with-xml2-dir=/usr/local/opt/libxml2 --with-xml2-lib=/usr/local/opt/libxml2/lib --with-xml2-include=/usr/local/opt/libxml2/include --with-xslt-include=/usr/local/opt/libxslt/include -v 1.0.8
    checking for ruby/io.h... yes
    checking for inflate() in -lz... yes
    checking for iconv_open() in -liconv... yes
    checking for xmlXPtrNewRange() in -lxml2... yes
    checking for libxml/xmlversion.h... no
    checking for libxml/xmlversion.h in /opt/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=/Users/gawlim/.rbenv/versions/1.9.3-p484/bin/ruby
        --with-iconv-dir
        --without-iconv-dir
        --with-iconv-include
        --without-iconv-include=${iconv-dir}/include
        --with-iconv-lib
        --without-iconv-lib=${iconv-dir}/lib
        --with-zlib-dir
        --without-zlib-dir
        --with-zlib-include
        --without-zlib-include=${zlib-dir}/include
        --with-zlib-lib
        --without-zlib-lib=${zlib-dir}/lib
        --with-xml2-dir
        --with-xml2-include=${xml2-dir}/include
        --with-xml2-lib=${xml2-dir}/lib
        --with-xslt-dir
        --without-xslt-dir
        --with-xslt-include=${xslt-dir}/include
        --with-xslt-lib
        --without-xslt-lib=${xslt-dir}/lib
        --with-exslt-dir
        --without-exslt-dir
        --with-exslt-include
        --without-exslt-include=${exslt-dir}/include
        --with-exslt-lib
        --without-exslt-lib=${exslt-dir}/lib
        --with-zlib
        --without-zlib
        --with-iconvlib
        --without-iconvlib
        --with-xml2lib
        --without-xml2lib
     extconf failure: %s
    need libxml2.

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

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


Solution 1:[1]

Although an old question, I though I'd share the solution I found. Before installing the libxslt-ruby gem, run the following:

gem update --system

Here's the documentation for that command:

http://guides.rubygems.org/command-reference/#gem-update

Solution 2:[2]

As of April 2022, the following worked for me:

brew install libxslt
gem install libxslt-ruby -v '1.2.0' --  --with-xslt-include="$(brew --prefix libxslt)/include/libxslt" --with-exslt-include="$(brew --prefix libxslt)/include/libexslt"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sam Murray
Solution 2 sandbergja