97

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

double-beep
  • 4,567
  • 13
  • 30
  • 40
maksimr
  • 3,282
  • 1
  • 21
  • 21

20 Answers20

126

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
Jongwook Choi
  • 6,879
  • 3
  • 20
  • 15
  • 1
    I can only see univeral-darwin20 in my path. can l still run ```ln -sf universal-darwin20 univeral-darwin19``` – leeCoder Jan 05 '21 at 17:02
  • Yes that's the reason why you would need to run the command to make a symbolic link `universal-darwin19` to `universal-darwin20` (please note a typo around `universal-darwin19`; thanks Ewan for fix in the original answer). – Jongwook Choi Jan 06 '21 at 18:27
  • 2
    Tried installing `rbenv`. Tried uninstalling and reinstalling `xcode tools`. But only this solution worked. Although, I'm seriously contemplating backing up everything and reseting my machine after all this – Sylvan D Ash Feb 08 '21 at 18:17
  • This workaround did not work. Need to perform an update in newer versions. – thalacker Jul 28 '21 at 17:27
  • 8
    I ran into a very similar issue with the Xcode 13 beta (not with vim but with a different project). It looks like the files are now inside the `universal-darwin21` folder for Xcode 13 so I had to update the command to be `ln -sf universal-darwin21 universal-darwin19` and also add `ln -sf universal-darwin21 universal-darwin20` to get my project to build. – Kevin Johnson Sep 13 '21 at 22:49
  • This didn't work and the resulted binary crashed. I reverted the changes and suggest against doing this. It's better to install another version of ruby next to the system one and not mangle with the latter as you risk more issues in the future. – Greg Dubicki Dec 22 '21 at 08:39
  • Getting `Before reporting this, could you check that the file you're documenting has proper syntax: /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/bin/ruby -c lib/jekyll/commands/doctor.rb. (NoMethodError) undefined method `[]' for nil:NilClass` – Anand Rockzz Jan 24 '22 at 06:51
  • 2
    This is a good technique but to update & simplify: `cd $(xcode-select -p)/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks/Ruby.framework/Versions/Current/usr/include/ruby-2.*`, then see what `universal-darwin*/` directory (directories?) you have, then make a symlink to it with a lower version number `20` or `21` as needed, e.g. `sudo ln -s universal-darwin21 universal-darwin20`. – Jerry101 Feb 02 '22 at 18:38
  • Worked for me! Thank you very much. On my BigSur I ran `cd /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/include/ruby-2.6.0/ruby/` then ´sudo ln -sf ../../../../Headers/ruby/config.h` followed by `cd /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/include/ruby-2.6.0` and a `sudo ln -sf universal-darwin21 universal-darwin20` – jonashackt May 10 '22 at 13:47
120

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
ReDetection
  • 3,012
  • 2
  • 21
  • 40
  • 3
    This should be the accepted answer @maksimr as it does exactly what fixes the problem without completely reinstalling macOS – ohcibi Jan 18 '19 at 13:45
  • 1
    Seems promising until the warning: "This package is incompatible with this version of macOS and may fail to install. [...] may damage your system [...]". I am just going to try asdf ( https://github.com/asdf-vm/asdf), which seems to have ruby in its module list. – FooF Mar 31 '20 at 11:45
  • With asdf ruby, gem wants to install packages into the ruby 2.3 system folder, so I stopped trying this. This seemed wrong, and I do not want to use sudo. Homebrew has ruby, and when running using the complete path to its gem utility, as it is by default not activated, my problematic gem could be installed (without sudo, yay). – FooF Mar 31 '20 at 12:08
  • The first cmd alone fixed the issue in my case. Not sure if it's related to how the 1st cmd works currently, but there was no .pkg file for me to open after the install was complete. After completing the 1st step, this command worked successfully: `sudo gem install cocoapods` - Thank you – Gene Bo May 08 '20 at 18:57
  • 2
    for me `brew install ruby`, then `export PKG_CONFIG_PATH="/usr/local/opt/ruby/lib/pkgconfig"` solved the issue (which was `sudo gem install eventmachine -v '1.2.7'`) – kino1 Sep 06 '20 at 03:09
  • This fixed `gem install fpm-cookery` from https://fpm-cookery.readthedocs.io/en/latest/pages/getting-started/ for me that was failing with the error OP posted on Catalina. Thank you! – Brent Writes Code May 19 '22 at 20:48
65

On macOS Catalina
After installing and uninstalling the developer tools multiple times, this is the only thing that worked for me:

First install the Ruby Version Manager rvm:

curl -L https://get.rvm.io | bash -s stable

Then install the latest version of ruby:

rvm install ruby-2.7.2

Finally try again to install the cocoapods:

sudo gem install cocoapods

(from this answer: https://stackoverflow.com/a/65033418/3605761)

edit: may need rvm reinstall ruby-2.7.2 instead of rvm install ruby-2.7.2 at step 2.

edit2: @greg-dubicki mentioned swapping ruby-2.7.2 for ruby-3.0.3: rvm install ruby-3.0.3 and then making it the default: rvm --default use 3.0.3 they also mentioned: As an extra benefit, thanks to the switch from the outdated system Ruby 2 to a current version 3, you will get up to 3 x better performance.

Eden
  • 1,592
  • 13
  • 23
  • 3
    make sure u use the correct version of ruby after install : `ruby --version` to check version and `rvm use 2.7.2` to set – Jack Ng Feb 22 '21 at 14:06
  • @greg-dubicki mentioned swapping ruby-2.7.2 for ruby-3.0.3: `rvm install ruby-3.0.3` and then making it the default: `rvm --default use 3.0.3` they also mentioned: As an extra benefit, thanks to the switch from the outdated system Ruby 2 to a current version 3, you will get up to 3 x better performance. – Eden Jan 05 '22 at 07:19
  • 1
    Thankyou! This is the only response that worked for me. Updated to 3.1.0 set version with `rvm use 3.1.0` – plam Feb 04 '22 at 10:05
19

I had the exact same problem. Could not install gems with native extensions. Running

$ open /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg

failed, because the file did not exist. Looks like Mojave can break the commandline tools.

I fixed it by removing the Xcode commandline tools, installing them again and then installing the missing headers:

$ sudo rm -rf /Library/Developer/CommandLineTools
$ xcode-select --install
$ open /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg
atamanroman
  • 10,999
  • 7
  • 52
  • 78
18

If you are facing this issue in Mac Big Sur, reinstalling CommandLineTools can fix this issue.

sudo rm -rf /Library/Developer/CommandLineTools

xcode-select --install
THess
  • 993
  • 1
  • 10
  • 21
Suraj Poddar
  • 306
  • 2
  • 3
  • This took a long time and installed an older version of CommandLineTools. Maybe that was the point(!) but I didn't realize that, let Software Update update it, and the result didn't fix the problem. The symlink approach in the top answer worked, with adjustments. – Jerry101 Feb 02 '22 at 18:42
4

On macOS Catalina If multiple versions of xcode are installed,xcode12/xcode12.4 etc.Please try to change your xcode command line tools in xcode preferences. It may works!!!

iticle
  • 123
  • 4
4

make sure Xcode version and macOS version match each other:

  • macOS 10.15: Xcode < 12.2
  • macOS 11: Xcode >= 12.2, <= 13.0
  • macOS 12: Xcode >= 13.1

This is because some versions of Xcode don't have the macOS SDK for the OS it's running on, when compiling native extension of gem.

leavez
  • 1,880
  • 2
  • 23
  • 33
3

I tried all of the above and I eventually got it working with this little 'gem', pardon the pun.

sudo gem install -n /usr/local/bin cocoapods -v 1.8.4

From https://developer.apple.com/forums/thread/668456

JupiterT
  • 173
  • 1
  • 8
2

2021 Advice
If you are able, updating to macOS Big Sur and then updating Xcode will also fix the issue.

Just tested on updating 10.15 -> 11.2.3 for macOS.
And 12.4 for Xcode.

thalacker
  • 1,639
  • 20
  • 33
1

MacOS Big Sur Beta 8

try this:

  1. install rbenv
  • git clone https://github.com/rbenv/rbenv.git ~/.rbenv

  • cd ~/.rbenv && src/configure && make -C src

  • Add ~/.rbenv/bin to your $PATH for access to the rbenv command-line utility. see: https://github.com/rbenv/rbenv#basic-github-checkout

  • make sure export PATH="$HOME/.rbenv/shims:${PATH}" was added to your $PATH

  1. xcode-select --switch /Applications/Xcode.app/Contents/Develope

  2. gem install cocoapods should work like a charm!

ukiyoevega
  • 129
  • 1
  • 5
1

for me it worked "rvm install ruby" (not with brew) and then doing steps from here:

$ sudo rm -rf /Library/Developer/CommandLineTools
$ xcode-select --install

and then

export PKG_CONFIG_PATH="/usr/local/opt/ruby/lib/pkgconfig" 

After all this, it worked.

Mihaela Romanca
  • 1,300
  • 2
  • 13
  • 22
0

My problem was different from - Yosemite upgrade broke ruby.h

As you can see in the description of the problem it's System's ruby, not Xcode's ruby so reinstall Xcode or symlink didn't help here.

Answer: I have reinstalled macOS Mojave without losing any data and this fixed the problem.

maksimr
  • 3,282
  • 1
  • 21
  • 21
  • I was running into this issue when trying to install Command-T for macvim and was worried I needed to drop the big bomb of reinstalling Mojave. Instead, I found the System ruby was actually Ruby 2.3.7 (Macvim was built against this System ruby, version 2.3.7), and I installed ruby 2.3.7 using rbenv. Once that was installed and set as my default ruby, I could install Command-T like normal. Might still need to consider reinstalling Mojave at a later date, but installing Ruby 2.3.7 via rbenv helped me get things running without doing that now. I hope this helps someone else in this situation. – Kyle Tolle Nov 14 '18 at 22:30
  • ReDetection solution worked and i also used Systems Ruby. I mean thats what the download is for right? Headers for Mac OS X. – Logemann Nov 20 '18 at 11:40
  • In my case /Library/Developer/CommandLineTools doesn't contain Packages folder. As I wrote above reinstall Mojave was a solution (You can reinstall it without losing data) – maksimr Nov 20 '18 at 13:42
0

I'm using rbenv (on Catalina) and none of the solutions I found here (or elsewhere) worked for me. Ultimately what fixed my environment was pointing rbenv global to a specific version of ruby instead of system:

$ rbenv global 2.6.3

This was the state of my env while gem install was failing:

$ gem env home
=> /Users/ryanc/.gem

$ which gem
=> /Users/ryanc/.rbenv/shims/gem

$ which ruby
=> /Users/ryanc/.rbenv/shims/ruby

$ ruby -v
=> ruby 2.6.3p62 (2019-04-16 revision 67580) [universal.x86_64-darwin19]

$ rbenv local
=> rbenv: no local version configured for this directory

$ rbenv global
=> system

$ rbenv which ruby
=> /usr/bin/ruby

The result of rbenv which ruby was the key. I decided to run rbenv global 2.6.3 and suddenly I could install gems again. Here's the new state of things:

$ ruby -v
=> ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-darwin19]

$ rbenv global
=> 2.6.3

$ rbenv which ruby
=> /Users/ryanc/.rbenv/versions/2.6.3/bin/ruby
ryanc
  • 187
  • 1
  • 7
0

I had a similar problem installing eventmachine 1.2.7, this worked for me (macOS 10.15.5):

(I use fish)

  1. brew install ruby
  2. Add the following ~/.config/fish/config.fish (without fish, just add these to path)
    1. fish_add_path /usr/local/opt/ruby/bin
    2. fish_add_path /usr/local/lib/ruby/gems/3.0.0/bin
  3. set -gx CPPFLAGS "-I/usr/local/opt/ruby/include"
  4. set -gx LDFLAGS "-L/usr/local/opt/ruby/lib"
  5. set -gx PKG_CONFIG_PATH "/usr/local/opt/ruby/lib/pkgconfig"
  6. sudo gem install eventmachine -v '1.2.7' --source 'https://rubygems.org/'

The set -gx commands I got after installing ruby thru brew.

I tried reinstalling the Xcode tools (12.3 as I can't upgrade to Big Sur just yet) and re-accepting agreement, but that didn't change anything


One note is that this was from installing eventmachine 1.2.7. The log error I got referenced this path as one of its flags:

-I/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 -I

Note that it was trying to look in .../MacOSX11.1.sdk/..., but I have 10.15.5. Even setting CPPFLAGS, LDFLAGS, PKG_CONFIG_PATH manually did not work as I was using either system ruby or whatever rbenv had, but I was too frustrated to dig deeper down that particular rabbit hole. So, I reinstalled w/ brew's ruby!

Graystripe
  • 188
  • 7
  • 9
0

This is not cocoa pod issue, It is related to the older version of ruby. Simply follow the below steps :

  1. rvm install "ruby-3.0.0"
  2. sudo gem install cocoapods
0

I ran into this after uninstalling an older version of Xcode. Typically, I install into /Applications/Xcode-13.2.1.app for example. However, xcode-select-p still showed it was pointing to an older version.

I just ran sudo xcode-select -s /Applications/Xcode-13.2.1.app and everything started working correctly again.

DustinB
  • 10,751
  • 5
  • 44
  • 54
0

If you're getting this error as part of bundle install, see below -

I couldn't get any of the posted solutions to work. What worked for me was installing the specified bundler version from the logs. For example, my Console was warning me about mismatching bundler version and I ran gem install bundler:2.2.32 to fix it which then fixed the bundle install command for me. In my case, i was using rvm and ruby version 3.0.0.

newDeveloper
  • 1,355
  • 1
  • 17
  • 27
-1

In my case (Catalina 10.15.7) simply re-installying ruby with homebrew fixed it. No need to install rvm or rubyenv.

Jeremy
  • 228
  • 3
  • 12
  • Note: Homebrew ruby is a "different" one than the system ruby, and pointing to the 'right' ruby for other downstream build process can be tricky. For example, homebrew refuses to link macOS provided ruby, so you'll need to configure PATH, LDFLAGS, CPPFLAGS, and pkgconfig properly, which often goes like a mess and does not work as smooth as expected. – Jongwook Choi Feb 09 '21 at 19:20
-1

it's about your ruby version or bad installation. you can install new ruby version with rvm

first install rvm with bellow command

curl -L https://get.rvm.io | bash -s stable

then close and reopen terminal or use command source [you mac bash file like .zshrc or .bashrrc] to load rvm command

next install new ruby version with bellow command

rvm install ruby-2.7.2

now you should use new version of ruby and install last version of cocoapods do this steps:

  • check ruby version
  • check ruby version with ruby -v command
  • if your ruby version not 2.7.2 use 'rvm use 2.7.2' command to switch on new ruby
  • then install cocoapods with 'sudo gem install cocoapods' command

for check new pods version use 'gem which cocoapods' command.

adel parsa
  • 59
  • 3
-1

I'm encountered with the same problem a moment ago, and the following works for me.

brew install rbenv
rbenv install 3.1.1 && rbenv global 3.1.1

Do as installation tips of the previous step: vim ~/.zhrc and append eval "$(rbenv init - zsh)"; finally, restart your term to make the line take effect.

I prefers not to make change under /Applications/Xcode.app since everything done will be lost after upgrading Xcode.

By the way, rbenv install --list tells all stable versions of ruby, and I choose the latest. You could try earlier ruby version that works for you.

DawnSong
  • 3,934
  • 2
  • 32
  • 37