m1 silicon macos 安装 cocoapods

视频

https://www.bilibili.com/video/BV1AA411X7o3

前言

大家在 m1 silicon macos 下开发 flutter ios 问题一直很多,今天就来讲下 cocoapods 的安装问题。

特别是安装了新系统 Ventura 后直接安装 cocoapods 会报错。

问题分析

❯ ruby -v
ruby 2.6.10p210 (2022-04-12 revision 67958) [universal.x86_64-darwin22]

是 macos 系统化自带的 ruby 版本太低,导致 cocoapods 安装失败。

默认 ruby 是 v2 版本,直接 install cocoapods 会报错的。

步骤

  • 安装或者升级 Homebrew 包管理工具
  • 安装全新的 Ruby 新版本
  • 设置你的系统配置 ~/.bash_profile 或者 ~/.zshrc 文件
  • 安装 cocopod

正文

1. 安装升级 Homebrew 包管理工具

# 安装
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

# 升级
brew upgrade

2. 安装 ruby

brew install ruby

输出

==> Summary
/usr/local/Cellar/ruby/3.1.3: 16,149 files, 41.6MB
==> Running `brew cleanup ruby`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
==> Caveats
==> ruby
By default, binaries installed by gem will be placed into:
  /usr/local/lib/ruby/gems/3.1.0/bin

You may want to add this to your PATH.

ruby is keg-only, which means it was not symlinked into /usr/local,
because macOS already provides this software and installing another version in
parallel can cause all kinds of trouble.

If you need to have ruby first in your PATH, run:
  echo 'export PATH="/usr/local/opt/ruby/bin:$PATH"' >> ~/.zshrc

For compilers to find ruby you may need to set:
  export LDFLAGS="-L/usr/local/opt/ruby/lib"
  export CPPFLAGS="-I/usr/local/opt/ruby/include"

For pkg-config to find ruby you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/ruby/lib/pkgconfig"

装完完之后是 v3 版本的 ruby

3. 配置环境变量

vi ~/.bash_profile

# ruyb
# intel 芯片
if [ -d "/usr/local/opt/ruby/bin" ]; then
  export PATH=/usr/local/opt/ruby/bin:$PATH
  export LDFLAGS="-L/usr/local/opt/ruby/lib"
  export CPPFLAGS="-I/usr/local/opt/ruby/include"
  export PATH=`gem environment gemdir`/bin:$PATH
fi
# m1 silicon 芯片
if [ -d "/opt/homebrew/opt/ruby/bin" ]; then
  export PATH=/opt/homebrew/opt/ruby/bin:$PATH
  export LDFLAGS="-L/opt/homebrew/opt/ruby/lib"
  export CPPFLAGS="-I/opt/homebrew/opt/ruby/include"
  export PATH=`gem environment gemdir`/bin:$PATH
fi

make 时需要用到 ruby,所以需要配置环境变量 LDFLAGS 库文件 CPPFLAGS 头文件 没有这个需求可以关掉

检查 ruby 版本

> ruby -v
ruby 3.1.3p185 (2022-11-24 revision 1a6b16756e) [x86_64-darwin22]

4. 安装 cocoapods

彻底删除旧版本

for i in $( gem list --local --no-version | grep cocoapods );
do
    sudo gem uninstall $i;
done

连同配置目录 ~/.cocoapods 一起删除

rm -rf ~/.cocoapods

开始全新安装

sudo gem install cocoapods

输出

Fetching nanaimo-0.3.0.gem
Fetching claide-1.1.0.gem
Fetching atomos-0.1.3.gem
Fetching colored2-3.1.2.gem
Fetching CFPropertyList-3.0.5.gem
Fetching xcodeproj-1.22.0.gem
Fetching ruby-macho-2.5.1.gem
Fetching nap-1.1.0.gem
Fetching molinillo-0.8.0.gem
Fetching gh_inspector-1.1.3.gem
Fetching fourflusher-2.3.1.gem
Fetching escape-0.0.4.gem
Fetching cocoapods-try-1.2.0.gem
Fetching netrc-0.11.0.gem
Fetching cocoapods-trunk-1.6.0.gem
Fetching cocoapods-search-1.0.1.gem
Fetching cocoapods-plugins-1.0.0.gem
Fetching cocoapods-downloader-1.6.3.gem
Fetching cocoapods-deintegrate-1.0.5.gem
Fetching ffi-1.15.5.gem
Fetching ethon-0.16.0.gem
Fetching typhoeus-1.4.0.gem
Fetching public_suffix-4.0.7.gem
Fetching fuzzy_match-2.0.4.gem
Fetching concurrent-ruby-1.1.10.gem
Fetching httpclient-2.8.3.gem
Fetching algoliasearch-1.27.5.gem
Fetching addressable-2.8.1.gem
Fetching zeitwerk-2.6.6.gem
Fetching tzinfo-2.0.5.gem
Fetching i18n-1.12.0.gem
Fetching activesupport-6.1.7.gem
Fetching cocoapods-1.11.3.gem
Fetching cocoapods-core-1.11.3.gem

...

Done installing documentation for nanaimo, colored2, claide, CFPropertyList, atomos, xcodeproj, ruby-macho, nap, molinillo, gh_inspector, fourflusher, escape, cocoapods-try, netrc, cocoapods-trunk, cocoapods-search, cocoapods-plugins, cocoapods-downloader, cocoapods-deintegrate, ffi, ethon, typhoeus, public_suffix, fuzzy_match, concurrent-ruby, httpclient, algoliasearch, addressable, zeitwerk, tzinfo, i18n, activesupport, cocoapods-core, cocoapods after 17 seconds
34 gems installed

> pod --version
Ignoring ffi-1.12.2 because its extensions are not built. Try: gem pristine ffi --version 1.12.2
1.11.3

参考

  • https://brew.sh
  • https://www.ruby-lang.org
  • https://cocoapods.org

结束语

如果本文对你有帮助,请转发让更多的朋友阅读。

也许这个操作只要你 3 秒钟,对我来说是一个激励,感谢。

祝你有一个美好的一天~

猫哥课程


© 猫哥

  • 微信 ducafecat

  • https://wiki.ducafecat.tech

  • https://ducafecat.com

Last Updated:
Contributors: ducafecat