iOS-Cocoapods 的正确安装姿势

在安装过程中出现curl: (7) Failed to connect to raw.githubusercontent.com port 443: Connection refused 问题访问我的处理方式可能会对你有帮助.
文末附带rvm 无法在线安装的解决办法.
文末还提供了pod install或者serach 过程中[!]CDN: trunk URL couldn't be downloaded:的解决办法.

1. Mac环境下 Cocoapods 的安装

安装pods需要依赖 ruby 环境,而安装 ruby 你需要借助工具 rvm,rvm是用来管理 ruby 版本的,也就是说你的电脑上可以安装多个不同版本的 ruby 可以使用 rvm 来管理他们。而安装 rvm 又需要借助另外一个工具 Homebrew,Homebrew是一款Mac OS平台下的软件包管理工具,拥有安装、卸载、更新、查看、搜索等很多实用的功能。这里不对 Homebrew 做过多讲解。

1.1 总体步骤

#####下载Xcode —>安装rvm —>安装ruby —>安装home-brew —>安装cocoapods

下载Xcode —>安装home-brew —>安装rvm —>安装ruby —>安装cocoapods

1.2 安装前,先检查是否有安装残留

1. 如果之前装过cocopods,最好先卸载掉,卸载命令:
$ sudo gem uninstall cocoapods

2. 先查看本地安装过的cocopods相关东西,命令如下:
$ gem list --local | grep cocoapods
会显示如下:
cocoapods (1.7.2)
cocoapods-core (1.7.2)
cocoapods-deintegrate (1.0.4)
cocoapods-downloader (1.2.2)
cocoapods-plugins (1.0.0)
cocoapods-search (1.0.0)
cocoapods-stats (1.1.0)
cocoapods-trunk (1.3.1)
cocoapods-try (1.1.0)

3. 使用删除命令, 逐个删除:
$  sudo gem uninstall cocoapods-core

1.3 Mac文件夹的显示隐藏命令行:

隐藏:defaults write com.apple.finder AppleShowAllFiles -bool true
显示:defaults write com.apple.finder AppleShowAllFiles -bool false

这里选择将隐藏文件显示出来; 退出终端,重启Finder. 如果不确定,可以把主目录下的隐藏文件都给删了.

1.4. RVM

  • Ruby Version Manager,Ruby版本管理器,包括Ruby的版本管理和Gem库管理(gemset)
1. 安装RVM
$ curl -sSL https://get.rvm.io | bash -s stable
期间可能需要管理员密码, 以及自动通过homebrew安装依赖包,等待一段时间就安装好了.

2. 载入 RVM 环境
$ source ~/.rvm/scripts/rvm

3. 检查一下是否安装正确
$ rvm -v
会显示如下:
rvm 1.29.8 (latest) by Michal Papis, Piotr Kuczynski, Wayne E. Seguin [https://rvm.io]
表示安装正确.

注意: 也可使用 ($ rvm -v) 来判断是否安装了rvm
// 结果类似如下代表没有安装rvm
zsh: command not found: rvm 

Update 2022.11.18

这里如果直接通过1.5进行安装ruby的话, 会报一些错误

Something went wrong during Homebrew installation,
can not find 'brew' command, please report a bug: https://github.com/rvm/rvm/issues
Requirements installation failed with status: 1.

意思就是没有homebrew, 解决办法就是跳转到1.7步骤, 先安装home-brew, 安装完成后再通过1.51.6步骤语句再安装ruby

1.5 用RVM安装Ruby环境(homebrew 安装完成后再安装)

1. 列出已知的ruby版本
$ rvm list known

2. 选择最新版本进行安装(这里以2.6.0为例)
$ rvm install 2.6.0

同样继续等待漫长的下载,编译过程,完成以后,Ruby, Ruby Gems 就安装好了。

3. 查询已经安装的ruby
$ rvm list

卸载一个已安装版本的命令
$ rvm remove + 要卸载的版本号

4. RVM 装好以后,需要执行下面的命令将指定版本的 Ruby 设置为系统默认版本
$ rvm 2.6.0 --default

5. 测试操作是否正确(分 2 步)
$ ruby -v
会显示如下:
ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-darwin18]

$ gem -v
会显示如下:
3.0.4

注: RubyGems(简称 gems)是一个用于对 Ruby组件进行打包的 Ruby 打包系统。 它提供一个分发 Ruby 程序和库的标准格式,还提供一个管理程序包安装的工具。

1.6 更换镜像源

1. 查看当前镜像源
$ gem sources -l
会显示如下:
*** CURRENT SOURCES *** 
http://rubygems.org/

2. 先删除, 再添加
$ gem sources --remove https://rubygems.org/
$ gem sources -a https://gems.ruby-china.com/

3. 再次查看, 测试是否成功
$ gem sources -l
会显示如下:
*** CURRENT SOURCES ***
https://gems.ruby-china.com/

到这里就已经把Ruby环境成功的安装到了Mac OS X上,接下来就可以进行相应的开发使用了。

1.7 安装home-brew

  • Homebrew: 是一个包管理器,用于在Mac上安装一些OS X没有的UNIX工具。
  • 官方网址: https://brew.sh/index_zh-cn
  • Homebrew是完全基于 Git 和 ruby.
  1. 安装 (这里最好去官网复制粘贴, 因为这个地址总会变)
$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)”

安装过程中需要按回车键确认

  1. 发现问题
    最近发现, 不管是否通过梯子都无法通过官网的代码直接安装homebrew, 搜了好久, 改hosts、git config --global http proxy都不行, 果断放弃了

  2. 解决问题(可以使用gtee代理的下载地址)

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

这个地址存放的都是国内的镜像, 直至安装完成会发现,环境变量也自动配置好了,brew可以直接使用(brew help试试)。如果不行,先执行下source /Users/zcm/.zprofile。

  1. 安装插件, 这个时候如果直接运行1.5步骤就可能报错:
Error running 'requirements_osx_brew_libs_install autoconf automake libtool pkg-config coreutils libyaml libksba readline zlib [email protected]',
please read /Users/lihe/.rvm/log/1668758532_ruby-3.0.0/package_install_autoconf_automake_libtool_pkg-config_coreutils_libyaml_libksba_readline_zlib_openssl@1.1.log
Requirements installation failed with status: 1.

不要慌, 按照提示把插件安装上即可:

brew install autoconf
brew install automake
brew install libtool
brew install libyaml
brew install libxslt
brew install libksba
brew install openssl

这时候还可能报错:

==> Installing dependencies for scrcpy: dav1d, frei0r, bdw-gc, libffi, pkg-config, guile, libtasn1, nettle, p11-kit, libevent, unbound, gnutls, lame, fribidi, pcre, [email protected], glib, libpthread-stubs, xorgproto, libxau, libxdmcp, libxcb, libx11, libxext, libxrender, lzo, pixman, cairo, gobject-introspection, graphite2, harfbuzz, libass, libbluray, cjson, cmocka, mbedtls, librist, libsoxr, libvidstab, libogg, libvorbis, libvpx, opencore-amr, little-cms2, openjpeg, opus, rav1e, flac, libsndfile, libsamplerate, rubberband, sdl2, snappy, speex, srt, leptonica, libb2, lz4, libarchive, tesseract, theora, x264, x265, xvid, zeromq, zimg, ffmpeg and libusb
==> Installing scrcpy dependency: dav1d
fatal: not in a git directory
Error: Command failed with exit 128: git

这时候通过:

brew -v

查看是否有如下信息:

Homebrew 3.6.11-25-gebe4447
fatal: detected dubious ownership in repository at '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-core'
To add an exception for this directory, call:

    git config --global --add safe.directory /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core
Homebrew/homebrew-core (no Git repository)
fatal: detected dubious ownership in repository at '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask'
To add an exception for this directory, call:

    git config --global --add safe.directory /usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask
Homebrew/homebrew-cask (no Git repository)

如果出现以上信息的话,只需要安装上面的提示执行以下命令:

git config --global --add safe.directory /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core
git config --global --add safe.directory /usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask

然后再通过brew install xxx 安装插件即可.

  1. 检测是否存在冲突
$ brew doctor
  1. 检查是否安装成功, 出现版本号就成功了.
$ brew --version
  1. 返回1.51.6步骤安装ruby, 完成后再通过1.8步骤安装cocoapods

1.8 安装Cocoapods (步骤有点慢,不要急)

1. 坑点:
使用$  sudo gem install cocoapods安装cocoapods 极有可能报error: RPC failed / early EOF

2. 正确的使用方法:
A. 看到报这个错之后,需要在终端执行$  sudo xcode-select -switch  /Applications/Xcode.app/Contents/Developer 这句,具体如下: 先找到xcode,显示包内容,在Contents里找到Developer文件,然后在终端输入sudo xcode-select -switch ,把找到的Developer文件夹拖进终端,就得到后边的路径啦,然后执行。因为xcode位置和版本安装的不一样,可能路径会有所不同。我的最终是sudo xcode-select -switch  /Applications/Xcode.app/Contents/Developer 这个。
B. 执行$ sudo gem install -n /usr/local/bin cocoapods 
C. Git clone https://git.coding.net/CocoaPods/Specs.git ~/.cocoapods/repos/master

然后就等待吧,下载完就ok了.

2. 离线安装RVM方式

  • 官方离线安装教程
// 离线包
curl -sSL https://github.com/rvm/rvm/tarball/stable -o rvm-stable.tar.gz
// 创建文件夹
mkdir rvm && cd rvm
// 解包
tar --strip-components=1 -xzf ../rvm-stable.tar.gz
// 安装 
./install --auto-dotfiles
// 加载
source ~/.rvm/scripts/rvm
// if --path was specified when instaling rvm, use the specified path rather than '~/.rvm'

// 查询 ruby的版本
rvm list known

在查询 ruby的版本时可能会出现下面的错误:A RVM version () is installed yet 1.25.14 (master) is loaded.Please do one of the following:* 'rvm reload'* open a new shell* 'echo rvm_auto_reload_flag=1 >> ~/.rvmrc' # for auto reload with msg.* 'echo rvm_auto_reload_flag=2 >> ~/.rvmrc' # for silent auto reload.


解决办法: sudo rm -rf /users/your_username/.rvmThen close and reopen the terminal.
然后重新打开终端即可.

3. [!] CDN: trunk URL couldn't be downloaded:

CocoaPods 1.8 版本之后的一些说明!

我的解决方法

// 在podfile 文件中添加 (选一个就行)
source 'https://github.com/CocoaPods/Specs.git'
或
source 'https://cdn.cocoapods.org/'

.End

你可能感兴趣的:(iOS-Cocoapods 的正确安装姿势)