Xcode 11无法成功安装Cocoapods的原因和解决方案: mkmf.rb can't find header files for ruby at xxx

我的系统是mac,然后今天在安装eventmachine的时候发现的报了错误,安装命令为:

sudo gem install cocoapods:

错误信息为

 参考地址:https://blog.csdn.net/u013538542/article/details/104660924

我主要是新买的macbook pro,安装了XCode 11,然后就遇到这个安装Cocoapods的错误,之前从来没有遇到过

后来经过了各种搜索后,解决思路和办法如下:

重点

macOS 10.14 上安装了Xcode 11,也需要安装了Xcode command line tools

$ sudo xcode-select --install

增加了一个接受证书的环节

$ sudo xcodebuild -license accept

这个问题主要是因为Xcode 11 携带了macOS 10.15 SDK,该SDK包含了ruby 2.6的头文件,但是对macOS 10.14系统的ruby 2.3却没有该文件,所以你可以通过一下命令来验证问题

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

这个命令在macOS 10.14系统上,Xcode 11版本安装的情况下会打印出这个不存在的路径

/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

然后,Xcode 11是安装在macOS 10.14 SDK上,在此路径/Library/Developer/CommandLineTools/SDKs/MacOS10.14.sdk 。但是它没有必要因为安装了旧的头文件而污染了系统目录。所以我们要改成,指定合适的SDK和ruby 2.3头文件

sudo xcode-select --switch /Library/Developer/CommandLineTools

然后,我们再来看下ruby 2.3的正确路径

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

这次就会输出一个正常的存在的路径

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

所以,现在,gem install就可以正常使用了

现在可以愉快的安装cocoapods吧

$ gem install cocoapods

安装完后,检查下版本

$ pod --version

输出

1.9.0

注意:

  在cocoapods 执行 sudo gem install cocoapods   的时候出现  While executing gem ... (Gem::FilePermissionError) You don't have write permissions for the /usr/bin directory.

改为 sudo gem install -n /usr/local/bin cocoapods  即可

注意:
如果想改回之前使用的XCode 11 SDK,就使用此命令

$ sudo xcode-select --switch /Applications/Xcode.app

必要的情况下,导入以下两个环境变量,写入到~/.zshrc文件里

export GEM_HOME=$HOME/.gem
export PATH=$GEM_HOME/bin:$PATH

原文地址:https://www.cnblogs.com/huihuizhang/p/12802689.html