36

gem install is failing in MacOs Mojave. Anything that can help me solve this? My ruby version is ruby 2.3.7p456.

➜ sudo gem install json -v '1.8.3'



current directory: /Library/Ruby/Gems/2.3.0/gems/json-1.8.3/ext/json/ext/generator
make "DESTDIR="
compiling generator.c
In file included from generator.c:1:
In file included from ./../fbuffer/fbuffer.h:5:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/include/ruby-2.3.0/ruby.h:33:
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/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"
         ^~~~~~~~~~~~~~~
1 error generated.
make: *** [generator.o] Error 1

make failed, exit code 2
Joe
  • 41,484
  • 20
  • 104
  • 125
SpaceX
  • 555
  • 1
  • 6
  • 16

8 Answers8

51

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.

CristinaTheDev
  • 1,952
  • 2
  • 17
  • 25
24

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"
Iulian Onofrei
  • 9,188
  • 10
  • 67
  • 113
3h4x
  • 936
  • 8
  • 14
13

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

sudo xcode-select -s /Applications/Xcode-beta.app/Contents/Developer
valexa
  • 4,462
  • 32
  • 48
  • In my case I needed to re-install Xcode command line tools, I think it was because I updated Xcode. This post gave me the correct hint: https://github.com/flori/json/issues/255 – Eugenio Dec 04 '18 at 12:00
  • Please provide explanations for your suggestion, describing what this command does. Thank you! – Caleb Hensley Nov 13 '20 at 22:00
  • 1
    I had a similar issue with Xcode 12.2. This worked for me! – Zaheer Moola Nov 26 '20 at 19:52
  • 3
    Confirm, XCode 12 is causing the problem Switching xcode-select to XCode 11.7 works too. – rogers Jan 04 '21 at 00:19
  • Great! This solved my issue installing Jekyll! Could you explain what this command is doing? PD: I'm using Xcode 12.5.1 – caravana_942 Sep 07 '21 at 21:48
12

In macOS Monterey (in the Beta at least), Ruby (ruby 2.6.3p62 (2019-04-16 revision 67580)) is still included but it doesn't seem to work well (fatal error: 'ruby/config.h' file not found), so I installed my own version of Ruby. You can install ruby manually, but I prefer to use rbenv, a Ruby version manager.


Install ruby using rbenv

  • Install rbenv: brew install rbenv ruby-build
    • ruby-build is a plugin for rbenv to actually install a ruby version, it provides ruby install command.
  • Setup rbenv: rbenv init, (and follow its recommended instructions, for example if you're using zsh, it says add eval "$(rbenv init -)" to ~/.zshrc)
  • Install ruby: rbenv install 3.0.1. Latest version listed here
  • Do what you came to do: sudo gem install cocoapods or bundle install

Or install ruby manually:

  • Run brew install ruby
  • Run echo 'export PATH="/opt/homebrew/opt/ruby/bin:$PATH"' >> ~/.zshrc so that this version is used
  • Restart terminal

asdf?

An alternative to rbenv is asdf, but I find asdf a bit complicated to use, since it supports more than just ruby, NodeJS. I would argue it does none of them very ergonomically.


Note: I have an M1 mac, and it's still working.

Ben Butterworth
  • 22,056
  • 10
  • 114
  • 167
  • Thanks, Ben. Running Monterey and Xcode 13 Beta 2. Manual install fixed it for me but required some extra exports. From the install: If you need to have ruby first in your PATH, run: - echo 'export PATH="/usr/local/opt/ruby/bin:$PATH"' >> /Users/[username]/.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" For pkg-config to find ruby you may need to set: - export PKG_CONFIG_PATH="/usr/local/opt/ruby/lib/pkgconfig" – Sean Jul 11 '21 at 19:39
  • Note: I had to run `rbenv global 3.0.2` after setting up `rbenv` Thanks! – Aviel Gross Nov 02 '21 at 21:20
6

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 /
karolszafranski
  • 702
  • 8
  • 17
2

I was having this same issue where I could not install native extensions due to a failure when including "ruby/config.h".

I believe the cause of this issue was because I was using the ruby version installed with Homebrew. After upgrading to a new version of Ruby with Homebrew, I saw the following helpful message from Homebrew:

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"

For pkg-config to find ruby you may need to set:
 export PKG_CONFIG_PATH="/usr/local/opt/ruby/lib/pkgconfig"

Adding the following lines to my ~/.bash_profile fixed the issue for me:

export LDFLAGS="-L/usr/local/opt/ruby/lib"
export CPPFLAGS="-I/usr/local/opt/ruby/include"
gatkin
  • 561
  • 5
  • 5
2

xcrun gem install <your_gem> worked for me, but I had to do it multiple times — every time bundle install failed.

The best solution is probably to set the same vars as xcrun in your .zshrc file. xcrun env on my system adds:

SDKROOT=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk
CPATH=/usr/local/include
LIBRARY_PATH=/usr/local/lib
David Gish
  • 750
  • 6
  • 14
0

I had this problem after installing Big Sur. I found the file ruby/config.h was not present in the Xcode Packages in the Xcode versions I had installed prior to upgrading. I was able to correct it by uninstalling the Xcode versions and reinstalling them.

Litehouse
  • 864
  • 8
  • 14