最好先安装完Xcode,并且保证安装了Xcode Command Line Tool! 最好先安装完Xcode,并且保证安装了Xcode Command Line Tool!! 最好先安装完Xcode,并且保证安装了Xcode Command Line Tool!!! 重要的事情说三遍,不然可能会遇到各种问题。
CocoaPods升级1.5.3
sudo gem install -n /usr/local/bin cocoapods -v 1.5.3
最新的CocoaPods安装教程
说明:最近由于使用
sudo gem install cocoapods
命令安装cocoapods
过程中,出现
ERROR: While executing gem ... (Gem::FilePermissionError)
You don't have write permissions for the /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/gems/2.6.0 directory.
文件没有读写权限问题,尝试修改文件权限也无法成功的情况,所以优先推荐使用Homebrew
进行安装。
打开终端:>_
0、使用Homebrew
安装
一、安装Homebrew
(如果已经安装过,则跳到下一步)
a:
git clone https://github.com/Homebrew/brew homebrew
等待完成
b:
eval "$(homebrew/bin/brew shellenv)"
brew update --force --quiet
chmod -R go-w "$(brew --prefix)/share/zsh"
等待完成
c:输入brew
指令
brew
出现
Example usage:
brew search TEXT|/REGEX/
brew info [FORMULA|CASK...]
brew install FORMULA|CASK...
brew update
brew upgrade [FORMULA|CASK...]
brew uninstall FORMULA|CASK...
brew list [FORMULA|CASK...]
等等指令提示,说明Homebrew安装成功。
二、使用Homebrew
安装CocosPods
brew install cocoapods
等待完成。(期间可能在下载ruby
这个库的时候会出现多次失败,失败就重复执行上面的命令,多次就好了😂,其他的库若有失败,也是多尝试几次)
==> Installing cocoapods
==> Pouring cocoapods--1.12.1.arm64_ventura.bottle.tar.gz
🍺 /Users/liuhao/homebrew/Cellar/cocoapods/1.12.1: 13,430 files, 27.8MB
==> Running `brew cleanup cocoapods`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
最后如果出现这个,没有失败Failed
或者Error
的情况
此时运行
pod --version
1.12.1
证明Cocoapods
安装成功!
1、查看当前Ruby版本
ruby -v
2、升级Ruby环境,首先需要安装rvm(第一步要下载一些东西等两分钟左右)
curl -L get.rvm.io | bash -s stable
source ~/.bashrc
source ~/.bash_profile
如果报错:
Failed to connect to raw.githubusercontent.com port 443: Connection refused
我的办法是切换外网(因为公司有梯子。。。),如果没有可以自行度娘。
3、查看rvm版本
rvm -v
显示如下(或者是其他版本)
rvm 1.29.3 (latest) by Michal Papis, Piotr Kuczynski, Wayne E. Seguin [https://rvm.io]
4、列出ruby可安装的版本信息
rvm list known
显示如下
# MRI Rubies
[ruby-]1.8.6[-p420]
[ruby-]1.8.7[-head] # security released on head
[ruby-]1.9.1[-p431]
[ruby-]1.9.2[-p330]
[ruby-]1.9.3[-p551]
[ruby-]2.0.0[-p648]
[ruby-]2.1[.10]
[ruby-]2.2[.10]
[ruby-]2.3[.7]
[ruby-]2.4[.4]
[ruby-]2.5[.1] // 重点在这里 重点在这里 重点在这里
[ruby-]2.6[.0-preview2] // 测试版
ruby-head
.....
5、安装一个ruby版本(这里我选择的是2.5.1版本,当然你也可以选择其他的)
rvm install 2.5.1
正常情况:
// 注意:安装过程中需要两次按下 Enter 键, 第二次按下后需要输入电脑访问密码(不可见,只管输入就行);
// 如果你电脑没有安装Xcode和Command Line Tools for Xcode以及Homebrew 会自动下载安装,建议提前安装这三者.
如果出现:
No binary rubies available for: osx/11.5/x86_64/ruby-3.0.0.
Continuing with compilation. Please read 'rvm help mount' to get more information on binary rubies.
Checking requirements for osx.
Installing requirements for osx.
可以先执行
rvm autolibs read-only
再执行:
rvm install 2.5.1
这里很多小伙伴会遇到错误,大部分是因为没有安装Homebrew造成,所以所以所以要提前安装比较好
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
如果出现:
Fetching /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core failed!
首先执行
whoami
#查询出主机名,如果你已经知道,可以跳过,例如我的:
zxmmax
再执行命令:(zxmmax
替换成自己的主机名)
sudo chown -R zxmmax:staff *
brew doctor
再次执行上面brew命令即可。
6、设置为默认版本
rvm use 2.5.1 --default
7、更换源
sudo gem update --system
gem sources --remove https://rubygems.org/
gem sources --add https://gems.ruby-china.com/
8、为了验证你的Ruby镜像是并且仅是ruby-china,执行以下命令查看
gem sources -l
如果是以下结果说明正确,如果有其他的请自行百度解决
*** CURRENT SOURCES ***
https://gems.ruby-china.com/
9、这时候才正式开始安装CocoaPods
sudo gem install -n /usr/local/bin cocoapods
10、如果安装了多个Xcode使用下面的命令选择(一般需要选择最近的Xcode版本)
sudo xcode-select -switch /Applications/Xcode.app/Contents/Developer
11、安装本地库
pod setup
注意:有时候这里会报一些ruby相关的错误:
Traceback (most recent call last):
6: from /Users/zhouxianming/.rvm/rubies/ruby-2.6.3/bin/ruby_executable_hooks:10:in `<main>'
5: from /Users/zhouxianming/.rvm/rubies/ruby-2.6.3/lib/ruby/gems/2.6.0/gems/executable-hooks-1.6.0/lib/executable-hooks/hooks.rb:49:in `run'
4: from /Users/zhouxianming/.rvm/rubies/ruby-2.6.3/lib/ruby/gems/2.6.0/gems/executable-hooks-1.6.0/lib/executable-hooks/hooks.rb:49:in `each'
3: from /Users/zhouxianming/.rvm/rubies/ruby-2.6.3/lib/ruby/gems/2.6.0/gems/executable-hooks-1.6.0/lib/executable-hooks/hooks.rb:50:in `block in run'
2: from /Users/zhouxianming/.rvm/rubies/ruby-2.6.3/lib/ruby/gems/2.6.0/gems/rubygems-bundler-1.4.5/lib/rubygems_executable_plugin.rb:4:in `block in <top (required)>'
1: from /Users/zhouxianming/.rvm/rubies/ruby-2.6.3/lib/ruby/site_ruby/2.6.0/rubygems/core_ext/kernel_require.rb:92:in `require'
/Users/zhouxianming/.rvm/rubies/ruby-2.6.3/lib/ruby/site_ruby/2.6.0/rubygems/core_ext/kernel_require.rb:92:in `require': cannot load such file -- rubygems-bundler/noexec (LoadError)
10: from /Users/zhouxianming/.rvm/rubies/ruby-2.6.3/bin/ruby_executable_hooks:10:in `<main>'
9: from /Users/zhouxianming/.rvm/rubies/ruby-2.6.3/lib/ruby/gems/2.6.0/gems/executable-hooks-1.6.0/lib/executable-hooks/hooks.rb:49:in `run'
8: from /Users/zhouxianming/.rvm/rubies/ruby-2.6.3/lib/ruby/gems/2.6.0/gems/executable-hooks-1.6.0/lib/executable-hooks/hooks.rb:49:in `each'
7: from /Users/zhouxianming/.rvm/rubies/ruby-2.6.3/lib/ruby/gems/2.6.0/gems/executable-hooks-1.6.0/lib/executable-hooks/hooks.rb:50:in `block in run'
6: from /Users/zhouxianming/.rvm/rubies/ruby-2.6.3/lib/ruby/gems/2.6.0/gems/rubygems-bundler-1.4.5/lib/rubygems_executable_plugin.rb:4:in `block in <top (required)>'
5: from /Users/zhouxianming/.rvm/rubies/ruby-2.6.3/lib/ruby/site_ruby/2.6.0/rubygems/core_ext/kernel_require.rb:156:in `require'
4: from /Users/zhouxianming/.rvm/rubies/ruby-2.6.3/lib/ruby/site_ruby/2.6.0/rubygems/core_ext/kernel_require.rb:168:in `rescue in require'
3: from /Users/zhouxianming/.rvm/rubies/ruby-2.6.3/lib/ruby/site_ruby/2.6.0/rubygems/core_ext/kernel_require.rb:168:in `require'
2: from /Users/zhouxianming/.rvm/rubies/ruby-2.6.3/lib/ruby/gems/2.6.0/gems/rubygems-bundler-1.4.5/lib/rubygems-bundler/noexec.rb:131:in `<top (required)>'
1: from /Users/zhouxianming/.rvm/rubies/ruby-2.6.3/lib/ruby/gems/2.6.0/gems/rubygems-bundler-1.4.5/lib/rubygems-bundler/noexec.rb:124:in `check'
/Users/zhouxianming/.rvm/rubies/ruby-2.6.3/lib/ruby/gems/2.6.0/gems/rubygems-bundler-1.4.5/lib/rubygems-bundler/noexec.rb:84:in `setup': undefined method `plain_specs' for #<Bundler::RubygemsIntegration:0x00007fa9370511e8> (NoMethodError)
一般是Xcode Command Line Tool
缺失的原因,可以先执行
xcode-select --install
进行安装。
12、执行以上命令后
Setting up CocoaPods master repo
$ /usr/bin/git clone https://github.com/CocoaPods/Specs.git master --progress
Cloning into 'master'...
remote: Counting objects: 1879515, done.
remote: Compressing objects: 100% (321/321), done.
Receiving objects: 21% (404525/1879515), 73.70 MiB | 22.00 KiB/
然后就是漫长的等待,当然,网络好的情况下会更快
以下一段忽略,直接看 第13步:
要查看文件下载进度的可以另外打开一个终端窗口(快捷键:选中终端按下Command+N组合键),输入以下两行命令回车执行
cd ~/.cocoapods
du -sh *
执行du -sh *之后会显示已下载的文件大小,可以多次执行来监看下载进度,如果之前还有文件大小,后来变成0了,可能是网络问题,下载已经中断了,需要结束命令并从新执行 pod setup
13、下载安装完成之后可执行下列命令检查是否可用(第一次使用可能要等一会)
pod search AFNetworking
14、CocoaPods的具体使用
新建一个Xcode工程,使用终端cd到工程目录下
创建Podfile文件:
pod init
之后就可以在项目目录里看到一个Podfile文件
打开Podfile文件:
open Podfile
添加:
pod 'AFNetworking'
保存后退出
开始下载:
pod install
CocoaPods版本
pod --version