Recurring Issues Installing Ruby C Extensions on OSX Mojave

Thu Mar 7, 2019
~400 Words

Recently I have had to take the time to fix failed installations of Ruby libraries which build C extensions on Mojave in multiple development environments and across multiple versions of Ruby. The solution(s) are in the end thankfully quite straightforward.

The most recent error was with nokogiri:

Installing nokogiri 1.8.5 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    current directory: /Users/styrmis/.rbenv/versions/2.4.5/lib/ruby/gems/2.4.0/gems/nokogiri-1.8.5/ext/nokogiri
/Users/styrmis/.rbenv/versions/2.4.5/bin/ruby -r ./siteconf20190307-87855-1kzst5q.rb extconf.rb --use-system-libraries=true
checking if the C compiler accepts ... yes
checking if the C compiler accepts -Wno-error=unused-command-line-argument-hard-error-in-future... no
Building nokogiri using system libraries.
ERROR: cannot discover where libxml2 is located on your system. please make sure `pkg-config` is installed.

The solution to this kind of problem in the past has often been to ensure that the Xcode command line tools (and so headers) are installed. However in recent cases this has not worked, citing that the tools are already installed:

% xcode-select --install
xcode-select: error: command line tools are already installed, use "Software Update" to install updates

As there were no new versions to install (via Software Update), then there was seemingly nothing left to do on this front.

On Github and StackOverflow various people suggest adding in manual symlinks or other temporary fixes which are likely to introduce harder-to-diagnose problems further down the line.

A better solution was found in the following comment, which referenced this release note from Apple which outlines that the header files have moved, but that it is possible to restore a copy of the headers to the old location by running the following installer, which will already be on your machine:

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

This fixed the failing nokogiri install, and would likely also have resolved the recent issues I had with readline and Ruby.

This however is also a temporary fix, as it simply allows you to continue to use older software that references the old location for headers. In this particular case I realised that the project in question had been pinned to ~> 1.8 for nokogiri–had this been unpinned then a bundle update nokogiri would have resolved this issue, as later versions of nokogiri include a fix for this particular issue.