Macos 安装 React Native 环境踩坑

542 阅读2分钟

因为要安装react-native环境,所以要安装ruby。本机的ruby太老了,算是我几年前安装的版本,看了下 rn官方推荐的链接 ,是 2.7.6。所以更新下ruby版本,执行如下代码

brew update

brew install ruby

结果当然是没有任何反应。查阅网站资料,因为源在国外,所以推荐替换成国内源,有如下可供选择:

  • 阿里云
  • 中科大
  • 中科院

1、更换homebrew源

这里网上推荐用阿里云的源,但是我一安装就报 416的莫名错误。中科大的源会报错资源地址404。最后使用中科院的源搞定了

# 替换各个源 
$ git -C "$(brew --repo)" remote set-url origin https://mirrors.ustc.edu.cn/brew.git 
$ git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git 
$ git -C "$(brew --repo homebrew/cask)" remote set-url origin https://mirrors.ustc.edu.cn/homebrew-cask.git 
# zsh 替换 brew bintray 镜像 
$ echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.zshrc 
$ source ~/.zshrc 
# bash 替换 brew bintray 镜像 
$ echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.bash_profile 
$ source ~/.bash_profile 
# 刷新源 
$ brew update

其中看好自己是 zshrc 还是 bash 这个步骤跟 macOS 系统使用的 shell 版本有关系,先查看 shell 版本

$ echo $SHELL

会输出 

/bin/zsh

或

/bin/bash

还可以重置

# 重置 brew.git 为官方源
$ git -C "$(brew --repo)" remote set-url origin https://github.com/Homebrew/brew.git

# 重置 homebrew-core.git 为官方源
$ git -C "$(brew --repo homebrew/core)" remote set-url origin https://github.com/Homebrew/homebrew-core.git

# 重置 homebrew-cask.git 为官方源
$ git -C "$(brew --repo homebrew/cask)" remote set-url origin https://github.com/Homebrew/homebrew-cask

# zsh 注释掉 HOMEBREW_BOTTLE_DOMAIN 配置
$ vi ~/.zshrc
# export HOMEBREW_BOTTLE_DOMAIN=xxxxxxxxx

# bash 注释掉 HOMEBREW_BOTTLE_DOMAIN 配置
$ vi ~/.bash_profile
# export HOMEBREW_BOTTLE_DOMAIN=xxxxxxxxx

# 刷新源
$ brew update

尝试安装, 发现失败:

Error: Failure while executing; `tar --extract --no-same-owner --file 。。。

解决方案:临时将brew的根域名换掉

export HOMEBREW_BOTTLE_DOMAIN=''

GitHub无法访问、443 Operation timed out的解决办法

下载github的资源经常报 443 ssl问题,这里的解决方案是 查询对应域名的ip,然后手动设置host

具体操作可以阅读 发烂渣这篇文章

注意的是,域名解析不是一成不变的,我昨天设置好的,第二天又不行了,一看ip,又变了(꒦_꒦)

这里推荐 switchhosts 软件,改起来比较方便

安装ruby

# 可能需要等一会
curl -L get.rvm.io | bash -s stable 
# 编译一下,自动安装最新版
source ~/.rvm/scripts/rvm

这样就安装好了,我试过 rbenv 安装,但是 不能全局生效,重启命令行后失效

CocoaPods 安装依赖卡住

image.png

解决办法:

对于旧版的 CocoaPods 可以使用如下方法使用 tuna 的镜像:

pod repo remove master
pod repo add master https://mirrors.tuna.tsinghua.edu.cn/git/CocoaPods/Specs.git
pod repo update

新版的 CocoaPods 不允许用pod repo add直接添加master库了,但是依然可以:

cd ~/.cocoapods/repos
pod repo remove master
git clone https://mirrors.tuna.tsinghua.edu.cn/git/CocoaPods/Specs.git master

最后进入自己的工程,在自己工程的podFile第一行加上:
source 'https://mirrors.tuna.tsinghua.edu.cn/git/CocoaPods/Specs.git'

安装 hermes-engine 时卡住了,436M 国外的maven源,太慢了,最后在同事的指导下,开了公司提供的网络加速搞定 😂

自己的电脑 安装环境时,始终有些下载不行

这里经过大佬指点,原来 Item命令行工具也是要 配置代理的,就算全局开了系统代理也不行

这里找了网上一篇文章,就不多写了