M1芯片的Mac安装homebrew

本文详细介绍了在M1芯片的Mac上安装Homebrew的步骤,包括设置ARM版Homebrew、安装过程、镜像源切换、问题解决方法以及如何卸载Homebrew。特别强调了使用中科大和清华大学镜像源来避免访问官方脚本地址失败的问题,并提供了多版本Homebrew共存的配置方法。
摘要由CSDN通过智能技术生成

因为最近入手一个新款的Mac,M1芯片的,安装了好久的homebrew,一直出现问题,也是参考了好多的文章,现在终于安装成功了,下面给大家分享一下安装的教程!!!

====================================================================

本文简单的介绍了M1芯片Mac上安装homebrew的方法,

在这里插入图片描述

一、脚本说明

Homebrew是一款包管理工具,目前支持macOSlinux系统。主要有四个部分组成: brewhomebrew-corehomebrew-caskhomebrew-bottles

(1)安装 ARM 版 Homebrew

ARM版Homebrew需要安装在/opt/homebrew路径下,早期的时候需要手动创建目录执行命令,目前使用最新脚本不需要手动操作。

直接执行:Homebrew默认安装脚本:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

如果你等待一段时间之后遇到下面提示,就说明无法访问官方脚本地址:

curl: (7) Failed to connect to raw.githubusercontent.com port 443: Connection refused

请按照下面 「安装说明」中步骤执行。

二、安装说明(建议先看一遍再操作)

(1)提前设置homebrew-core镜像源并执行:

安装脚本的好处是可稳定访问,安装默认使用中科大镜像:

# 中科大
HOMEBREW_CORE_GIT_REMOTE=https://mirrors.ustc.edu.cn/homebrew-core.git

/bin/bash -c "$(curl -fsSL https://cdn.jsdelivr.net/gh/ineo6/homebrew-install/install.sh)"

然后还需要设置下环境变量,在~/.zshrc或者~/.bashrc文件末尾追加下面代码。

export PATH=/opt/homebrew/bin:$PATH

注意:如果你确定使用的终端是zsh,那请编辑.zshrc文件,否则请操作.bashrc文件。

.bashrc为例,在终端执行下面代码可以直接追加内容并让文件生效:

echo export PATH=/opt/homebrew/bin:$PATH >> ~/.bashrc 

# 重启一下
source ~/.bashrc

在这里插入图片描述

如果命令执行中卡在下面信息:

==> Tapping homebrew/core
Cloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-core'...

请Control + C中断脚本执行如下命令:

cd "$(brew --repo)/Library/Taps/"
mkdir homebrew && cd homebrew
git clone git://mirrors.ustc.edu.cn/homebrew-core.git

cask 同样也有安装失败或者卡住的问题,解决方法也是一样:

cd "$(brew --repo)/Library/Taps/"
cd homebrew
git clone https://mirrors.ustc.edu.cn/homebrew-cask.git

如果出现successful就表示成功
在这里插入图片描述
成功执行之后继续执行前文的安装命令:

/bin/bash -c "$(curl -fsSL https://cdn.jsdelivr.net/gh/ineo6/homebrew-install/install.sh)"

最后看到==> Installation successful!就说明安装成功了。
在这里插入图片描述
最后更新下:

brew update

更新成功的话,如图所示:
在这里插入图片描述

如果更新的时候出现
zsh: command not found: brew

就执行下面这句脚本
/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"
在这里插入图片描述
然后按照下面的细节执行:
在这里插入图片描述
最终成功的截图,如图所示:
在这里插入图片描述

其他的安装形式
(1)安装 X86 版 Homebrew

因为目前很多软件包没有支持ARM架构,我们也可以考虑使用x86版的Homebrew。

在命令前面添加arch -x86_64,就可以按X86模式执行该命令,比如:

# 设置homebrew-core镜像
HOMEBREW_CORE_GIT_REMOTE=https://mirrors.ustc.edu.cn/homebrew-core.git

arch -x86_64 /bin/bash -c "$(curl -fsSL https://cdn.jsdelivr.net/gh/ineo6/homebrew-install/install.sh)"
(2)多版本共存

如果你同时安装了ARM和X86两个版本,那你需要设置别名,把命令区分开。

同样是.zshrc或者.bashrc里面添加:

alias abrew='arch -arm64 /opt/homebrew/bin/brew'
alias ibrew='arch -x86_64 /usr/local/bin/brew'

abrewibrew可以根据你的喜好自定义。

然后再执行source ~/.zshrcsource ~/.bashrc命令更新文件。

三、设置镜像

brewhomebrew/core是必备项目,homebrew/caskhomebrew/bottles按需设置。

通过 brew config 命令可以查看相关配置信息。

3.1 中科大源
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

brew update

# 长期替换homebrew-bottles
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.bash_profile
source ~/.bash_profile

注意bottles可以临时设置,在终端执行下面命令:

export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles
3.2 清华大学源
git -C "$(brew --repo)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git

git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git

git -C "$(brew --repo homebrew/cask)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask.git

brew update

# 长期替换homebrew-bottles
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles' >> ~/.bash_profile
source ~/.bash_profile
3.3 恢复默认源
git -C "$(brew --repo)" remote set-url origin https://github.com/Homebrew/brew.git

git -C "$(brew --repo homebrew/core)" remote set-url origin https://github.com/Homebrew/homebrew-core.git

git -C "$(brew --repo homebrew/cask)" remote set-url origin https://github.com/Homebrew/homebrew-cask.git

brew update
homebrew-bottles配置只能手动删除,将 ~/.bash_profile 文件中的 HOMEBREW_BOTTLE_DOMAIN=https://mirrors.xxx.com内容删除,并执行 source ~/.bash_profile。

至此,安装和设置操作都完成了。

四、其他相关

4.1 cask

目前cask是从GitHub上读取软件源,而GitHub Api对访问有限制,如果使用比较频繁的话,可以申请Api Token,然后在环境变量中配置到HOMEBREW_GITHUB_API_TOKEN

.bash_profile中追加:

export HOMEBREW_GITHUB_API_TOKEN=yourtoken

注意:因为cask是从GitHub下载软件,所以目前是无法加速的。

4.2 如何卸载Homebrew

使用官方脚本同样会遇到uninstall地址无法访问问题,可以使用下面脚本:

/bin/bash -c "$(curl -fsSL https://cdn.jsdelivr.net/gh/ineo6/homebrew-install/uninstall.sh)"

五、总结

在前面的过程中我们把brewhomebrew-core的地址都指向到中科大镜像。

原理是通过修改install.sh脚本,在里面预设镜像地址。

STAT="stat --printf"
  CHOWN="/bin/chown"
  CHGRP="/bin/chgrp"
  GROUP="$(id -gn)"
  TOUCH="/bin/touch"
fi
# 这里替换了BREW_REPO
BREW_REPO="https://mirrors.ustc.edu.cn/brew.git"

更新后的方案使用HOMEBREW_CORE_GIT_REMOTE来预设homebrew-core源,不过这个设置方法很可能无效!!!

参考网站:

清华大学开源软件镜像站
中科大源

评论 20
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

经理,天台风好大

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值