Fix: Failed to build gem native extension, cannot find ruby.h

Under macOS Mojave 10.14.6, When I tried to do gem install jekyll locally, I met the following error:

mkmf.rb can't find header files for ruby at /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/include/ruby.h

it is due to xcode has been upgraded, but the command line tool is not setting up correctly.

the solution is as following:

sudo rm -rf /Library/Developer/CommandLineTools
xcode-select --install
sudo xcodebuild -license accept
open /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg

after fixed that error, continune to build jekyll, it complains ruby version is too low.

ERROR: Error installing jekyll:
jekyll-sass-converter requires Ruby version >= 2.4.0.
~ moreless$ which ruby
~ moreless$ brew upgrade ruby
Warning: ruby 2.6.5 already installed

therefore, reinstall ruby, and got following message:

$ brew reinstall ruby
==> Reinstalling ruby
==> Downloading
Already downloaded: /Users/yonwang/Library/Caches/Homebrew/downloads/182d59bf6e0c972f8d9271f628e7b87879c16b1954b0c886d286f83519c7e7b1 — ruby-2.6.5.mojave.bottle.1.tar.gz
==> Pouring ruby-2.6.5.mojave.bottle.1.tar.gz
==> Caveats
By default, binaries installed by gem will be placed into:
You may want to add this to your PATH.ruby is keg-only, which means it was not symlinked into /usr/local,
because macOS already provides this software and installing another version in
parallel can cause all kinds of trouble.
If you need to have ruby first in your PATH run:
echo 'export PATH="/usr/local/opt/ruby/bin:$PATH"' >> ~/.bash_profile
For compilers to find ruby you may need to set:
export LDFLAGS="-L/usr/local/opt/ruby/lib"
export CPPFLAGS="-I/usr/local/opt/ruby/include"

After modifying .bash_profile as suggested, path to ruby will be updated to the new path.


Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store