Install Nokogiri

First, ensure you have the necessary libraries installed on your system:

brew install libxml2 libxslt

Then, install Nokogiri with the following command:

gem install nokogiri -- --use-system-libraries --with-xml2-include="$(brew --prefix libxml2)/include/libxml2" --with-xml2-lib="$(brew --prefix libxml2)/lib" --with-xslt-include="$(brew --prefix libxslt)/include" --with-xslt-lib="$(brew --prefix libxslt)/lib"

This command tells the gem installer to use the system libraries for libxml2 and libxslt instead of the packaged ones.

If you are using Bundler, you can set the configuration options like this:

bundle config build.nokogiri --use-system-libraries --with-xml2-include="$(brew --prefix libxml2)/include/libxml2" --with-xml2-lib="$(brew --prefix libxml2)/lib" --with-xslt-include="$(brew --prefix libxslt)/include" --with-xslt-lib="$(brew --prefix libxslt)/lib"
bundle install

The error message suggests that the pkg-config tool is not installed, which is required to find the correct library paths during the Nokogiri installation. To resolve this issue, first, install pkg-config:

brew install pkg-config

Alternatively, you can install the pkg-config gem:

gem install pkg-config -v "~> 1.1"

After installing pkg-config, try installing Nokogiri again with the same command you used before:

gem install nokogiri -v '1.8.2' -- --use-system-libraries --with-xml2-include="$(brew --prefix libxml2)/include/libxml2" --with-xml2-lib="$(brew --prefix libxml2)/lib" --with-xslt-include="$(brew --prefix libxslt)/include" --with-xslt-lib="$(brew --prefix libxslt)/lib"

This should resolve the issue and install Nokogiri successfully.