Mac M1(Apple silicon) 安装 homebrew 和 nodejs 遇到的坑和解决办法

安装 Homebrew 和 Nodejs 对于程序员来说可能没什么难度,但是对我这个学习前端的设计师来说有点难度,另外考虑到 M1 更是难上加难,遇到了各种问题,求助搜索引擎和朋友,历时3个小时完成,为了避免后来人踩坑,特此记录遇到的问题和解决方案。

安装 homebrew

我是按少数派教程(链接:https://sspai.com/post/63935)来做的。参考多个教程的朋友注意有些命令是不能重复输入的,否则会出现奇奇怪怪的各种问题。

另外一定要先在 Mac Store 下载 Apple Developer 和 XCode,XCode 要先启动一次然后同意协议。


如果在输入:

1
2
curl -L <https://github.com/Homebrew/brew/tarball/master> | tar xz --strip 1 -C homebrew

出现了诸如:

1
2
3
4
tar: could not chdir to 'homebrew'
curl: (23) Failed writing body (0 != 1370)
curl: (6) Could not resolve host: homebrew


之类的错误,记得把目录切回到 /opt,即:

1
2
cd /opt

再执行 “curl”就没问题了。

安装 node.js


如果在遇到执行:

1
2
brew install node

出现诸如:

1
2
Updating Homebrew...

或者出现:

1
2
Cloning into '/opt/Homebrew/Library /Taps/homebrew/homebrew-core'...

等了好久还没反应的问题。这一般是国内连接 homebrew 服务器速度太慢导致的。解决办法是关掉 homebrew 的自动更新,然后把 homebrew 的服务器改为国内的镜像。

可以直接按 “control+c“ 结束当前终端的代码,然后进行下面的操作。

1
2
3
4
5
6
cd /opt/homebrew/Library/Taps
mkdir homebrew
cd homebrew
git clone <https://mirrors.ustc.edu.cn/homebrew-core.git>
git clone <https://mirrors.ustc.edu.cn/homebrew-cask.git>

关闭homebrew自动更新

在终端执行以下命令即可。

1
2
3
echo "export HOMEBREW_NO_AUTO_UPDATE=true" >> ~/.zshrc
source ~/.zshrc

替换 homebrew 源


如果按网上的教程,用:

1
2
3
cd "$(brew --repo)"
git remote set-url origin <https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git>

是肯定会报错:

1
2
fatal: No such remote 'origin'

解决办法是,在“cd”之后先执行:

1
2
git remote add origin <https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git>

再执行“set-url”就正常了。


如果遇到:

1
2
error: Not a valid ref: refs/remotes origin /master

试试“set-url”其他源试试。


如果遇到:

1
2
fatal: not a git repository (or any of the parent directories): .git

输入以下命令即可解决。

1
2
git init


如果遇到:

1
2
3
4
5
Error: Could not 'git stash' in /opt/homebrew!
Please stash/commit manually if you need to keep your changes or, if not, run:
cd /opt/homebrew
git reset --hard origin/master

其实解决办法就在报错命令里了:

1
2
3
cd /opt/homebrew
git reset --hard origin/master


如果执行:

1
2
cd "$(brew --repo)"/Library/Taps/homebrew/homebrew-cask

出现错误:

1
2
cd: no such file or directory: /opt/homebrew/Library/Taps/homebrew/cask

无视即可。可能是因为M1的配置和之前的不一样,没有 cask 这个目录。


执行:

1
2
3
4
5
cd /opt/homebrew/Library/Taps/
mkdir homebrew
cd homebrew
git clone <https://mirrors.ustc.edu.cn/homebrew-core.git>

正式安装 node

解决完以上问题,先输入以下命令更新 homebrew。

1
2
brew update

再输入以下命令装 node 应该就没问题了。

1
2
brew install node

node 即使安装成功,中间也会出现很多警告,这个可以无视,之后遇到问题了可以看警告中提供的解决方案。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
Warning: You are running macOS on a arm64 CPU architecture.
We do not provide support for this (yet).
Reinstall Homebrew under Rosetta 2 until we support it.
You will encounter build failures with some formulae.
Please create pull requests instead of asking for help on Homebrew's GitHub,
Twitter or any other official channels. You are responsible for resolving
any issues you experience while you are running this
unsupported configuration.

==> Downloading <https://mirrors.cloud.tencent.com/homebrew-bottles/bottles/icu4c>
######################################################################## 100.0%
Error: SHA256 mismatch
Expected: 0d03423f2a4a557fc04a021fad963ab71d05b9129693887522dde996aff8c9f9
Actual: e055a567ca6c52d45b0a07cd9c0182591ece5984b20a0a6f1d6dd2e8ed5d3d0a
File: /Users/dengjiangling/Library/Caches/Homebrew/downloads/7a6b1f9f807a76760bf7490facaa94b765c3c720fb218cabe5c3f329b3c85ba7--icu4c-67.1.arm64_big_sur.bottle.tar.gz
To retry an incomplete download, remove the file above.
Warning: Bottle installation failed: building from source.
==> Downloading <https://github.com/unicode-org/icu/commit/715d254a02b0b22681cb6f>
######################################################################## 100.0%
==> Downloading <https://github.com/unicode-org/icu/releases/download/release-67->
==> Downloading from <https://github-production-release-asset-2e65be.s3.amazonaws>
######################################################################## 100.0%
==> Downloading <https://mirrors.cloud.tencent.com/homebrew-bottles/bottles/node->
######################################################################## 100.0%
==> Installing dependencies for node: icu4c
==> Installing node dependency: icu4c
==> Pouring icu4c-67.1.arm64_big_sur.bottle.tar.gz
==> Caveats
icu4c is keg-only, which means it was not symlinked into /opt/homebrew,
because macOS provides libicucore.dylib (but nothing else).

If you need to have icu4c first in your PATH run:
echo 'export PATH="/opt/homebrew/opt/icu4c/bin:$PATH"' >> ~/.zshrc
echo 'export PATH="/opt/homebrew/opt/icu4c/sbin:$PATH"' >> ~/.zshrc

For compilers to find icu4c you may need to set:
export LDFLAGS="-L/opt/homebrew/opt/icu4c/lib"
export CPPFLAGS="-I/opt/homebrew/opt/icu4c/include"

验证安装结果

如果homebrew和node安装成功后,在终端里输入:

1
2
file /opt/homebrew/bin/node

如果看到:

1
2
/opt/homebrew/bin/node: Mach-O 64-bit executable arm64

“arm64”就说明安装的 node 确实是原生支持 M1 的版本。

另外不要因为安装了最新版的 node 沾沾自喜,为了稳定运行,建议安装 nvm 然后用 “arch -x86_64 zsh” 启动兼容模式安装 LTS 版本的node,否则会出现一堆后续问题,这个之后我再慢慢和大家讲。

总之 M1 原生模式还不适合做前端开发。

安装过程中如果遇到其他问题你解决了,记得写文章发表,便于后来者搜索寻找解决方案。

Mac M1(Apple silicon) 安装 homebrew 和 nodejs 遇到的坑和解决办法

https://www.ftium4.com/Mac-M1-Apple-silicon-installation-homebrew-and-nodejs-solutions.html

作者

龙爪槐守望者

发布于

2021-01-03

更新于

2021-06-04

许可协议

评论