Error installing cocoapods:ERROR: Failed to build gem native extension.

一、问题描述

在终端用命令sudo gem install cocoapods更新cocoapods时,出现了如下错误:

jundeMacBook-Pro-4:~ junwen$ sudo gem install cocoapods
Password:
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 ./siteconf20200507-595-1huhzcq.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

二、定位原因

查看报错信息,个人猜测是Ruby的版本太低导致的。
我的报错的环境是:Mac OS Mojave 10.14ruby 2.3.7
而我同事的cocoapods版本是1.9.1,对应的ruby 2.6.3
Mac OS系统是Mac OS Catalina 10.15.4
对比下环境差异,如果能把ruby升到2.6.X以上版本,这个问题就解决了。

三、解决方案

1、升级Mac OS系统到:Mac OS Catalina 10.15.4
2、将Xcode更新到最新版本,我的版本是Version 11.4.1 (11E503a)
3、安装最新的cocoapods:sudo gem install cocoapods

推荐阅读更多精彩内容