macOS Ventura 13.0环境下安装Flutter CocoaPods失败求助
问题描述
在搭载macOS Ventura(13.0)的Intel MacBook Pro上进行Flutter iOS开发时,执行命令sudo gem install cocoapods安装CocoaPods失败,错误提示无法构建gem原生扩展,要求先安装开发工具。具体错误日志如下:
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.6.0/gems/ffi-1.15.5/ext/ffi_c /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/bin/ruby -I /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0 -r ./siteconf20221026-9237-4mv2z6.rb extconf.rb checking for ffi.h... *** extconf.rb failed *** Could not create Makefile due to some reason, probably lack of necessary libraries and/or headers. Check the mkmf.log file for more details. You may need configuration options. Provided configuration options: --with-opt-dir --without-opt-dir --with-opt-include --without-opt-include=${opt-dir}/include --with-opt-lib --without-opt-lib=${opt-dir}/lib --with-make-prog --without-make-prog --srcdir=. --curdir --ruby=/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/bin/$(RUBY_BASE_NAME) --with-ffi_c-dir --without-ffi_c-dir --with-ffi_c-include --without-ffi_c-include=${ffi_c-dir}/include --with-ffi_c-lib --without-ffi_c-lib=${ffi_c-dir}/lib --enable-system-libffi --disable-system-libffi --with-libffi-config --without-libffi-config --with-pkg-config --without-pkg-config /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/mkmf.rb:467:in `try_do': The compiler failed to generate an executable file. (RuntimeError) You have to install development tools first. from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/mkmf.rb:585:in `block in try_compile' from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/mkmf.rb:534:in `with_werror' from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/mkmf.rb:585:in `try_compile' from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/mkmf.rb:1109:in `block in have_header' from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/mkmf.rb:959:in `block in checking_for' from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/mkmf.rb:361:in `block (2 levels) in postpone' from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/mkmf.rb:331:in `open' from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/mkmf.rb:361:in `block in postpone' from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/mkmf.rb:331:in `open' from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/mkmf.rb:357:in `postpone' from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/mkmf.rb:958:in `checking_for' from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/mkmf.rb:1108:in `have_header' from extconf.rb:10:in `system_libffi_usable?' from extconf.rb:42:in `<main>' To see why this extension failed to compile, please check the mkmf.log which can be found here: /Library/Ruby/Gems/2.6.0/extensions/universal-darwin-22/2.6.0/ffi-1.15.5/mkmf.log extconf failed, exit code 1 Gem files will remain installed in /Library/Ruby/Gems/2.6.0/gems/ffi-1.15.5 for inspection. Results logged to /Library/Ruby/Gems/2.6.0/extensions/universal-darwin-22/2.6.0/ffi-1.15.5/gem_make.out
需确认:
- 该问题是否由macOS Ventura(13.0)系统版本导致?
- 如何解决此问题以顺利安装CocoaPods?
解决方案
是否由系统版本导致?
是的,macOS Ventura 13.0与系统默认的Ruby 2.6版本存在兼容性问题,构建gem原生扩展时会因缺少适配的开发工具或库文件触发错误。此外,Ventura对系统目录的权限管控更严格,直接使用sudo gem install修改系统Ruby环境也容易引发权限或依赖问题。
具体解决步骤
方法1:安装Xcode Command Line Tools(优先推荐)
错误日志明确提示需安装开发工具,执行以下命令:
xcode-select --install
安装完成后,重新执行CocoaPods安装命令:
sudo gem install cocoapods
若仍报错,尝试重启终端后再执行。
方法2:使用Homebrew管理Ruby与CocoaPods
修改系统Ruby易引发问题,更稳妥的方式是通过Homebrew安装独立Ruby环境:
- 安装Homebrew(未安装时执行):
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
- 安装Ruby:
brew install ruby
- 配置环境变量,让终端优先使用Homebrew安装的Ruby:
- Bash用户编辑
~/.bash_profile或~/.bashrc,添加:echo 'export PATH="/usr/local/opt/ruby/bin:$PATH"' >> ~/.bash_profile source ~/.bash_profile - Zsh用户编辑
~/.zshrc,添加:echo 'export PATH="/usr/local/opt/ruby/bin:$PATH"' >> ~/.zshrc source ~/.zshrc
- Bash用户编辑
- 安装CocoaPods(无需sudo):
gem install cocoapods
方法3:指定兼容的ffi版本
错误中提到的ffi-1.15.5与Ventura兼容性不佳,可先安装低版本ffi再装CocoaPods:
sudo gem install ffi --version 1.14.2 sudo gem install cocoapods
内容的提问来源于stack exchange,提问作者hdhmergo
相关产品推荐
相关产品推荐

