You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

M1芯片MacBook Air无法安装CocoaPods,执行sudo gem install ffi报错

M1芯片Mac安装CocoaPods时ffi安装报错解决方案

报错详情

运行sudo gem install ffi命令时出现如下报错:

ERROR:  Loading command: install (LoadError)
dlopen(/Library/Ruby/Gems/2.6.0/gems/date-3.1.1/lib/date_core.bundle, 9): no suitable image found.  Did find:
/Library/Ruby/Gems/2.6.0/gems/date-3.1.1/lib/date_core.bundle: mach-o, but wrong architecture
/Library/Ruby/Gems/2.6.0/gems/date-3.1.1/lib/date_core.bundle: mach-o, but wrong architecture - /Library/Ruby/Gems/2.6.0/gems/date-3.1.1/lib/date_core.bundle
ERROR:  While executing gem ... (NoMethodError)
undefined method `invoke_with_build_args' for nil:NilClass

错误原因

该报错为M1系列Mac设备特有的架构兼容问题:系统预置的Ruby 2.6.0版本仅适配x86架构,在arm64架构的M1芯片上运行gem相关操作时会出现架构不匹配,导致依赖库加载失败。

解决方案

方案1:临时使用Rosetta模式运行命令(快速解决)

  • 右键点击Dock上的「终端」图标,选择「显示简介」,勾选「使用Rosetta打开」
  • 完全退出终端后重新打开,执行以下命令安装ffi:
    sudo arch -x86_64 gem install ffi
    
  • 完成后执行CocoaPods安装命令即可:
    sudo gem install cocoapods
    

方案2:使用Homebrew安装新版Ruby(长期稳定,推荐)

避免修改系统自带Ruby环境,减少后续架构兼容问题

  • 执行命令安装Ruby:
    brew install ruby
    
  • 配置环境变量,zsh用户执行:
    echo 'export PATH="/opt/homebrew/opt/ruby/bin:$PATH"' >> ~/.zshrc
    source ~/.zshrc
    
    bash用户执行:
    echo 'export PATH="/opt/homebrew/opt/ruby/bin:$PATH"' >> ~/.bash_profile
    source ~/.bash_profile
    
  • 验证Ruby路径,执行以下命令确认输出为/opt/homebrew/opt/ruby/bin/ruby,而非系统路径/usr/bin/ruby:
    which ruby
    
  • 无需sudo直接执行安装命令即可:
    gem install ffi
    gem install cocoapods
    

内容的提问来源于stack exchange,提问作者Hamada Raouf

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.14 07:06:04