Skip to main content

万能解决方案之彻底解决cocoapods环境问题

·407 words·2 mins
IOS 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安装
#

官方网站:https://www.git-scm.com/download/

如果是iOS开发者,可以先装一个xcode,自带git环境安装。

默认系统是没有git的,我们也可以手动先安装一个。

我是macOS系统,https://www.git-scm.com/download/mac

这里有很多种方式,我们选择Binary installer二进制安装文件,点击installer,进入https://sourceforge.net/projects/git-osx-installer/files/,选择最新的版本2.33.0。

下载安装即可。

三、Homebrew安装
#

Homebrew官网: https://brew.sh/

打开终端命令输入:

/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版本看看是否安装成功。

四、Ruby安装
#

Ruby官网: https://www.ruby-lang.org/zh_cn/downloads/

RVM方式安装
#

RVM(Ruby Version Manager)是一个命令行工具,用于管理多个 Ruby 版本和 Gem 集。它允许你在同一台机器上安装、管理和切换不同的 Ruby 版本,并为每个项目创建独立的 Gem 环境。

  1. 安装命令
curl -L https://get.rvm.io | bash -s stable
  1. 载入RVM环境
source ~/.rvm/scripts/rvm
  1. 查看rvm版本
rvm -v

能输出版本信息说明安装完成。

  1. 列出已知版本
rvm list known
  1. 安装指定ruby版本
rvm install 3.0.0
  1. 查看ruby版本信息

xiaopin@PPM2-MacBook-Air ~ % ruby -v ruby 3.0.0p0 (2020-12-25 revision 95aff21468) [arm64-darwin23]

说明安装成功!

brew方式安装(推荐)
#

RVM方式好像停止更新一样,无法保持Ruby版本最新兼容,这次macOS 15.0系统,RVM方式的版本不兼容新系统。

  • 使用Brew安装最新的Ruby版本
brew install ruby
  • 修改Shell配置文件,
#Intel芯片(目前来说可以抛弃了,现在基本都是苹果芯片)
echo 'export PATH="/usr/local/opt/ruby/bin:$PATH"' >> ~/.bash_profile
source ~/.bash_profile
#苹果M系列芯片
echo 'export PATH="/opt/homebrew/opt/ruby/bin:$PATH"' >> ~/.zshrc
source ~/.zshrc

也可以手动的的找到.zshrc文件,一般在 Users/用户名/.zshrc 目录下,用文本编辑器打开,在最后面加上

#如果是RVM安装的会自动有这行,可以不管,也可以注释
export PATH="$PATH:$HOME/.rvm/bin"
#在后面加上这行,会自动的将默认环境指向homebrew的ruby版本
export PATH="/opt/homebrew/opt/ruby/bin:$PATH"
  • 验证安装
#查看默认ruby路径
which ruby
#查看ruby版本
ruby -v
#ruby 3.3.5 (2024-09-03 revision ef084cc8f4) [arm64-darwin24]

五、升级gem和更换镜像源
#

Gems 是 Ruby 的包管理系统(RubyGems)用来分发和管理 Ruby 库和应用程序的包。Gems 可以包含库、应用程序、文档和其他资源,方便开发者在项目中使用和管理依赖。

  1. 升级gem命令
sudo gem update --system
  1. 更换镜像源

​ 移除旧源

gem sources --remove  https://rubygems.org/

​ 添加新源

gem sources -a https://gems.ruby-china.com

​ 查看镜像源信息

gem sources -l

六、彻底卸载cocoapods
#

  1. 卸载已有旧版本
sudo gem uninstall cocoapods
  1. 查看本地已安装的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)

  1. 逐个删除
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异常解决
#

  1. 尝试删除
rm ~/Library/Caches/CocoaPods/search_index.json
  1. 不行的话再逐步执行一下命令
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镜像源
#

  1. 查看本地源

    pod repo
    
  2. 修改默认镜像

    国内网络限制,github源下载麻烦,可以切换默认master源 清华源:https://mirrors.tuna.tsinghua.edu.cn/git/CocoaPods/Specs.git gitee源:https://gitee.com/mirrors/CocoaPods-Specs.git

    官方cdn: https://cdn.cocoapods.org/

    以Gitee为例:

    pod repo remove master
    pod repo add master https://gitee.com/mirrors/CocoaPods-Specs.git
    pod repo update
    
  3. 修改Podfile

    source 'https://gitee.com/mirrors/CocoaPods-Specs.git'
    
  4. 也可以用官方CDN源,Podfile文件默认不设置source(推荐)

    source 'https://cdn.cocoapods.org/'
    
Pin
Author
Pin
A little bit about you