Mac配置CocoaPods

本文介绍安装并使用CocoaPods

CocosPods的官网: https://www.cocoapods.org/

前言

CocoaPods是一个ruby gem,不知道具体是啥暂时也没关系~

安装CocoaPods的完整流程是:1、安装Homebrew -> 2、安装Ruby –> 3、安装并配置CocoaPods

三者之间有一个链式的依赖关系:Homebrew 用于安装 Ruby,然后 Ruby 用于运行 CocoaPods。

所以开始之前,我们先要了解两个东西,分别是Homebrew和Ruby。

1、Homebrew

官网:https://brew.sh/

Homebrew 是 Mac 操作系统上的一个包管理器,它可以让我们更方便地安装、更新和删除软件。Homebrew 可以用来安装 Ruby,也可以安装许多其他类型的软件。

安装Homebrew

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

Logs:

Last login: Tue Mar  5 10:23:16 on ttys000
➜  ~ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
==> Checking for `sudo` access (which may request your password)...
Password:
==> This script will install:
/opt/homebrew/bin/brew
/opt/homebrew/share/doc/homebrew
/opt/homebrew/share/man/man1/brew.1
/opt/homebrew/share/zsh/site-functions/_brew
/opt/homebrew/etc/bash_completion.d/brew
/opt/homebrew

Press RETURN/ENTER to continue or any other key to abort:
==> /usr/bin/sudo /usr/sbin/chown -R rayvision:admin /opt/homebrew
==> Downloading and installing Homebrew...
remote: Enumerating objects: 1215, done.
remote: Counting objects: 100% (886/886), done.
remote: Compressing objects: 100% (270/270), done.
remote: Total 1215 (delta 648), reused 819 (delta 605), pack-reused 329
Receiving objects: 100% (1215/1215), 1.01 MiB | 1.88 MiB/s, done.
Resolving deltas: 100% (682/682), completed with 140 local objects.
From https://github.com/Homebrew/brew
 * [new branch]          build-cask-internal-json-v3 -> origin/build-cask-internal-json-v3
 * [new branch]          dependabot/bundler/Library/Homebrew/json_schemer-2.2.0 -> origin/dependabot/bundler/Library/Homebrew/json_schemer-2.2.0
   96f861e94..cf7c093b3  master     -> origin/master
 * [new branch]          memoize-installed-tap-loading -> origin/memoize-installed-tap-loading
 * [new branch]          sponsors-maintainers-man-completions -> origin/sponsors-maintainers-man-completions
 * [new tag]             4.2.10     -> 4.2.10
 * [new tag]             4.2.11     -> 4.2.11
HEAD is now at cf7c093b3 Merge pull request #16812 from cho-m/formula_auditor-also-check-tag
Updated 3 taps (homebrew/services, homebrew/core and homebrew/cask).
==> Installation successful!

==> Homebrew has enabled anonymous aggregate formulae and cask analytics.
Read the analytics documentation (and how to opt-out) here:
  https://docs.brew.sh/Analytics
No analytics data has been sent yet (nor will any be during this install run).

==> Homebrew is run entirely by unpaid volunteers. Please consider donating:
  https://github.com/Homebrew/brew#donations

==> Next steps:
- Run brew help to get started
- Further documentation:
    https://docs.brew.sh

if there is a Warning like:

 Warning: /opt/homebrew/bin is not in your PATH.
  Instructions on how to configure your shell for Homebrew
  can be found in the 'Next steps' section below.

Steps

Next steps:
- Run these two commands in your terminal to add Homebrew to your PATH:
    (echo; echo 'eval "$(/opt/homebrew/bin/brew shellenv)"') >> /Users/hsiao/.zprofile
    eval "$(/opt/homebrew/bin/brew shellenv)"

we should follow the steps with two command:

1、(echo; echo 'eval "$(/opt/homebrew/bin/brew shellenv)"') >> /Users/hsiao/.zprofile

2、eval "$(/opt/homebrew/bin/brew shellenv)"

2、Ruby

官网:https://www.ruby-lang.org/en/

Ruby 是一种开源的编程语言,它易于理解和编写。CocoaPods 是用 Ruby 编写的一个gem,因此我们需要在计算机上安装 Ruby 才能使用 CocoaPods。

Mac系统自带的 Ruby 版本通常位于/usr/bin/ruby路径下,但自带的通常是较低版本,不建议用它来开发。建议同时安装最新版本的Ruby,并制定使用我们所安装的版本(当前最新版本为3.3.0)的 Ruby 版本进行开发。

我们可以采用rvm来管理多个不同版本的Ruby,就像flutter也可以使用fvm来管理。

查看默认的ruby:

/usr/bin/ruby -v

Logs:

ruby 2.6.10p210 (2022-04-12 revision 67958) [universal.arm64e-darwin23]

使用homebrew安装ruby

brew install ruby

Logs:

Last login: Tue Mar  5 11:05:10 on ttys001
➜  ~ brew install ruby
==> Downloading https://ghcr.io/v2/homebrew/core/ruby/manifests/3.3.0
############################################################################################################################################################################## 100.0%
==> Fetching dependencies for ruby: libyaml, ca-certificates and openssl@3
==> Downloading https://ghcr.io/v2/homebrew/core/libyaml/manifests/0.2.5
############################################################################################################################################################################## 100.0%
==> Fetching libyaml
==> Downloading https://ghcr.io/v2/homebrew/core/libyaml/blobs/sha256:98c0cf81bcdf7577d5fdc8cc18732970b9ae7e0e7423a733f88f0f566ba483ad
############################################################################################################################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/ca-certificates/manifests/2023-12-12
############################################################################################################################################################################## 100.0%
==> Fetching ca-certificates
==> Downloading https://ghcr.io/v2/homebrew/core/ca-certificates/blobs/sha256:5c99ffd0861f01adc19cab495027024f7d890e42a9e7b689706b85c8e2b9c9b3
############################################################################################################################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/openssl/3/manifests/3.2.1-1
############################################################################################################################################################################## 100.0%
==> Fetching openssl@3
==> Downloading https://ghcr.io/v2/homebrew/core/openssl/3/blobs/sha256:fce7bf159988bf2b0960fa3d33ea810688d422c433b6461c7020cd0c937827a6
############################################################################################################################################################################## 100.0%
==> Fetching ruby
==> Downloading https://ghcr.io/v2/homebrew/core/ruby/blobs/sha256:f1df6988bf95afc9fd2f0f3c81f5ebbc44c329e0256e0cb9e6d1ff0afa87042c
############################################################################################################################################################################## 100.0%
==> Installing dependencies for ruby: libyaml, ca-certificates and openssl@3
==> Installing ruby dependency: libyaml
==> Downloading https://ghcr.io/v2/homebrew/core/libyaml/manifests/0.2.5
Already downloaded: /Users/rayvision/Library/Caches/Homebrew/downloads/5f6b0af1730aa1bc02f8021c90ed7ffe988eeba95eec83e6c828f77332ba6406--libyaml-0.2.5.bottle_manifest.json
==> Pouring libyaml--0.2.5.arm64_sonoma.bottle.tar.gz
🍺  /opt/homebrew/Cellar/libyaml/0.2.5: 10 files, 352.3KB
==> Installing ruby dependency: ca-certificates
==> Downloading https://ghcr.io/v2/homebrew/core/ca-certificates/manifests/2023-12-12
Already downloaded: /Users/rayvision/Library/Caches/Homebrew/downloads/13aa86e429e05d02a76005d1881eaf625091a5ac4dc7d7674c706d12ba48796a--ca-certificates-2023-12-12.bottle_manifest.json
==> Pouring ca-certificates--2023-12-12.all.bottle.tar.gz
==> Regenerating CA certificate bundle from keychain, this may take a while...
🍺  /opt/homebrew/Cellar/ca-certificates/2023-12-12: 3 files, 226.7KB
==> Installing ruby dependency: openssl@3
==> Downloading https://ghcr.io/v2/homebrew/core/openssl/3/manifests/3.2.1-1
Already downloaded: /Users/rayvision/Library/Caches/Homebrew/downloads/f7b6e249843882452d784a8cbc4e19231186230b9e485a2a284d5c1952a95ec2--openssl@3-3.2.1-1.bottle_manifest.json
==> Pouring openssl@3--3.2.1.arm64_sonoma.bottle.1.tar.gz
🍺  /opt/homebrew/Cellar/openssl@3/3.2.1: 6,874 files, 32MB
==> Installing ruby
==> Pouring ruby--3.3.0.arm64_sonoma.bottle.tar.gz
==> Caveats
By default, binaries installed by gem will be placed into:
  /opt/homebrew/lib/ruby/gems/3.3.0/bin

You may want to add this to your PATH.

ruby is keg-only, which means it was not symlinked into /opt/homebrew,
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="/opt/homebrew/opt/ruby/bin:$PATH"' >> ~/.zshrc

For compilers to find ruby you may need to set:
  export LDFLAGS="-L/opt/homebrew/opt/ruby/lib"
  export CPPFLAGS="-I/opt/homebrew/opt/ruby/include"
==> Summary
🍺  /opt/homebrew/Cellar/ruby/3.3.0: 19,656 files, 51.4MB
==> 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:
  /opt/homebrew/lib/ruby/gems/3.3.0/bin

You may want to add this to your PATH.

ruby is keg-only, which means it was not symlinked into /opt/homebrew,
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="/opt/homebrew/opt/ruby/bin:$PATH"' >> ~/.zshrc

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

⚠️注意:要将当前Ruby设置为默认版本,不然在下一步安装CocoaPods会报兼容性错误,因为默认ruby版本太低了

默认版本太低报错

ERROR:  Error installing cocoapods:
	The last version of drb (>= 0) to support your Ruby & RubyGems was 2.0.6. Try installing it with `gem install drb -v 2.0.6` and then running the current command again
	drb requires Ruby version >= 2.7.0. The current ruby version is 2.6.10.210.

配置ruby到$PATH环境变量,默认使用当前最新版

echo 'export PATH="/opt/homebrew/opt/ruby/bin:$PATH"' >> ~/.zshrc
echo 'export LDFLAGS="-L/opt/homebrew/opt/ruby/lib"' >> ~/.zshrc
echo 'export CPPFLAGS="-I/opt/homebrew/opt/ruby/include"' >> ~/.zshrc
source ~/.zshrc

查看当前使用的Ruby版本

login: Tue Mar  5 11:18:48 on ttys002
➜  ~ ruby -v
ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [arm64-darwin23]

🏅没错,当前已经使用我们所安装的最新Ruby,版本是3.3.0。

接下来安装CocoaPods

3、CocoaPods

官网:https://www.cocoapods.org/

安装CocoaPods

sudo gem install cocoapods

Logs:

~ sudo gem install cocoapods
Password:
Fetching nanaimo-0.3.0.gem
Fetching colored2-3.1.2.gem
Fetching claide-1.1.0.gem
Fetching CFPropertyList-3.0.7.gem
Fetching atomos-0.1.3.gem
Fetching xcodeproj-1.24.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-2.1.gem
Fetching cocoapods-deintegrate-1.0.5.gem
Fetching ffi-1.16.3.gem
Fetching ethon-0.16.0.gem
Fetching typhoeus-1.4.1.gem
Fetching public_suffix-4.0.7.gem
Fetching fuzzy_match-2.0.4.gem
Fetching concurrent-ruby-1.2.3.gem
Fetching httpclient-2.8.3.gem
Fetching algoliasearch-1.27.5.gem
Fetching addressable-2.8.6.gem
Fetching tzinfo-2.0.6.gem
Fetching i18n-1.14.1.gem
Fetching connection_pool-2.4.1.gem
Fetching activesupport-7.1.3.2.gem
Fetching cocoapods-1.15.2.gem
Fetching cocoapods-core-1.15.2.gem
Successfully installed nanaimo-0.3.0
Successfully installed colored2-3.1.2
Successfully installed claide-1.1.0
Successfully installed CFPropertyList-3.0.7
Successfully installed atomos-0.1.3
Successfully installed xcodeproj-1.24.0
Successfully installed ruby-macho-2.5.1
Successfully installed nap-1.1.0
Successfully installed molinillo-0.8.0
Successfully installed gh_inspector-1.1.3
Successfully installed fourflusher-2.3.1
Successfully installed escape-0.0.4
Successfully installed cocoapods-try-1.2.0
Successfully installed netrc-0.11.0
Successfully installed cocoapods-trunk-1.6.0
Successfully installed cocoapods-search-1.0.1
Successfully installed cocoapods-plugins-1.0.0
Successfully installed cocoapods-downloader-2.1
Successfully installed cocoapods-deintegrate-1.0.5
Building native extensions. This could take a while...
Successfully installed ffi-1.16.3
Successfully installed ethon-0.16.0
Successfully installed typhoeus-1.4.1
Successfully installed public_suffix-4.0.7
Successfully installed fuzzy_match-2.0.4
Successfully installed concurrent-ruby-1.2.3
Successfully installed httpclient-2.8.3
A new major version is available for Algolia! Please now use the https://rubygems.org/gems/algolia gem to get the latest features.
Successfully installed algoliasearch-1.27.5
Successfully installed addressable-2.8.6
Successfully installed tzinfo-2.0.6
Successfully installed i18n-1.14.1
Successfully installed connection_pool-2.4.1
Successfully installed activesupport-7.1.3.2
Successfully installed cocoapods-core-1.15.2
Successfully installed cocoapods-1.15.2
Parsing documentation for nanaimo-0.3.0
Installing ri documentation for nanaimo-0.3.0
Parsing documentation for colored2-3.1.2
Installing ri documentation for colored2-3.1.2
Parsing documentation for claide-1.1.0
Installing ri documentation for claide-1.1.0
Parsing documentation for CFPropertyList-3.0.7
Installing ri documentation for CFPropertyList-3.0.7
Parsing documentation for atomos-0.1.3
Installing ri documentation for atomos-0.1.3
Parsing documentation for xcodeproj-1.24.0
Installing ri documentation for xcodeproj-1.24.0
Parsing documentation for ruby-macho-2.5.1
Installing ri documentation for ruby-macho-2.5.1
Parsing documentation for nap-1.1.0
Installing ri documentation for nap-1.1.0
Parsing documentation for molinillo-0.8.0
Installing ri documentation for molinillo-0.8.0
Parsing documentation for gh_inspector-1.1.3
Installing ri documentation for gh_inspector-1.1.3
Parsing documentation for fourflusher-2.3.1
Installing ri documentation for fourflusher-2.3.1
Parsing documentation for escape-0.0.4
Installing ri documentation for escape-0.0.4
Parsing documentation for cocoapods-try-1.2.0
Installing ri documentation for cocoapods-try-1.2.0
Parsing documentation for netrc-0.11.0
Installing ri documentation for netrc-0.11.0
Parsing documentation for cocoapods-trunk-1.6.0
Installing ri documentation for cocoapods-trunk-1.6.0
Parsing documentation for cocoapods-search-1.0.1
Installing ri documentation for cocoapods-search-1.0.1
Parsing documentation for cocoapods-plugins-1.0.0
Installing ri documentation for cocoapods-plugins-1.0.0
Parsing documentation for cocoapods-downloader-2.1
Installing ri documentation for cocoapods-downloader-2.1
Parsing documentation for cocoapods-deintegrate-1.0.5
Installing ri documentation for cocoapods-deintegrate-1.0.5
Parsing documentation for ffi-1.16.3
Installing ri documentation for ffi-1.16.3
Parsing documentation for ethon-0.16.0
Installing ri documentation for ethon-0.16.0
Parsing documentation for typhoeus-1.4.1
Installing ri documentation for typhoeus-1.4.1
Parsing documentation for public_suffix-4.0.7
Installing ri documentation for public_suffix-4.0.7
Parsing documentation for fuzzy_match-2.0.4
Installing ri documentation for fuzzy_match-2.0.4
Parsing documentation for concurrent-ruby-1.2.3
Installing ri documentation for concurrent-ruby-1.2.3
Parsing documentation for httpclient-2.8.3
Installing ri documentation for httpclient-2.8.3
Parsing documentation for algoliasearch-1.27.5
Installing ri documentation for algoliasearch-1.27.5
Parsing documentation for addressable-2.8.6
Installing ri documentation for addressable-2.8.6
Parsing documentation for tzinfo-2.0.6
Installing ri documentation for tzinfo-2.0.6
Parsing documentation for i18n-1.14.1
Installing ri documentation for i18n-1.14.1
Parsing documentation for connection_pool-2.4.1
Installing ri documentation for connection_pool-2.4.1
Parsing documentation for activesupport-7.1.3.2
Couldn't find file to include 'activesupport/README.rdoc' from lib/active_support.rb
Installing ri documentation for activesupport-7.1.3.2
Parsing documentation for cocoapods-core-1.15.2
Installing ri documentation for cocoapods-core-1.15.2
Parsing documentation for cocoapods-1.15.2
Installing ri documentation for cocoapods-1.15.2
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, tzinfo, i18n, connection_pool, activesupport, cocoapods-core, cocoapods after 10 seconds
34 gems installed

A new release of RubyGems is available: 3.5.4 → 3.5.6!
Run `gem update --system 3.5.6` to update your installation.

➜  ~

安装过程结束,接下来我们需要初始化pod,这个过程会设置master repo,也就是配置默认的源,但是这个过程也可能异常,稍后会介绍。

初始化pod

pod setup

🤯Oops,发生了一个意外:

zsh:command not found:pod

这个错误通常表示pod命令没有被正确添加到当前的$PATH环境变量中,也就是说,系统找不到pod命令的位置。

我们是通过gem install cocoapods命令安装的 CocoaPods,那么 pod 命令应该位于 Ruby Gems 的bin目录中。但这个目录可能没有被加入到$PATH环境变量中,因此需要手动添加。

依然以 zsh 为例,你需要做的是,打开~/.zshrc文件,然后添加下面这一行:

export PATH="$PATH:$(gem env gemdir)/bin"

或者使用命令行:

echo 'export PATH="$PATH:$(gem env gemdir)/bin"' >> ~/.zshrc
source ~/.zshrc

然后重新打开终端。

查验一下是否已经添加到环境变量中:

cat ~/.zshrc

export PATH="/user/local/bin:$PATH"
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="$PATH:$(gem env gemdir)/bin"

包含了export PATH="$PATH:$(gem env gemdir)/bin"就能使用pod指令了。

继续完成初始化工作

pod setup
Setup completed

使用Pod的搜索功能

pod search AFNetworking

搜索遇到问题:

Setup completed
[!]Unable to find a pod with name, author, summary, or description matching 'AFNetworking'

针对该问题,网络上一种

方案A:是认为是search缓存导致的,解决方案:

rm ~/Library/Caches/CocoaPods/search_index.json

实测无效🙅。另一种

方案B:是不管当前问题,直接使用Xcode创建一个iOS项目,并使用pod随便引用一个三方库如AFNetworking,最后执行pod install完成之后,再回到这里就能正常使用pod search功能。本文没尝试这种方案。。。

那该怎么解决呢?先看看pod环境

pod env

Logs:

xcode-select: error: tool 'xcodebuild' requires Xcode, but active developer directory '/Library/Developer/CommandLineTools' is a command line tools instance

解决方案是:

sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer

其实这里补充很关键的一点是,首先,确保已经安装了 Xcode 和 Xcode 命令行工具。如果没有,请在 App Store 中下载并安装 Xcode,并打开终端输入以下命令以安装 Xcode 命令行工具:

xcode-select --install

最后我们再次检查pod环境:pod env,好了,环境已经没有问题了。

  ~ pod env

### Stack

```
   CocoaPods : 1.15.2
        Ruby : ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [arm64-darwin23]
    RubyGems : 3.5.4
        Host : macOS 14.3.1 (23D60)
       Xcode : 15.2 (15C500b)
         Git : git version 2.39.3 (Apple Git-145)
Ruby lib dir : /opt/homebrew/Cellar/ruby/3.3.0/lib
Repositories :
```

### Installation Source

```
Executable Path: /opt/homebrew/lib/ruby/gems/3.3.0/bin/pod
```

### Plugins

```
cocoapods-deintegrate : 1.0.5
cocoapods-plugins     : 1.0.0
cocoapods-search      : 1.0.1
cocoapods-trunk       : 1.6.0
cocoapods-try         : 1.2.0

到这一步,pod环境已经正常,但在此使用pod的搜索功能还是报错找不到对应的库。

我们查看一下本地已添加的私有仓库

pod repo

😧发现竟然一个都没有:

0 repos

这意味着没有本地仓库,那我们可以使用pod repo add命令添加一个私有仓库。例如,执行pod repo add <repo-name> <repo-url>来添加一个私有仓库。

这里添加官方私有仓库

pod repo add cocoapods https://github.com/CocoaPods/Specs.git

Logs

Cloning spec repo `cocoapods` from `https://github.com/CocoaPods/Specs.git`

这个过程需要等待一段时间⏳,指令结束后我们再次查看本地似有仓库,看到已经包含官方私有仓库了:

pod repo

cocoapods
- Type: git (remotes/origin/master)
- URL:  https://github.com/CocoaPods/Specs.git
- Path: /Users/rayvision/.cocoapods/repos/cocoapods

📌也有安装清华大学的私有仓库,但是本文主打一个能够科学上网、所以未采用该方案。

使用清华源:

pod repo add tuna https://mirrors.tuna.tsinghua.edu.cn/git/CocoaPods/Specs.git

[清华源]:https://mirrors.tuna.tsinghua.edu.cn/help/CocoaPods/

安装过程终于快收尾了,再来试试搜索功能:

pod search AFNetworking

没想到还是报一样的错误❌,找不到!!!

😊其实到这里重开一遍终端再次执行搜索指令,就能正常工作了。

或者在设置官方私有仓库之后执行:

pod repo update --verbose

发表回复