M1 Pro新电脑安装相关环境并配置cocoapods

1,006 阅读4分钟

首先贴一下我的相关配置2022.4.12 image.png image.png

M1上运行Xcode更改为Rosetta 访达-应用程序-Xcode-右键-显示简介-通用-使用Rosetta打开 image.png

一. 安装brewcmakepip直接看这里

二. 安装Ruby

Mac电脑自带Ruby环境,但是版本一般比较低,为了避免在安装过程中出现问题,先对Ruby进行升级。

1. 检查当前Ruby版本

 ruby -v

笔者更新后版本是:

ruby 3.0.0p0 (2020-12-25 revision 95aff21468) [arm64-darwin21]

2. 安装rvm,升级Ruby

 // 下载rvm稳定版,需要等待几分钟
 curl -L get.rvm.io | bash -s stable

 // 设置shell和profile,bash和zsh二选一
 // bash
 source ~/.bashrc
 source ~/.bash_profile

 // zsh,新版系统默认shell都是zsh
 source ~/.zshrc
 source ~/.profile

3. 查看rvm可安装的版本

rvm list known

笔者显示如下:

# MRI Rubies
[ruby-]1.8.6[-p420]
[ruby-]1.8.7[-head] # security released on head
[ruby-]1.9.1[-p431]
[ruby-]1.9.2[-p330]
[ruby-]1.9.3[-p551]
[ruby-]2.0.0[-p648]
[ruby-]2.1[.10]
[ruby-]2.2[.10]
[ruby-]2.3[.8]
[ruby-]2.4[.10]
[ruby-]2.5[.8]
[ruby-]2.6[.6]
[ruby-]2.7[.2]
[ruby-]3[.0.0]

ruby-head
# for forks use: rvm install ruby-head-<name> --url https://github.com/github/ruby.git --branch 2.2

# JRuby
jruby-1.6[.8]
jruby-1.7[.27]
jruby-9.1[.17.0]
jruby[-9.2.14.0]
jruby-head

# Rubinius
rbx-1[.4.3]
rbx-2.3[.0]
rbx-2.4[.1]
rbx-2[.5.8]
rbx-3[.107]
rbx-4[.20]
rbx-5[.0]
rbx-head

# TruffleRuby
truffleruby[-20.3.0]

# Opal
opal

# Minimalistic ruby implementation - ISO 30170:2012
mruby-1.0.0
mruby-1.1.0
mruby-1.2.0
mruby-1.3.0
mruby-1[.4.1]
mruby-2.0.1
mruby-2[.1.1]
mruby[-head]

# Ruby Enterprise Edition
ree-1.8.6
ree[-1.8.7][-2012.02]

# Topaz
topaz

# MagLev
maglev-1.0.0
maglev-1.1[RC1]
maglev[-1.2Alpha4]
maglev-head

# Mac OS X Snow Leopard Or Newer
macruby-0.10
macruby-0.11
macruby[-0.12]
macruby-nightly
macruby-head

# IronRuby
ironruby[-1.1.3]
ironruby-head

选择3.0.0安装,首先查看ruby源,更改成国内源。

4. 查看ruby

由于国外Ruby源较慢,我们更换成ruby-china的源。

gem sources -l

5. 更换源

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

gem sources -l查看下:
*** CURRENT SOURCES ***
gems.ruby-china.com/

6. 选择一个Ruby版本安装

rvm install 3.0.0

7. 设置为默认版本

rvm use 3.0.0 --default

三. 安装cocoapods

不需要arch -x86_64 xxx这个命令.

1、install cocoapods

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

2、选择Xcode版本

如果安装了多个Xcode版本,使用下面的命令选择(推荐选择最近的Xcode版本)。

sudo xcode-select -switch /Applications/Xcode.app/Contents/Developer

3、安装本地库

pod setup

Mac OS Catalina开始,终端执行上面命令后就直接结束了。

没关系,我们手动安装本地库就可以了。

// 国外地址,如果你能科学上网的话,可以使用这个方法
git clone https://github.com/CocoaPods/Specs.git ~/.cocoapods/repos/trunk

// 清华大学镜像,速度杠杠的,推荐使用
git clone https://mirrors.tuna.tsinghua.edu.cn/git/CocoaPods/Specs.git  ~/.cocoapods/repos/trunk

4、检查是否可用

执行pod search命令,检查是否可用。

pod search AFNetworking

5、CocoaPods使用

  • 新建一个Xcode工程,使用终端cd到工程目录下。
  • 创建Podfile文件。
pod init
  • 打开Podfile文件,添加AFNetworking,保存并退出。
open Podfile

pod 'AFNetworking'
  • 开始下载。
pod install

下载完成之后会生成.xcworkspace文件,以后打开项目都使用这个文件。

6、一些简单pod命令

安装

pod install

升级

pod update

不更新已有库安装和升级

pod install --verbose --no-repo-update
pod update --verbose --no-repo-update

遇到的常见问题

希望评论区也能贴出遇到的问题或解决方案,我会择机同步到此文档,帮助更多童鞋。

ruby和brew不兼容或者ruby版本低的问题

安装完执行pod install时候报错:

# can't find gem cocoapods (>= 0.a) with executable pod (Gem::GemNotFoundException)

提示找不到 gem 可执行文件

解决办法:

  1. 重新安装 ruby 环境(默认安装最新版本)
     rvm reinstall ruby --disable-binary
    
    运行结果:
    mruby-1.3.0 - #removing src/mruby-1.3.0 - please wait
    mruby-1.3.0 - #removing rubies/mruby-1.3.0 - please wait
    RVM does not have prediction for required space for mruby-1.3.0, assuming 150MB should be enough, let us know if it was not.
    Checking requirements for osx.
    Certificates bundle '/usr/local/etc/openssl@1.1/cert.pem' is already up to date.
    Requirements installation successful.
    Installing Ruby from source to: /Users/jack/.rvm/rubies/mruby-1.3.0, this may take a while depending on your cpu(s)...
    mruby-1.3.0 - #downloading 1.3.0, this may take a while depending on your connection...
    mruby-1.3.0 - #extracting 1.3.0 to /Users/jack/.rvm/src/mruby-1.3.0 - please wait
    mruby-1.3.0 - #compiling - please wait
    mruby-1.3.0 - #installing - please wait
    Install of mruby-1.3.0 - #complete
    Required ruby-2.7.0 is not installed.
    To install do: 'rvm install "ruby-2.7.0"'
    

只执行这一步,我这再试一下pod install就可以了。

如果还不可以就继续第2步。

  1. 重新安装 cocoapods
    gem install cocoapods
    
    运行结果:
    Successfully installed cocoapods-1.9.3
    Parsing documentation for cocoapods-1.9.3
    Done installing documentation for cocoapods after 1 seconds
    1 gem installed
    
    再重新执行 pod install 就可以了
  • Homebrew,是Mac OS X上的软件包管理工具,使用起来非常方便,安装任意软件包时 >brew 会自动下载其依赖;

  • RubyGems 提供了ruby社区gem的托管服务,主要用于下载、安装使用 ruby 软件包

  • 平常 iOS 开发使用 cocoapods 等工具都是使用 gems 进行安装管理,当使用 brew 安装软件包时有可能因依赖导致 ruby 环境错乱,不建议混合使用(使用 brew 也可以安装 cocoapods 而且很方便)

说明

本文是对几篇参照文章的整理以及自己安装过程遇到的问题并实测解决。参考文章如下: