Mac配置Ruby环境和安装Homebrew和CocoaPods

12,213 阅读6分钟

安装ruby环境

安装RVM

RVM 是一个命令行工具,可以提供一个便捷的多版本 Ruby 环境的管理和切换

1、打开终端安装rvm,安装代码

curl -L get.rvm.io | bash -s stable

  • 如果这边有挂vpn科学上网可能会遇见如下问题:
curl: (7) Failed to connect to raw.githubusercontent.com port 443 after 5 ms: Couldn't connect to server
  • 将您的 DNS 服务器设置为8.8.8.8,如下图所示:

2、查看rvm是否安装成功

rvm -v

备注:如果出现查找不到的情况,可以关闭终端再次打开

成功会看到如下:

rvm 1.29.12 (latest) by Michal Papis, Piotr Kuczynski, Wayne E. Seguin [https://rvm.io]

安装Homebrew

Mac上的软件包管理工具

1、安装《 国内 OR 国外选择一种即可 》

  • 安装国内版本
/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"
  • 安装GitHub国外版本
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

备注:这里需要输入你的电脑密码进行安装,然后就是漫长的等待。最好挂个代理下载的快。

2、查看是否安装成功

brew -v

成功大致如下:

Homebrew 4.0.15-97-ge191b82

3、卸载

  • 卸载国内版本
/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/HomebrewUninstall.sh)"
  • 卸载GitHub国外版本
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/uninstall.sh)"

更多关于Homebrew查看:www.jianshu.com/p/6729a6b68…

安装Ruby环境

1、列出已有的ruby:

rvm list known

2、安装

rvm install ruby-3.0.0   # 我选择安装的最新版本

成功大致如下:
ruby-3.0.0 - #adjusting #shebangs for (gem irb erb ri rdoc testrb rake).
Install of ruby-3.0.0 - #complete 
Ruby was built without documentation, to build it run: rvm docs generate-ri

安装成功后,我们让其在本地环境中生效:

rbenv shell 3.0.0

3、查询已经安装的ruby

rvm list

成功大致如下:
=* ruby-3.0.0 [ arm64 ]

# => - current
# =* - current && default
#  * - default

4、设置 Ruby 版本

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

rvm 3.0.0 --default

5、查看是否设置正确

ruby -v

大致如下:
ruby 3.0.0p0 (2020-12-25 revision 95aff21468) [x86_64-darwin20]

6、切换Source源,替换Ruby源

因为Ruby的默认源使用的是cocoapods.org,国内访问这个网址有时候会有问题,通常解决方案是将远替换成国内Ruby镜像

  • 删除原有源
gem source -r https://rubygems.org/
  • 更新新源
gem source -a https://gems.ruby-china.com
  • 验证是否替换成功
gem sources -l

输出如下结果表示替换成功:

*** CURRENT SOURCES ***

https://gems.ruby-china.com/

更多关于Ruby请查看:www.jianshu.com/p/36a7a354d…

安装Cocoapods

第一种:使用Homebrew安装,直接终端输入:

brew install cocoapods

成功大致如下:

Warning: Treating cocoapods as a formula. For the cask, use homebrew/cask/cocoapods
==> **Downloading https://ghcr.io/v2/homebrew/core/cocoapods/manifests/1.11.2_1**
Already downloaded: /Users/aba/Library/Caches/Homebrew/downloads/d6cda7b7f4a10f3f41627eef16cb4deb95fa5a828a64ad48b98bd46db3d6e97c--cocoapods-1.11.2_1.bottle_manifest.json
==> **Downloading https://ghcr.io/v2/homebrew/core/cocoapods/blobs/sha256:4eb89ca73f311a1e0c52a0b72d22**
Already downloaded: /Users/aba/Library/Caches/Homebrew/downloads/738e83a462b9a0cec78a37271267ae01966cc849a57449726dee8f426fa8739f--cocoapods--1.11.2_1.monterey.bottle.tar.gz
==> **Reinstalling** **cocoapods******
==> **Pouring cocoapods--1.11.2_1.monterey.bottle.tar.gz**
🍺  /usr/local/Cellar/cocoapods/1.11.2_1: 14,105 files, 29.8MB

查看cocoapods版本

pod --version

1.11.2  # 我的cocoapods版本

检查更新

  • 更新brew仓库,原理就是从github上拉去一下brew-core的最新代码
brew update
  • 检查更新
brew outdated cocoapods    # cocoapods (1.9.1) < 1.9.3

更新cocoapods

brew upgrade cocoapods

第二种:采用Gem安装,直接终端输入:

1、安装cocoapods

sudo gem install cocoapods

mac OS 10.11之后 
sudo gem install -n /usr/local/bin cocoapods

成功大致如下:

Fetching cocoapods-1.11.2.gem
Successfully installed cocoapods-1.11.2
Parsing documentation for cocoapods-1.11.2
Installing ri documentation for cocoapods-1.11.2
Done installing documentation for cocoapods after 2 seconds
1 gem installed

如果出现如下情况

ERROR:  While executing gem ... (Gem::FilePermissionError)
    You don't have write permissions for the /usr/bin directory.

解决方案:

sudo gem install cocoapods -n /usr/local/bin

如果还不行可以再试试

sudo gem install cocoapods --user-install

2、将Github上的Pod托管的Podspec索引都安装到本地

pod setup
  • 如果上述命令执行后什么都没做,则自己手动添加索引库
pod repo add master https://github.com/CocoaPods/Specs.git

如果出现该问题,克隆失败,其实就是克隆CocoaPods索引库到本地

Cloning spec repo `master` from `https://github.com/CocoaPods/Specs.git`
[!] /usr/local/bin/git clone https://github.com/CocoaPods/Specs.git -- master
\
正克隆到 'master'...
fatal: 无法访问 'https://github.com/CocoaPods/Specs.git/':LibreSSL SSL_connect: Operation timed out in connection to github.com:443
  • 方案一:该问题就说明网络不好,则再次执行上述命令;
  • 方案二:Cocoapods的GitHub下载zip,然后解压到~/.cocoapods/repos/master目录下,没有则创建一个master文件夹。 备注:command + G,输入文件目录;

3、查看索引是否配置成功

pod repo list

cocoapods
- Type: git (remotes/origin/master)
- URLhttps://github.com/CocoaPods/Specs.git
- Path: /Users/aba/.cocoapods/repos/cocoapods

master 
- Type: git (master) 
- URL: https://github.com/CocoaPods/Specs.git 
- Path: /Users/aba/.cocoapods/repos/master

如果执行pod repo list查询失败,则重新安装cocoapods

sudo gem uninstall cocoapods && sudo gem install cocoapods -- --enable-libffi-alloc

如果需要制作库上传到Cocoapods,则还需要加入trunk索引源

pod repo add trunk https://cdn.cocoapods.org/

卸载CocoaPods

移除pod组件

1、查看pod位置

which pod

2、删除

sudo rm -rf 路径地址 

备注:-rf表示递归和强制,切记不要乱使用rm -rf /,否则你的系统就全没了,哈哈哈!!!

3、查看是否卸载成功

pod version

移除RubyGems中的CocoaPods程序包

1、查看gem当中的程序包

gem list

大致会看到如下结果

2、移除程序包

sudo gem uninstall cocoapods

移除指定程序包

sudo gem uninstall cocoapods -v 0、1.9.3

Cocoapods使用时候遇见的问题

1、执行pod install or pod update出现以下错误:

[!] CocoaPods was not able to update the `cocoapods` repo. If this is an unexpected issue and persists you can inspect it running `pod repo update --verbose`

解决方案:先执行rm -rf ~/.cocoapods/repos,然后在执行上述操作。

2、执行pod install出现以下错误:

[!] Error installing Mantis
[!] /usr/bin/git clone https://github.com/guoyingtao/Mantis.git /var/folders/99/7x38x27970qfg1thbjmbbh9r0000gn/T/d20240119-85029-11ujtwl --template= --single-branch --depth 1 --branch v2.18.0

Cloning into '/var/folders/99/7x38x27970qfg1thbjmbbh9r0000gn/T/d20240119-85029-11ujtwl'...
fatal: unable to access 'https://github.com/guoyingtao/Mantis.git/': Failed to connect to github.com port 443 after 75003 ms: Couldn't connect to server

如果设置过代理,可能本机系统代理端口和git端口不一致导致
1.查看当前用户配置

git config --global  --list

2.配置全局代理

git config --global http.proxy http://127.0.0.1:8118
git config --global https.proxy http://127.0.0.1:8118

WX20240123-170316.png

这里需要注意的是端口(8118)需要和代理软件端口一致,然后网络连接里面的端口也需要保持一致;

WX20240207-154550.png

3、全局代理导致提交代码失败

fatal: unable to access 'https://git.mpbk-ios.git/': Proxy CONNECT aborted
  • 查看是否设置过全局代理
git config --global http.proxy
git config --global https.proxy

解决方案: 取消全局代理

git config --global --unset http.proxy
git config --global --unset https.proxy

安装Xcode插件

插件包Alcatraz

1、安装插件包

mkdir -p ~/Library/Application\Support/Developer/Shared/Xcode/Plug-ins;curl -L http://git.io/lOQWeA | tar xvz -C ~/Library/Application\Support/Developer/Shared/Xcode/Plug-ins

2、卸载

rm -rf ~/Library/Application\Support/Developer/Shared/Xcode/Plug-ins/Alcatraz.xcpluginrm -rf ~/Library/Application\Support/Alcatraz

GitHub配置SSH keys

生成SSH keys

1、打开终端执行如下命令:

ssh-keygen -t rsa -C "your_email@example.com"

2、配置密码流程,不过我一般都不会设置密码;
如果设置了密码每次提交代码都要让你输入密码,因此直接回车完事

Generating public/private rsa key pair.

Enter file in which to save the key (/Users/mac/.ssh/id_rsa): 

Enter passphrase (empty for no passphrase): 

Enter same passphrase again: 

Your identification has been saved in /Users/mac/.ssh/id_rsa

Your public key has been saved in /Users/mac/.ssh/id_rsa.pub

3、获取SSH key

pbcopy < ~/.ssh/id_rsa.pub

此时已将密钥复制在剪切板,直接去GitHub里面Settings->SSH and GPG keys -> New SSH key -> Key 粘贴进去保存即可。

4、运行命令

ssh -T git@github.com

5、查看本地SSH密钥

ls -al ~/.ssh

6、删除本地SSH密钥

rm ~/.ssh/被删除密钥名

例如:rm ~/.ssh/id_ecdsa.pub