万能解决方案之彻底解决macOS cocoapods环境的所有问题
一、Hosts配置
1.1 从Finder中配置显示Macintosh HD和用户目录
英文:打开Finder,点击顶部菜单Finder->Settings
中文:打开访达,点击顶部菜单访达->设置->边栏->勾选硬盘->勾选用户目录(房子图标)
1.2 修改配置 /private/etc/Hosts
进入Macintosh HD后,默认是看不到/private/etc/Hosts, 因为这些事系统的隐藏系统文件,按Command + Shift + . 组合命令,显示或隐藏文件。 找到/private/etc/Hosts,将Hosts文件复制到桌面再进行修改。
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
# GitHub Start
140.82.114.4 github.com
140.82.114.20 gist.github.com
151.101.184.133 assets-cdn.github.com
151.101.184.133 raw.githubusercontent.com
151.101.184.133 gist.githubusercontent.com
151.101.184.133 cloud.githubusercontent.com
151.101.184.133 camo.githubusercontent.com
151.101.184.133 avatars0.githubusercontent.com
199.232.68.133 avatars0.githubusercontent.com
199.232.28.133 avatars1.githubusercontent.com
151.101.184.133 avatars1.githubusercontent.com
151.101.184.133 avatars2.githubusercontent.com
199.232.28.133 avatars2.githubusercontent.com
151.101.184.133 avatars3.githubusercontent.com
199.232.68.133 avatars3.githubusercontent.com
151.101.184.133 avatars4.githubusercontent.com
199.232.68.133 avatars4.githubusercontent.com
151.101.184.133 avatars5.githubusercontent.com
199.232.68.133 avatars5.githubusercontent.com
151.101.184.133 avatars6.githubusercontent.com
199.232.68.133 avatars6.githubusercontent.com
151.101.184.133 avatars7.githubusercontent.com
199.232.68.133 avatars7.githubusercontent.com
151.101.184.133 avatars8.githubusercontent.com
199.232.68.133 avatars8.githubusercontent.com
# GitHub End
主要是将# GitHub Start 到 # GitHub End的内容添加进去,保存以后,在将Host文件复制替换到/private/etc/Hosts。
二、Git安装
如果没有安装xcode , 默认系统是没有git的,我们也可以手动先安装一个
官方下载地址:https://www.git-scm.com/download/
我是macOS系统,https://www.git-scm.com/download/mac
Download for macOS
There are several options for installing Git on macOS. Note that any non-source distributions are provided by third parties, and may not be up to date with the latest source release.
Choose one of the following options for installing Git on macOS:
Homebrew
Install homebrew if you don't already have it, then:
$ brew install git
MacPorts
Install MacPorts if you don't already have it, then:
$ sudo port install git
Xcode
Apple ships a binary package of Git with Xcode.
Binary installer
Tim Harper provides an installer for Git. The latest version is 2.33.0, which was released over 2 years ago, on 2021-08-30.
Building from Source
If you prefer to build from source, you can find tarballs on kernel.org. The latest version is 2.45.1.
Installing git-gui
If you would like to install git-gui and gitk, git's commit GUI and interactive history browser, you can do so using homebrew
$ brew install git-gui
这里有很多种方式,我们选择Binary installer二进制安装文件,点击installer,进入https://sourceforge.net/projects/git-osx-installer/files/,选择最新的版本2.33.0。
下载安装即可。
三、Homebrew安装
打开终端命令输入:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
或使用国内镜像更快
/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"
如果失败多试几次,可能是你的网络问题,安装完成以后命令行输入:brew -v ,查看homebrew版本看看是否安装成功。
四、RVM安装
- 安装命令
curl -L https://get.rvm.io | bash -s stable
- 载入RVM环境
source ~/.rvm/scripts/rvm
- 查看rvm版本
rvm -v
能输出版本信息说明安装完成。
五、用RVM安装Ruby
1.列出已知版本
rvm list known
2.安装指定ruby版本
rvm install 3.0.0
3.查看ruby版本信息
xiaopin@PPM2-MacBook-Air ~ % ruby -v
ruby 3.0.0p0 (2020-12-25 revision 95aff21468) [arm64-darwin23]
说明安装成功!
六、升级gem和更换镜像源
- 升级gem命令
sudo gem update --system
- 更换镜像源
移除旧源
gem sources --remove https://rubygems.org/
添加新源
gem sources -a https://gems.ruby-china.com
查看镜像源信息
gem sources -l
七、彻底卸载cocoapods
- 卸载已有旧版本
sudo gem uninstall cocoapods
- 查看本地已安装的cocoapods依赖
gem list --local | grep cocoapods
显示如下:
cocoapods (1.0.1)
cocoapods-core (1.0.1)
cocoapods-deintegrate (1.0.1)
cocoapods-downloader (1.1.1)
cocoapods-plugins (1.0.0)
cocoapods-search (1.0.0)
cocoapods-stats (1.0.0)
cocoapods-trunk (1.0.0)
cocoapods-try (1.1.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
...
八、Cocoapods安装
macOS 10.11以后系统的安装cocoapods 指令,可安装制定版本:
sudo gem install -n /usr/local/bin cocoapods
或
sudo gem install -n /usr/local/bin cocoapods -v 1.9.2
安装完成后,查看一下版本
pod --version
九、pod search异常解决
- 尝试删除
rm ~/Library/Caches/CocoaPods/search_index.json
- 不行的话再逐步执行一下命令
cd ~/.cocoapods/repos
pod repo remove master
git clone https://github.com/CocoaPods/Specs.git master
rm ~/Library/Caches/CocoaPods/search_index.json
pod search afnetworking
十、Cocoapods国内镜像源&Github镜像源
-
查看本地源
pod repo
- master
- Type: git (remotes/origin/master)
- URL: https://github.com/CocoaPods/Specs.git
- Path: /Users/xiaopin/.cocoapods/repos/master
- master
-
修改默认镜像
国内网络限制,github源下载麻烦,可以切换默认master源
清华源:https://mirrors.tuna.tsinghua.edu.cn/git/CocoaPods/Specs.git
gitee源:https://gitee.com/mirrors/CocoaPods-Specs.git以Gitee为例:
pod repo remove master pod repo add master https://gitee.com/mirrors/CocoaPods-Specs.git pod repo update
-
修改Podfile
source 'https://gitee.com/mirrors/CocoaPods-Specs.git'