gem install - fatal error: 'ruby/config.h' file not found in Mojave

I got into this issue when trying to install fastlane with homebrew. None of the suggestions mentioned earlier helped me. Manually installing the macOS_SDK_headers_for_macOS_10.14.pkg fixed it.

sudo installer -pkg /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg -target /

Update 15/03/2021

According to Apple, Ruby won't be included anymore in future versions of the system, so using a Ruby version manager will be the only way to go once this will actually happen.


Original answer

After hours and hours of trial and errors I wanted to go deeper into the reasons of this error and I finally found this that I think is the clean way to go around this issue:

Catalina (NdMe: I think this started earlier, on Mojave) no longer supports including macOS headers in command line tools, they have to be explicitly used through Xcode SDK. You can do so by running gem through the xcrun tool like:

xcrun gem install <your_gem>

Quoted from a Github issue comment.


Xcode 12 upgrade messed this up for me.

I installed ruby from brew and added this to my .zshrc

export PATH="/usr/local/opt/ruby/bin:$PATH"
export LDFLAGS="-L/usr/local/opt/ruby/lib"
export CPPFLAGS="-I/usr/local/opt/ruby/include"
export PKG_CONFIG_PATH="/usr/local/opt/ruby/lib/pkgconfig"

If you have the Xcode 10 beta running this might fix it

sudo xcode-select -s /Applications/Xcode-beta.app/Contents/Developer