macOS Mojave 'ruby/config.h' file not found

When I try to build vim(8.1.0509) with ruby support(--enable-rubyinterp) on macOS Mojave(10.14.1) I get error:

In file included from /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/include/ruby-2.3.0/ruby.h:33:
/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/include/ruby-2.3.0/ruby/ruby.h:24:10: fatal error:
      'ruby/config.h' file not found
#include "ruby/config.h"

I have installed Xcode 10.1.

xcode-select -p
/Applications/Xcode.app/Contents/Developer

Thanks


Catalina, Big Sur, etc. (As of 2020 and 2021)

Recent versions of macOS and Xcode (e.g. Catalina 10.15 and Xcode 12.2) definitely have this broken. Reinstalling Xcode and xcode-select --install did not help for me at all (the solutions already posted seem to be outdated). I did not want to use rvm because it may cause some annoying problems.

I did a workaround by manually making a symbolic link:

cd /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/include/ruby-2.6.0/ruby
ln -sf ../../../../Headers/ruby/config.h

where ruby/config.h can be found at: /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk/System/Library/Frameworks/Ruby.framework/Versions/2.6/Headers/ruby/config.h.

You should change SDK versions (e.g. 11.1 in the above example) accordingly to your current xcode installation.

It is also very likely that one runs into another error where .../universal-darwin19/ruby/config.h cannot be found (Commonmarker gem cannot be installed (needed for jekyll) macos). It can be quick-fixed as follows:

cd /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/include/ruby-2.6.0
ln -sf universal-darwin20 universal-darwin19

macOS Catalina

Issue reappeared in Catalina and I wasn't able to spot this package in the Developer SDK. Until better solution found, I was able to workaround this with rubyenv: https://github.com/orta/cocoapods-keys/issues/198#issuecomment-510909030

brew install rbenv ruby-build
echo 'if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi' >> ~/.bash_profile 
rbenv install 2.6.3
rbenv global 2.6.3

Mojave and older

This answer helped me: https://stackoverflow.com/a/53194299/2105993

xcode-select --install
open /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg