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

8 Answers 8

52

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.

1
  • 5
    running Catalina - this fixed failed attempts to run 'gem update' and 'gem install <gems>'
    – Riana
    Jan 3, 2021 at 0:51
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"
4
  • Thanks, I had similar issue with xcode 12 update. This approach helped me.
    – f0rz
    Dec 2, 2020 at 21:34
  • I had to change the paths to point to my rbenv installation but otherwise it worked. Thanks!
    – jonnysamps
    Dec 28, 2020 at 16:48
  • This solved the problem for Xcode 13.1
    – rami
    Dec 22, 2021 at 14:56
  • It was helpful also for Xcode 13.2.1, when running commands with sudo didn't help, and removing Gemfile.lock didn't help
    – olha
    Feb 16, 2022 at 0:00
13

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

sudo xcode-select -s /Applications/Xcode-beta.app/Contents/Developer
5
  • 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: github.com/flori/json/issues/255
    – Eugenio
    Dec 4, 2018 at 12:00
  • 1
    Please provide explanations for your suggestion, describing what this command does. Thank you! Nov 13, 2020 at 22:00
  • 1
    I had a similar issue with Xcode 12.2. This worked for me! Nov 26, 2020 at 19:52
  • 3
    Confirm, XCode 12 is causing the problem Switching xcode-select to XCode 11.7 works too.
    – rogers
    Jan 4, 2021 at 0:19
  • Great! This solved my issue installing Jekyll! Could you explain what this command is doing? PD: I'm using Xcode 12.5.1 Sep 7, 2021 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.

2
  • 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, 2021 at 19:39
  • Note: I had to run rbenv global 3.0.2 after setting up rbenv Thanks! Nov 2, 2021 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 /
1
  • 1
    Thank you! All other suggestions here (and on other SO threads) failed. This was the ticket.
    – Paul Irish
    Mar 13, 2019 at 17:03
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"
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
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.