80

I updated XCode on my Mac and since then when starting Docker using docker-sync-stack start I get this error message:

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

I tried installing ruby with this: brew install rbenv ruby-build but this does not change anything.

Does anybody know how I can fix it?

Thanks!

Torben
  • 5,388
  • 12
  • 46
  • 78
  • 2
    I figured it out - after upgrading to Xcode 9 you must install the Command Line Tools for Xcode 9 as well. – Torben Sep 23 '17 at 09:06
  • 9
    Hit this issue when upgrading to Xcode 11 GM. Anyone found a solution? The `sudo xcode-select --install` command says I already have Command-Line Tools installed. Also tried manually downloading Xcode 11 Command-Line Tools from Developer Downloads. But I still get the 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` – jdelaune Sep 13 '19 at 09:39

11 Answers11

193

For Xcode 11 on macOS 10.14, this can happen even after installing Xcode and installing command-line tools and accepting the license with

sudo xcode-select --install
sudo xcodebuild -license accept

The issue is that Xcode 11 ships the macOS 10.15 SDK which includes headers for ruby2.6, but not for macOS 10.14's ruby2.3. You can verify that this is your problem by running

ruby -rrbconfig -e 'puts RbConfig::CONFIG["rubyhdrdir"]'

which on macOS 10.14 with Xcode 11 prints the non-existent path

/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/include/ruby-2.3.0

However, Xcode 11 installs a macOS 10.14 SDK within /Library/Developer/CommandLineTools/SDKs/MacOS10.14.sdk. It isn't necessary to pollute the system directories by installing the old header files as suggested in other answers. Instead, by selecting that SDK, the appropriate ruby2.3 headers will be found:

sudo xcode-select --switch /Library/Developer/CommandLineTools
ruby -rrbconfig -e 'puts RbConfig::CONFIG["rubyhdrdir"]'

This should now correctly print

/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/include/ruby-2.3.0

Likewise, gem install should work while that SDK is selected.

To switch back to using the current Xcode 11 SDK, use

sudo xcode-select --switch /Applications/Xcode.app
joki
  • 6,619
  • 2
  • 22
  • 30
  • after export, it fixed the `ruby -rrbconfig -e 'puts RbConfig::CONFIG["rubyhdrdir"]'` but when I `gem install`, it still prints the wrong dir for file not found error – Mzq Oct 25 '19 at 01:49
  • @Miranda can you verify that there's a `ruby.h` file in that path? Are you getting the exact same error message as in the question? What do `xcodebuild -version` and `xcrun --show-sdk-version` print? – joki Oct 26 '19 at 13:16
  • 3
    `$ xcrun --show-sdk-version` give `xcodebuild: error: SDK "/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/" cannot be located. xcrun: error: unable to lookup item 'SDKVersion' in SDK '/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/'`, I end up using @bdorfman 's solution of `rbenv` – Mzq Oct 28 '19 at 02:39
  • @Miranda thank you for your report, I was able to reproduce the problem while `/Applications/Xcode.app` is selected using `xcode-select`. Selecting the command-line tools SDK instead worked for me (setting the variable is no longer required). – joki Nov 05 '19 at 10:56
  • I searched for a couple of hours before finding this post, thank you. – zenchemical Mar 07 '20 at 01:40
  • 1
    Accepting the license helped me install CocoaPods as well. Thank you. – andrewlundy Mar 13 '20 at 20:59
  • @Miranda run `xcode-select --install` to get Command Line Tools installed first. Then `sudo xcode-select --switch /Library/Developer/CommandLineTools`. That will resolve this error. (Shared for future Googlers.) – podcastfan88 Jun 26 '20 at 01:35
  • Worked too on MacOS 10.15.7 and Xcode 11.7, I just had to run `sudo xcode-select --switch /Applications/Xcode.app`, because `ruby -rrbconfig` was printing the right path – Ricardo Isidro Feb 08 '21 at 16:16
  • I was having the same issue with Xcode 12.2 and because I named my Xcode 12.2 instead of the default Xcode, following these steps resolved my issue. Thank you. – kelvin Aug 25 '21 at 10:14
137

None of the other solutions worked for me, here's what I ran to resolve the issue on Mac OS 10.14.x:

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
ryanpcmcquen
  • 6,285
  • 3
  • 24
  • 37
36

When upgrading XCode you need to install the Command-Line Tools and additionally accept the terms and conditions:

sudo xcode-select --install

Then:

sudo xcodebuild -license
tadman
  • 208,517
  • 23
  • 234
  • 262
  • 6
    The first command was enough to get docker-sync-stack stop complaining. Actually the second command failed with "xcode-select: error: tool 'xcodebuild' requires Xcode, but active developer directory '/Library/Developer/CommandLineTools' is a command line tools instance" – Motin Dec 10 '17 at 17:42
  • 1
    As @Motin said, it seems like the second command doesn't work, but thankfully it wasn't necessary to get `docker-sync start` to stop complaining. – Joseph Siefers Oct 05 '18 at 16:28
  • This wasn't enough for me, unfortunately. – ryanpcmcquen Oct 09 '19 at 13:59
20

As of Xcode 11, it seems like the ruby development headers are no longer included, so you will need to manually install them outside of Xcode Command Line Tools.

E.g. for me using rbenv i did

rbenv install 2.6.4
rbenv global 2.6.4
eval "$(rbenv init -)"

Then ran bundle install and everything worked as expected.

bdorfman
  • 201
  • 2
  • 3
  • 1
    Most importantly this worked for me. I'd also like to also note by adding `rbenv` in my CI workflow I was able to make great improvements (no need to install bundler using sudo anymore!) Thanks a lot @bdorfman – Tumata Sep 20 '19 at 20:59
  • 1
    This doesn't work for me for some reason. I even tried replacing "2.6.4" with "2.3.0". When I look, there is no `/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/include` directory though the path up to "include" does exist. – Darrell Brogdon Sep 22 '19 at 03:31
  • thanks but I had to install the gem again, since there is nothing to bundle for docker-sync. The answer from @fnordl worked for me. – nils petersohn Sep 23 '19 at 15:10
  • @DarrellBrogdon Your environment probably isnt set up right. If its working correctly mkmf wont be looking for 2.3 any more itll be looking for whichever version you set. If you're still getting an error with the path `/System/Library/Frameworks/Ruby.framework/Versions/2.3` it indicates that your env is still using the macOS system ruby and not rbenv's. – bdorfman Sep 23 '19 at 18:10
  • This also solved my Docker x Ruby issues – Julius Naeumann Mar 14 '23 at 15:56
16

For Xcode 11 I fixed this by also installing of /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg package.

Sergey Tykhonov
  • 161
  • 1
  • 3
  • 1
    Hey, I don't have this file. Where can I find it ? – nils petersohn Sep 23 '19 at 13:09
  • Verify the pkg exists at the path Sergey provided. Check the path from root. Not in your users path. If is not there you can attempt to first install the command line tools: `xcode-select --install` and it should show up. Then, once you have the package at that path force the install like so `sudo installer -pkg macOS_SDK_headers_for_macOS_10.14.pkg -target /` (executing from /Library/Developer/CommandLineTools/Packages) – daveferrara1 Sep 25 '19 at 23:18
16

Single line command fix:

On OSX Mojave I ran the following command:

sudo installer -pkg /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg -target /

and it fixed the issue.

SudoPlz
  • 20,996
  • 12
  • 82
  • 123
  • 1
    This fixed the problem for me. – Ruben Marin Oct 03 '19 at 07:21
  • 2
    Didn't work for me. I am prompted to add `-allowUntrusted` flag. But then it still failed at the end. The error message is `installer: The install failed (The Installer encountered an error that caused the installation to fail. Contact the software manufacturer for assistance.)` – Anthony Kong Nov 05 '19 at 03:45
  • 1
    after trying every thing, you saved my life man thanks – Mohammed Riyadh Jul 31 '21 at 14:52
7

I had the same problem while installing fastlane. The tadman's answer is correct, but the command lines don't work for me with Xcode 10.2.1 (I'm not sure with other versions). You need to open Xcode, then open Preferences, select Locations and choose Command Line Tools. In my case, Command Line Tools field is empty.

Duan Nguyen
  • 468
  • 5
  • 10
7

As @bdorfman said in his comment, the Ruby headers are no longer included as of Xcode 11 build but they seem to try to make a new build for this as soon as possible (?): https://discuss.circleci.com/t/xcode-11-gm-seed-2-released/32505/12.

What worked for me was to use rbenv like @bdorfman said:

rbenv install 2.6.4
rbenv global 2.6.4
eval "$(rbenv init -)"

But with the extra step of adding docker-sync to your new ruby version. Since after you change the ruby version (for example)

$ ruby -v
ruby 2.6.4p104 (2019-08-28 revision 67798) [x86_64-darwin18]

And then running gem list:

$ gem list
bigdecimal (default: 1.4.1)
bundler (default: 1.17.2)
cmath (default: 1.0.0)
csv (default: 3.0.9)
date (default: 2.0.0)
dbm (default: 1.0.0)
did_you_mean (1.3.0)
e2mmap (default: 0.1.0)
etc (default: 1.0.1)
fcntl (default: 1.0.0)
fiddle (default: 1.0.0)
fileutils (default: 1.1.0)
forwardable (default: 1.2.0)
io-console (default: 0.4.7)
ipaddr (default: 1.2.2)
irb (default: 1.0.0)
json (default: 2.1.0)
logger (default: 1.3.0)
matrix (default: 0.1.0)
minitest (5.11.3)
mutex_m (default: 0.1.0)
net-telnet (0.2.0)
openssl (default: 2.1.2)
ostruct (default: 0.1.0)
power_assert (1.1.3)
prime (default: 0.1.0)
psych (default: 3.1.0)
rake (12.3.2)
rdoc (default: 6.1.2)
rexml (default: 3.1.9)
rss (default: 0.2.7)
scanf (default: 1.0.0)
sdbm (default: 1.0.0)
shell (default: 0.7)
stringio (default: 0.0.2)
strscan (default: 1.0.0)
sync (default: 0.5.0)
test-unit (3.2.9)
thwait (default: 0.1.0)
tracer (default: 0.1.0)
webrick (default: 1.4.2)
xmlrpc (0.3.0)
zlib (default: 1.0.0)

(does not contains docker-sync)

If you run the gem install for docker-sync you will use the docker-sync for your rbenv managed ruby.

$ gem install docker-sync

fnordl
  • 71
  • 1
  • 4
3

If you want to get a deeper insight into the issue:

Mac OS X 10.14 has stopped placing the include libraries in their usual location, /usr/include, keeping them only inside the XCode directory.

To confirm that this is the problem, simply run ls /usr/include. If the result comes out empty, its really this problem.

Apple ships a “legacy installer” for you to be able to install the headers in the “old location”. To fix the problem just run this:

sudo installer -pkg /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg -target /

https://silvae86.github.io/sysadmin/mac/osx/mojave/beta/libxml2/2018/07/05/fixing-missing-headers-for-homebrew-in-mac-osx-mojave/

Dmytro Hutsuliak
  • 1,741
  • 4
  • 21
  • 37
2

Running open /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg in the terminal opens the package installation wizard. After properly installing was I able to proceed.

MacOS 10.4.4

LordKiz
  • 603
  • 6
  • 15
1

Open Terminal

xcode-select --install
sudo xcodebuild -license accept
curl -L https://get.rvm.io | bash -s stable

Close and Reopen Terminal

rvm install ruby-2.6

sudo gem install cocoapods
pod install 

After completing right click on project top-left on android studio then click on Flutter -> Open IOS Module in Xcode

Ahmed Raza
  • 416
  • 6
  • 10