首页 > 解决方案 > Errors when installing cocoapods with gem

问题描述

I am using macOS Mojave and when I try to install cocoapods using gem I am getting the following error.

Building native extensions. This could take a while... ERROR: Error installing cocoapods: ERROR: Failed to build gem native extension.

current directory: /Library/Ruby/Gems/2.3.0/gems/ffi-1.12.2/ext/ffi_c

/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/bin/ruby -r ./siteconf20200302-707-1iveybq.rb extconf.rb mkmf.rb can't find header files for ruby at /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/include/ruby.h

extconf failed, exit code 1

Gem files will remain installed in /Library/Ruby/Gems/2.3.0/gems/ffi-1.12.2 for inspection. Results logged to /Library/Ruby/Gems/2.3.0/extensions/universal-darwin-18/2.3.0/ffi-1.12.2/gem_make.out

This error comes when I run the following commands.

sudo gem install cocoapods

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

I have already installed xcode command line tools and I get the following message when I run xcode-select --install

xcode-select: error: command line tools are already installed, use "Software Update" to install updates

I have also agreed to xcode license agreements with the command sudo xcodebuild -license

The error log in /Library/Ruby/Gems/2.3.0/extensions/universal-darwin-18/2.3.0/ffi-1.12.2/gem_make.out gives the following

current directory: /Library/Ruby/Gems/2.3.0/gems/ffi-1.12.2/ext/ffi_c /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/bin/ruby -r ./siteco$ mkmf.rb can't find header files for ruby at /System/Library/Frameworks/Ruby.fra$

extconf failed, exit code 1

What do I do to fix this issue ?

标签: iosxcodemacosrubygems

解决方案


这个问题的答案可以在这里找到,尽管它与这个问题无关。以下是效果最好的答案片段。

对于macOS 10.14 上的 Xcode 11,即使在安装 Xcode 并安装命令行工具并接受许可后,也可能发生这种情况

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

问题是 Xcode 11 附带了 macOS 10.15 SDK,其中包含 ruby​​2.6 的标头,但不包含 macOS 10.14 的 ruby​​2.3 的标头。您可以通过运行来验证这是您的问题

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

在带有 Xcode 11 的 macOS 10.14 上打印不存在的路径

/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 在/Library/Developer/CommandLineTools/SDKs/MacOS10.14.sdk. 没有必要按照其他答案中的建议通过安装旧的头文件来污染系统目录。相反,通过选择该 SDK,将找到适当的 ruby​​2.3 标头:

sudo xcode-select --switch /Library/Developer/CommandLineTools
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应该在选择该 SDK 时工作。

要切换回使用当前的 Xcode 11 SDK,请使用

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

推荐阅读