2021最新安装CocoaPods -- 快速安装Homebre

2022-01-01  本文已影响0人  啸狼天

本着升级更新版本的念头(其实就是一时手贱),cocoaPods 使用不了了, 所以就重新安装一下。

如果之前装过cocopods,最好先卸载掉,卸载命令:

sudo gem uninstall cocoapods

先查看本地安装过的cocopods相关东西,命令如下:

gem list --local | grep cocoapods
mars@macbook-pro ~ % gem list --local | grep cocoapods
cocoapods-core (1.11.2, 1.10.0)
cocoapods-deintegrate (1.0.4)
cocoapods-downloader (1.4.0)
cocoapods-plugins (1.0.0)
cocoapods-search (1.0.0)
cocoapods-trunk (1.5.0)
cocoapods-try (1.2.0)

然后逐个删除吧:

//示例
sudo gem uninstall cocoapods-core
sudo gem uninstall cocoapods-deintegrate
sudo gem uninstall cocoapods-downloader
sudo gem uninstall cocoapods-plugins
sudo gem uninstall cocoapods-search
sudo gem uninstall cocoapods-trunk
sudo gem uninstall cocoapods-try

为了方便操作, 免去配置镜像, 所以提前安装了Homebrew

关于Homebrew
Homebrew 是一款 Mac OSX 环境下高效便捷的包管理器(通过 brew 命令执行相关包管理),推荐日常 Mac OSX 环境下安装、卸载软件的使用;

Homebrew的安装方法

官网给出的安装方法:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

但是非常慢, 所以找到了一个国内自动安装脚本

/bin/bash -c "$(curl -fsSL https://cdn.jsdelivr.net/gh/ineo6/homebrew-install/install.sh)"
/bin/zsh -c "$(curl -fsSL [https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh](https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh))"

若没明白也没关系, 你可以考虑安装后先执行下brew doctor, 它会帮你执行一系列环境相关检查, 对你提出很多建设性的意见. PATH环境变量的问题应该也会有一些相关说明,可以作为进一步的处理介绍。

Homebrew 切换国内源

中国科大

# 修改 brew.git
> cd "$(brew --repo)" && git remote set-url origin https://mirrors.ustc.edu.cn/brew.git

# 修改 homebrew-core.git
> cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core" && git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git

# 修改 homebrew-bottles
> echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.bash_profile && source ~/.bash_profile

# 立刻生效
> brew update

清华大学

# 修改 brew.git
> cd "$(brew --repo)" && git remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git

# 修改 homebrew-core.git
> cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core" && git remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git

# 修改 homebrew-bottles
> echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles' >> ~/.bash_profile && source ~/.bash_profile

# 立刻生效
> brew update
# 查看(诊断) brew 状态
> brew doctor

# 重置 brew.git
> cd "$(brew --repo)" && git fetch && git reset --hard origin/master

# 切回官方 brew.git
> cd "$(brew --repo)" && git remote set-url origin https://github.com/Homebrew/brew.git

# 重置 homebrew-core.git
> cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core" && git fetch && git reset --hard origin/master

# 切回官方 homebrew-core.git
> cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core" && git remote set-url origin https://github.com/Homebrew/homebrew-core.git

# 立刻生效
> brew update

brew 卸载脚本

/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/HomebrewUninstall.sh)"

其他操作

安装软件:brew install xxx
卸载软件:brew uninstall xxx
搜索软件:brew search xxx
更新软件:brew upgrade xxx
查看列表:brew list
更新brew:brew update
清理所有包的旧版本:brew cleanup
清理指定包的旧版本:brew cleanup $FORMULA
查看可清理的旧版本包,不执行实际操作:brew cleanup -n

Homebrew 安装完成之后,处理安装Ruby 版本:

brew install ruby

当ruby安装完成之后,会提醒你将ruby的路径导入到环境变量中,如下提示:

==> Reinstalling ruby
==> Downloading https://homebrew.bintray.com/bottles/ruby-2.7.1_2.catalina.bottle.tar.gz
Already downloaded: /Users/lichenchen/Library/Caches/Homebrew/downloads/1a9255e83b1fb1738532e9156b16bc750b493bbef40486552db5f7fa5b8ff08d--ruby-2.7.1_2.catalina.bottle.tar.gz
==> Pouring ruby-2.7.1_2.catalina.bottle.tar.gz
==> Caveats
By default, binaries installed by gem will be placed into:
  /usr/local/lib/ruby/gems/2.7.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"

这里,我们按照提示,直接导入到环境变量就好了,执行以下命令:

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

source ~/.zshrc

最后我们再使用which命令看一下当前的ruby使用的是哪个路径下的ruby。

which ruby

变成如下的路径就替换了我们自己所安装的ruby
/usr/local/opt/ruby/bin/ruby
至此,系统中就有了两套ruby环境,我们使用的时候用我们自己安装的一套就好了,也就从根本上解决了权限的问题。

安装完成后查看其Ruby源:

gem sources -l

此时可能会出现错误:dyld: Library not loaded: /usr/local/opt/gmp/lib/libgmp.10.dylib

这里就要更新 Ruby 版本了:

brew update && brew install gmp

更新完成后再次查看 Ruby源:

如果显示的ruby源不是ruby-china,则需要进行源替换:

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

ruby源正常, 安装CocoaPods:

sudo gem install -n /usr/local/bin cocoapods
mars@macbook-pro ~ %  echo 'export PATH="/usr/local/sbin:$PATH"' >> ~/.zshrc
mars@macbook-pro ~ %  echo 'export PATH="/usr/local/opt/ruby/bin:$PATH"' >> ~/.zshrc
mars@macbook-pro ~ %  export LDFLAGS="-L/usr/local/opt/ruby/lib"
mars@macbook-pro ~ %  export CPPFLAGS="-I/usr/local/opt/ruby/include"
mars@macbook-pro ~ % source ~/.zshrc
mars@macbook-pro ~ % which ruby
/usr/local/opt/ruby/bin/ruby
mars@macbook-pro ~ % sudo gem install -n /usr/local/bin cocoapods
Password:
Fetching nanaimo-0.3.0.gem
Fetching claide-1.0.3.gem
Fetching atomos-0.1.3.gem
Fetching ruby-macho-2.5.1.gem
Fetching colored2-3.1.2.gem
Fetching CFPropertyList-3.0.5.gem
Fetching xcodeproj-1.21.0.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.5.1.gem
Fetching cocoapods-deintegrate-1.0.5.gem
Fetching ffi-1.15.4.gem
Fetching ethon-0.15.0.gem
Fetching typhoeus-1.4.0.gem
Fetching public_suffix-4.0.6.gem
Fetching fuzzy_match-2.0.4.gem
Fetching concurrent-ruby-1.1.9.gem
Fetching httpclient-2.8.3.gem
Fetching algoliasearch-1.27.5.gem
Fetching addressable-2.8.0.gem
Fetching zeitwerk-2.5.3.gem
Fetching tzinfo-2.0.4.gem
Fetching i18n-1.8.11.gem
Fetching activesupport-6.1.4.4.gem
Fetching cocoapods-1.11.2.gem
Fetching cocoapods-core-1.11.2.gem
Successfully installed nanaimo-0.3.0
Successfully installed colored2-3.1.2
Successfully installed claide-1.0.3
Successfully installed CFPropertyList-3.0.5
Successfully installed atomos-0.1.3
Successfully installed xcodeproj-1.21.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-1.5.1
Successfully installed cocoapods-deintegrate-1.0.5
Building native extensions. This could take a while...
Successfully installed ffi-1.15.4
Successfully installed ethon-0.15.0
Successfully installed typhoeus-1.4.0
Successfully installed public_suffix-4.0.6
Successfully installed fuzzy_match-2.0.4
Successfully installed concurrent-ruby-1.1.9
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.0
Successfully installed zeitwerk-2.5.3
Successfully installed tzinfo-2.0.4
Successfully installed i18n-1.8.11
Successfully installed activesupport-6.1.4.4
Successfully installed cocoapods-core-1.11.2
Successfully installed cocoapods-1.11.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.0.3
Installing ri documentation for claide-1.0.3
Parsing documentation for CFPropertyList-3.0.5
Installing ri documentation for CFPropertyList-3.0.5
Parsing documentation for atomos-0.1.3
Installing ri documentation for atomos-0.1.3
Parsing documentation for xcodeproj-1.21.0
Installing ri documentation for xcodeproj-1.21.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-1.5.1
Installing ri documentation for cocoapods-downloader-1.5.1
Parsing documentation for cocoapods-deintegrate-1.0.5
Installing ri documentation for cocoapods-deintegrate-1.0.5
Parsing documentation for ffi-1.15.4
Installing ri documentation for ffi-1.15.4
Parsing documentation for ethon-0.15.0
Installing ri documentation for ethon-0.15.0
Parsing documentation for typhoeus-1.4.0
Installing ri documentation for typhoeus-1.4.0
Parsing documentation for public_suffix-4.0.6
Installing ri documentation for public_suffix-4.0.6
Parsing documentation for fuzzy_match-2.0.4
Installing ri documentation for fuzzy_match-2.0.4
Parsing documentation for concurrent-ruby-1.1.9
Installing ri documentation for concurrent-ruby-1.1.9
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.0
Installing ri documentation for addressable-2.8.0
Parsing documentation for zeitwerk-2.5.3
Installing ri documentation for zeitwerk-2.5.3
Parsing documentation for tzinfo-2.0.4
Installing ri documentation for tzinfo-2.0.4
Parsing documentation for i18n-1.8.11
Installing ri documentation for i18n-1.8.11
Parsing documentation for activesupport-6.1.4.4
Installing ri documentation for activesupport-6.1.4.4
Parsing documentation for cocoapods-core-1.11.2
Installing ri documentation for cocoapods-core-1.11.2
Parsing documentation for cocoapods-1.11.2
Installing ri documentation for cocoapods-1.11.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, zeitwerk, tzinfo, i18n, activesupport, cocoapods-core, cocoapods after 32 seconds
34 gems installed
mars@macbook-pro ~ % 

安装成功后进行pod设置:

pod setup

执行完毕后, 查看pod版本看安装是否成功:

pod --version

CocoaPods的使用

CocoaPods安装成功后, 就可直接进行使用, 这里不做过多的赘述,直接写几个常用的命令:

cd 项目目录地址
// 创建 Podfile 文件
pod init
// 编辑 
vim Podfile
// 安装
pod install
// 更新所有依赖的开源库
pod update
pod install --no-repo-update
pod update --no-repo-update
// 搜索框架
pod search 框架名

Podfile 讲解

# 下面两行是指明依赖库的来源地址
source 'https://github.com/CocoaPods/Specs.git'
source 'https://github.com/Artsy/Specs.git'

# 说明平台是ios,版本是9.0
platform :ios, '9.0'

# 忽略引入库的所有警告(强迫症者的福音啊)
inhibit_all_warnings!

# 针对MyApp target引入AFNetworking
# 针对MyAppTests target引入OCMock,
target 'MyApp' do 
    pod 'AFNetworking', '~> 3.0' 
    target 'MyAppTests' do
       inherit! :search_paths 
       pod 'OCMock', '~> 2.0.1' 
    end
end
# 这个是cocoapods的一些配置,官网并没有太详细的说明,一般采取默认就好了,也就是不写.
post_install do |installer|       
   installer.pods_project.targets.each do |target| 
     puts target.name 
   end
end

详细看这里: 你真的会写Podfile吗?

使用中遇到的问题

情况一:
打开终端,查看一下cocoapods的版本号:pod --version
如果不是最新版本,升级一下cocoapods版本:sudo gem install -n /usr/local/bin cocoapods --pre
但是有时候cocoapods升到最新版本后我cd到工程文件下pod install,发现还是没有更新最新的库。这时候需要更新本地Pod的索引文件:pod setup或pod repo update
此时在pod install或pod update,A库应该就是最新的了。

情况二:
可能是别的库锁定了这个库的版本,也就是第三方库里用了第三方库并把它的版本给锁定了。
更详细看这里,都是杯具:
https://www.jianshu.com/p/a4c131491608

其他使用中的常见错误, 这里不做更多整理, 遇见了自己搜索解决方法即可。

如何卸载Homebrew

使用官方脚本同样会遇到uninstall地址无法访问问题,可以使用下面脚本:

/bin/bash -c "$(curl -fsSL https://cdn.jsdelivr.net/gh/ineo6/homebrew-install/uninstall.sh)"

参考:

Homebrew 国内自动安装脚本
通过 Homebrew 安装 CocoaPods - IDE

上一篇下一篇

猜你喜欢

热点阅读