CocoaPods在M1芯片(Apple Silicon)上失败,显示ffi错误的架构。

86

在 M1 MacBook 上运行 'pod install' 失败了,因为出现了 ffi 问题,如 这里所述。

我尝试了一些解决方法(我想我按不同的顺序都试过了),但现在我得到了一个稍微不同的错误:

LoadError - dlopen(/opt/homebrew/lib/ruby/gems/3.0.0/gems/ffi-1.15.0/lib/ffi_c.bundle, 9): no suitable image found.  Did find:
    /opt/homebrew/lib/ruby/gems/3.0.0/gems/ffi-1.15.0/lib/ffi_c.bundle: mach-o, but wrong architecture
    /opt/homebrew/lib/ruby/gems/3.0.0/gems/ffi-1.15.0/lib/ffi_c.bundle: mach-o, but wrong architecture - /opt/homebrew/lib/ruby/gems/3.0.0/gems/ffi-1.15.0/lib/ffi_c.bundle

看起来我现在有了ffi,但是架构不对?我该怎么办?无论是否在Rosetta模式下运行终端都会发生这种情况。

顺便说一句,其中一个提出的解决方法对我没有成功。当我尝试:

sudo arch -x86_64 gem install ffi

我理解为:

arch: posix_spawnp: gem: Bad CPU type in executable

不确定这是否相关。

12个回答

139

回答自己的问题。我通过卸载有问题的Ruby版本和CocoaPods,然后使用-x86_64架构重新安装ffi和CocoaPods来解决ffi问题。

以下是我采取的步骤以恢复工作状态(并应用M1解决方案来处理ffi):

  1. 使用以下命令卸载Ruby:brew uninstall ruby --force

  2. 卸载CocoaPods。首先尝试使用 gem list --local | grep cocoapods 列出所有的CocoaPods版本/组件。然后逐个卸载它们,例如:

sudo gem uninstall cocoapods

sudo gem uninstall cocoapods-core

sudo gem uninstall cocoapods-downloader

等等。

  1. 接下来,我可以使用以下命令重新安装ffi,然后安装CocoaPods:

sudo arch -x86_64 gem install ffi

sudo arch -x86_64 gem install cocoapods

完成后,我可以按预期运行pod install。请注意,我一直在标准模式下打开终端-无需Rosetta。


谢谢,问题完美解决。非常感激! - Jason Etheridge
需要使用 brew uninstall --ignore-dependencies ruby 而不是 brew uninstall ruby --force。 - shankar upadhyay
它对我的 M1 没有帮助。 - Syed Amir Ali
这对我有用!注意:您不应更改-x86_64的arch参数为-arm64(就像我所做的那样),以使其在您的Apple Silicon机器上正常工作。 - Wouter van Koppen
1
在浏览了这么多线程之后,最终这一步解决了我的问题:brew uninstall ruby --force。谢谢! - Josef Büttgen
显示剩余7条评论

44

如Datasun所说,我删除了CocoaPods

gem list --local | grep cocoapods | awk '{print $1}' | xargs sudo gem uninstall

然后运行:

brew remove rbenv
sudo rm -rf ~/.rbenv
sudo arch -x86_64 gem install ffi
sudo arch -x86_64 gem install cocoapods
brew install rbenv
rbenv install 3.0.1
rbenv global 3.0.1

pod install

没有任何问题。


这太棒了,感谢您让复制和粘贴变得更容易。 - Frankely Diaz
1
对于那些尝试上述操作并收到“无podfile”错误提示的人,为了后世纪录,我运行了上述命令,但不包括“pod install”,接着运行了“bundle install”,现在一切都很流畅。 - kykyi
3
这个命令仍然无法运行:sudo arch -x86_64 gem install ffi。还有其他命令可以尝试吗? - razorsyntax
你救了我的一天,但我使用的是搭载 M1 芯片的 Mac,在安装 rbenv 3.0.1 之前,我按照 https://dev59.com/zVEG5IYBdhLWcg3wbtZr 中的步骤进行操作。 - Kiên Trần Trung
这个解决方案对我没有帮助,我正在使用M1。 - Syed Amir Ali
显示剩余3条评论

31

我同意Datasun的回答。但我也成功地按照大致相同的步骤进行了操作,得到了一个我认为更好或者同样好的功能性结果。我在终端中运行了以下命令:

brew uninstall --ignore-dependencies ruby

然后

sudo gem uninstall cocoapods
sudo gem uninstall cocoapods-core
sudo gem uninstall cocoapods-downloader

之后我想要用Homebrew来安装CocoaPods,所以我使用了:

brew install cocoapods

(您可能需要重新安装)


1
谢谢,这是唯一对我有效的解决方案。 - Mohammed Riyadh
1
是的,通过brew重新安装,而不是gem! - khandaniel
1
非常感谢,伙计!这是唯一对我有效的解决方案。 - Flux
使用Homebrew安装CocoaPods似乎是我所缺失的那一部分!干杯! - saricden

18

我尝试了几乎所有方法,但这是唯一对我有效的解决方案:

通过 gem 卸载 cocoapods 包

sudo gem uninstall cocoapods

使用Homebrew重新安装它

brew install cocoapods

问题已得到解决。

问题出在我通过gem install而非homebrew安装了cocoapods。


12

我通过首先清理所有先前安装的Pod,使其工作:

pod deintegrate

然后使用以下命令重新安装它们:

arch -x86_64 pod install

2
这在我的 Mac M1 上有效。 - Umair A.
你的解决方案在Mac M1和iPhone X ios 16.2上运行良好。 - Mankeomorakort

7
我在我的新 MacBook 上使用 M1 芯片设置 Capacitor 项目时遇到了这个错误。然而,如果你想要使用 Capacitor、Flutter 或 React Native,你都可能会遇到这个错误,因为这是一个与框架无关的与计算机相关的问题。
你的电脑必须安装 Ruby 和 cocoapods。在我的情况下,逐个运行以下命令可以解决问题。
1.
brew install ruby
echo 'export PATH="/opt/homebrew/opt/ruby/bin:$PATH"' >> ~/.zshrc
brew install cocoapods

现在,您已准备就绪。

pod install

你可能需要知道 pod 的路径。echo 'export PATH="/opt/homebrew/opt/ruby/bin:/opt/homebrew/lib/ruby/gems/3.1.0/bin/:$PATH"' >> ~/.zshrc。Ruby 版本可能会有所不同。 - hiroshi

6

当我尝试使用x86_64时:

sudo arch -x86_64 gem install ffi

linking shared-object ffi_c.bundle
ld: warning: ignoring file /Users/aa/.rbenv/versions/3.0.1/lib/libruby.3.0.dylib, building for macOS-x86_64 but attempting to link with file built for macOS-arm64
Undefined symbols for architecture x86_64:
  "_rb_ary_detransient", referenced from:
      _memory_put_array_of_int8 in AbstractMemory.o

所以我尝试了:

sudo arch -arm64e gem install ffi

sudo arch -arm64e gem install cocoapods

然后pod install就可以工作了!

我还需要运行rbenv init并将评估放置在~/.zshrc中才能让正确的Ruby版本工作。


我认为这是最好的解决方案。其他所有的解决方案都迫使我最终不得不再次使用 arch -x86_64 pod install 而不是 pod install - Jared

5

我同意Datasun的回答。但目前我们可以在M1 Macbook上使用arm64代替x86_64

我重新安装了ruby并在终端运行了以下命令:

gem uninstall cocoapods
gem uninstall cocoapods-core
gem uninstall cocoapods-downloader

gem uninstall ffi
gem install ffi

这对我有用:
  1. sudo arch -arm64e gem install cocoapods
  2. sudo arch -arm64e gem install ffi
- Corey Gwin
我尝试了所有的方法,只有这个有效,谢谢@nathanwhy! - Quentin Rth

4

我只需要运行以下命令进行pod安装,不需要卸载ruby或cocoapod。

arch -x86_64 pod install


确实,只需键入:arch -x86_64 pod install - Medhi

2

对我而言,在遵循 @Datasun 的步骤之后,我还需要做这些额外的事情:

brew install ruby

并且

echo 'export PATH="/opt/homebrew/opt/ruby/bin:$PATH"' >> ~/.zshrc

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接