最新Mac安装CocoaPods详细教程及各种坑解决办法

时间:2024-03-09 15:49:23

  网上有很多教程,但要么内容很老,要么不详细,要么各种坑的情况没写。最近买新电脑了,正好要走一遍这些流程,所以写下次教程。

  如果你的Ruby版本是2.2.2以上,则忽略下面第一步。查看Ruby版本命令:ruby -v

一、安装RVM及更新Ruby

  安装RVM的目的是为了更新Ruby。

  安装RVM命令:curl -L get.rvm.io | bash -s stable

  可能会出现下面错误:

# 张林峰,
#
#   Thank you for using RVM!
#   We sincerely hope that RVM helps to make your life easier and more enjoyable!!!
#
# ~Wayne, Michal & team.

In case of problems: https://rvm.io/help and https://twitter.com/rvm_io

Upgrade Notes:


  * WARNING: You have \'~/.profile\' file, you might want to load it,
    to do that add the following line to \'/Users/zhanglinfeng/.bash_profile\':

      source ~/.profile

  * No new notes to display.

  意思就是让你打开根目录下的~/.profile这个文件,在文件末尾加上这句话source ~/.profile,保存,重新执行安装RVM命令。

  安装RVM成功后,执行rvm list known命令列出已知的ruby版本。

  我这里最新的是2.4,所以执行安装Ruby命令:rvm install 2.4

  可能会出现报错1:

zhanglinfengdeMacBook-Pro:~ zhanglinfeng$ rvm install 2.4
Searching for binary rubies, this might take some time.
No binary rubies available for: osx/10.12/x86_64/ruby-2.4.0.
Continuing with compilation. Please read \'rvm help mount\' to get more information on binary rubies.
Checking requirements for osx.
About to install Homebrew, press `Enter` for default installation in `/usr/local`,
type new path if you wish custom Homebrew installation (the path needs to be writable for user)
: 

  这种情况找不到2.4这个版本,可能你会问刚才列出的ruby版本中有2.4啊。rvm list known命令列出列出的是各个平台中的ruby版本,你这个平台不一定有2.4。看上面错误提示,你点击Enter后会给你安装Homebrew,安装完后会自动给你下载安装ruby2.4。这个过程花的时间比较长。(我猜测如果不点Enter,直接关掉终端,重新打开终端,执行安装一个版本低一点的ruby说不定就不会报这个错)

  查看现在的ruby版本 ruby -v,如果不是2.4,则执行rvm use ruby-2.4,重新查看,发现ruby的版本一变为2.4

  可能会出现报错2:

zhanglingdembp2:~ zhanglinfeng$ rvm install 2.6.3
Warning! PATH is not properly set up, \'/Users/zhanglinfeng/.rvm/gems/ruby-2.4.0/bin\' is not at first place.
         Usually this is caused by shell initialization files. Search for \'PATH=...\' entries.
         You can also re-add RVM to your profile by running: \'rvm get stable --auto-dotfiles\'.
         To fix it temporarily in this shell session run: \'rvm use ruby-2.4.0\'.
         To ignore this error add rvm_silence_path_mismatch_check_flag=1 to your ~/.rvmrc file.
Searching for binary rubies, this might take some time.
No binary rubies available for: osx/10.14/x86_64/ruby-2.6.3.
Continuing with compilation. Please read \'rvm help mount\' to get more information on binary rubies.
Checking requirements for osx.
Installing requirements for osx.
Updating system............
Error running \'requirements_osx_brew_update_system ruby-2.6.3\',
showing last 15 lines of /Users/zhanglinfeng/.rvm/log/1585985497_ruby-2.6.3/update_system.log
++ printf %b \'
Xcode version older than 4.6.2 installed, download and install newer version from:

    http://connect.apple.com

After installation open Xcode, go to Downloads and install Command Line Tools.
\n\'

Xcode version older than 4.6.2 installed, download and install newer version from:

    http://connect.apple.com

After installation open Xcode, go to Downloads and install Command Line Tools.

++ return 1
Requirements installation failed with status: 1.

这种情况先升级rvm就可以了,升级rvm命令

rvm get master

然后再升级ruby

rvm install 2.4

 

二、安装CocoaPods

1.安装

  安装CocoaPods命令:sudo gem install cocoapods,但是这个被防火墙了。我们换国内镜像来安装。执行下面两个命令

gem sources --remove https://rubygems.org/
gem sources --add https://gems.ruby-china.com/

  查看是否换成了国内镜像命令:gem sources -l

  安装CocoaPods命令:sudo gem install cocoapods

  查看本地安装的CocoaPods列表: gem list

  卸载某个版本:

sudo gem uninstall cocoapods -v 0.35.0
sudo gem uninstall cocoapods-core -v 0.35.0
sudo gem uninstall cocoapods // 卸载所有

 

  可能出现的问题1:

  While executing gem ... (Gem::DependencyError)
  Unable to resolve dependencies: cocoapods requires cocoapods-core (= 1.2.1), cocoapods-downloader (< 2.0, >= 1.1.3), cocoapods-trunk (< 2.0, >= 1.2.0), molinillo (~> 0.5.7), xcodeproj (< 2.0, >= 1.4.4), colored2 (~> 3.1), ruby-macho (~> 1.1)

  可能你的gem版本问题, 执行下面这句

sudo gem update --system

  可能出现的问题2:

GemWrappers: Can not wrap missing file: pod
GemWrappers: Can not wrap missing file: sandbox-pod

表示你的 gem出问题了执行下面的命令

 

gem uninstall rubygems-update

 

 

 

2.初始化

pod 需要从指定的源中获取到第三方库, 所以需要对一个\'pod源\' 进行初始化, 把所有的库映射到本地

pod setup

如果成功,恭喜你后面不用看了。

如果报如下错误

[!] Unable to add a source with url `https://github.com/CocoaPods/Specs.git` named `cocoapods`.
You can try adding it manually in `/Users/zhanglinfeng/.cocoapods/repos` or via `pod repo add`.

添加默认的pod源

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

查看pod镜像源列表

pod repo list

 

如果Cloning spec repo `master` from `https://github.com/CocoaPods/Specs.git`一直卡主不动,或者卡了很长时间后出现下面错误

zhanglinfeng@mac LFKit % pod repo add master https://github.com/CocoaPods/Specs.git
Cloning spec repo `master` from `https://github.com/CocoaPods/Specs.git`
[!] /usr/bin/git clone https://github.com/CocoaPods/Specs.git -- master

Cloning into \'master\'...
error: RPC failed; curl 56 LibreSSL SSL_read: SSL_ERROR_SYSCALL, errno 54
fatal: the remote end hung up unexpectedly
fatal: early EOF
fatal: index-pack failed

我在网上找了很多方法,我也都尝试过,可能对有些人有用,对有些人没用。我是通过方法四解决的。

方法一:把pod源更换成国内镜像源

// 移除现有的镜像
pod repo remove master
// 添加新镜像源
pod repo add master https://gitcafe.com/akuandev/Specs.git
pod repo update
pod setup

已知的国内源有

https://gitcafe.com/akuandev/Specs.git 
http://git.oschina.net/akuandev/Specs.git 
https://git.coding.net/hging/Specs.git 
https://mirrors.tuna.tsinghua.edu.cn/git/CocoaPods/Specs.git

但是我试了后前2个国内源分别报如下错误

zhanglinfeng@mac LFKit % pod repo add master https://gitcafe.com/akuandev/Specs.git
Cloning spec repo `master` from `https://gitcafe.com/akuandev/Specs.git`
[!] /usr/bin/git clone https://gitcafe.com/akuandev/Specs.git -- master

Cloning into \'master\'...
fatal: unable to access \'https://gitcafe.com/akuandev/Specs.git/\': LibreSSL SSL_connect: SSL_ERROR_SYSCALL in connection to gitcafe.com:443 
zhanglinfeng@mac LFKit % pod repo add master http://git.oschina.net/akuandev/Specs.git
Cloning spec repo `master` from `http://git.oschina.net/akuandev/Specs.git`
[!] /usr/bin/git clone http://git.oschina.net/akuandev/Specs.git -- master

Cloning into \'master\'...
fatal: unable to access \'http://git.oschina.net/akuandev/Specs.git/\': The requested URL returned error: 403

第三个源https://git.coding.net/hging/Specs.git 是add成功了,但是pod install会会出现Cloning spec repo `cocoapods` from `https://github.com/CocoaPods/Specs.git`一直卡主不动,估计是公司项目里指定了这个源的原因,于是我放弃了换国内源的方法,但你们可以试试这个方法,说不定对你们来说可行。

方法二:增加git缓存大小

git config --global http.postBuffer 524288000

结果还是不行,又尝试增大到1G也不行,然后没尝试增到更大,继续找其他方法,你们可以试试更大,说不定可行。

方法三:在github 个人账号的setting中进行SSH keys的配置

可以用sourcetree客户端生成比较简单,偏好设置->账户,如果sourcetree客户端上已经添加了GitHub账户就点编辑,没有就添加一个GitHub账户,协议选择ssh,再点创建秘钥。

也可以命令创建ssh key

ssh-keygen -t rsa -C "邮箱"

如果你有多个代码托管平台,注册的邮箱不一样,想区分不同平台的ssh key,可以生成ssh key并同时保存制定的文件名

ssh-keygen -t rsa -f ~/.ssh/id_rsa.github -C "邮箱"

其中,id_rsa.github为指定的文件名

配置ssh config文件,如果文件不存在就创建

vim ~/.ssh/config

点击i进入编辑模式,添加

Host github.com
    Hostname ssh.github.com
    Port 443
    User 用户名
    IdentityFile ~/.ssh/id_rsa.github

点击esc退出编辑模式,并输入:wq保存并退出

将 SSH 密钥复制到剪贴板。
如果您的 SSH 密钥文件与示例代码不同,请修改文件名以匹配您当前的设置。 在复制密钥时,请勿添加任何新行或空格。

pbcopy < ~/.ssh/id_rsa.pub

如果 pbcopy 不可用,可找到隐藏的 .ssh 文件夹,在常用的文本编辑器中打开该文件,并将其复制到剪贴板。

不管是sourcetree还是命令创建的key,都要打开GitHub网站,在设置中设置ssh key

测试ssh key是否配置成功

ssh -T git@github.com

可能会看到类似如下的警告:

The authenticity of host \'github.com (IP ADDRESS)\' can\'t be established.
RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
Are you sure you want to continue connecting (yes/no)?

或者

The authenticity of host \'github.com (IP ADDRESS)\' can\'t be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no)?

输入yes,出现

Hi username! You\'ve successfully authenticated, but GitHub does not
 provide shell access.

SSH配置成功

然后可以

git clone git@github.com:CocoaPods/Specs.git ~/.cocoapods/repos/master

 方法四:

pod setup
cd ~/.cocoapods/repos 
git clone --depth 1 https://github.com/CocoaPods/Specs.git master
cd master
git fetch --unshallow
pod setup
 

 方法五:配置GitHub dns

在网站 https://www.ipaddress.com/ 分别搜索对应网址的ip结果

151.101.185.194 github.global-ssl.fastly.net
192.30.253.112 github.com

刷新dns

// 老系统
type dscacheutil -flushcache
// 新系统
sudo killall -HUP mDNSResponder

 方法六:使用代理

查看代理端口:
偏好设置-》网络-》连接的网络-》高级-》代理-》SOCKS代理服务器

配置:

打开代理软件,配置好代理之后,去终端输入 git配置命令,命令如下:
git config --global http.https://github.com.proxy socks5://127.0.0.1:10000
如此就从根本上解决了问题,下面附上设置代理前后 git命令的速度
代理前 10k/s ☹️
代理后 600k/s