MacOS 安装 Homebrew

目录

1 首先需要安装Command Line Tools (CLT) for Xcode

 2 安装Homebrew

3 Apple Silicon CPU 设备上的 macOS 系统

4 替换现有仓库


1 首先需要安装Command Line Tools (CLT) for Xcode

安装Command Line Tools (CLT) for Xcode的目的是为MacOS添加命令工具:git 和 curl

打开Mac的终端

 在终端中输入命令,安装Command Line Tools (CLT) for Xcode

xcode-select --install

 

 2 安装Homebrew

在终端输入以下几行命令设置环境变量,设定使用清华Homebrew镜像源:

export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git"
export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git"
export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles"

然后,在终端运行以下命令以安装 Homebrew:

git clone --depth=1 https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/install.git brew-install
/bin/bash brew-install/install.sh
rm -rf brew-install

3 Apple Silicon CPU 设备上的 macOS 系统

如果您设备是Apple Silicon CPU 设备上的 macOS 系统,则需要设置进行如下设置,如果是Intel CPU 系统则跳过这步。

在终端输入命令检查处理器类型:

uname -m

显示结果

arm64

则说明您使用的是 Apple Silicon CPU 设备上的 macOS 系统(ARM 处理器)需要进行如下设置:

test -r ~/.bash_profile && echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.bash_profile
test -r ~/.zprofile && echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zprofile
test -r ~/.zshrc && echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zshrc

|对基于 Intel CPU 设备上的 macOS 系统(命令行运行 uname -m 应输出 x86_64)的用户可跳过本步。

4 替换现有仓库

Homebrew默认仓库在境外,访问非常慢,替换为国内镜像仓库可以大大加快访问速度,这里设置是是清华镜像仓库,可以替换 brew 程序本身的源:

export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git"
for tap in core cask{,-fonts,-drivers,-versions} command-not-found; do
    brew tap --custom-remote --force-auto-update "homebrew/${tap}" "https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-${tap}.git"
done
brew update

然后设置系统环境变量,如果用户设置了环境变量 HOMEBREW_BREW_GIT_REMOTE 和 HOMEBREW_CORE_GIT_REMOTE,则每次执行 brew update 时,brew 程序本身和 Core Tap (homebrew-core) 的远程将被自动设置。推荐用户将这两个环境变量设置加入 shell 的 profile 设置中:

test -r ~/.bash_profile && echo 'export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git"' >> ~/.bash_profile  # bash
test -r ~/.bash_profile && echo 'export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git"' >> ~/.bash_profile
test -r ~/.bash_profile && echo 'export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles"' >> ~/.bash_profile
test -r ~/.profile && echo 'export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git"' >> ~/.profile
test -r ~/.profile && echo 'export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git"' >> ~/.profile
test -r ~/.profile && echo 'export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles"' >> ~/.profile
test -r ~/.zprofile && echo 'export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git"' >> ~/.zprofile
test -r ~/.zprofile && echo 'export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git"' >> ~/.zprofile
test -r ~/.zprofile && echo 'export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles"' >> ~/.zprofile
test -r ~/.zshrc && echo 'export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git"' >> ~/.zshrc
test -r ~/.zshrc && echo 'export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git"' >> ~/.zshrc
test -r ~/.zshrc && echo 'export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles"' >> ~/.zshrc

如果您熟悉 vim 等编辑名,您可以手动编辑 .zprofile .profile 和 .bash_profile文件。

如果您使用的是 zsh 编辑.zprofile即可

brew的相关指令

brew help查看帮助

brew -v查看版本

brew update更新brew

brew install <包>安装

brew uninstall <包名>卸载

brew outdated查询可更新的包

brew upgrade 全部更新包
brew upgrade 包名 指定包更新包

brew cleanup清理旧版本

brew info 包名查看包信息

brew list查看安装列表

brew search <包名>查询可用包

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值