homebrew 安装及配置为国内镜像

本文发布于:2024-08-17,最后更新于:2025-02-22,如果内容失效请留言告知。
目录
[隐藏]

1 MacOS 安装 homebrew

首先设置二进制下载相关环境变量。编辑配置文件 vi ~/.bash_profile,增加如下内容:

export HOMEBREW_INSTALL_FROM_API=1
export HOMEBREW_API_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/api"
export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles"
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"

让环境变量设置在当前终端立即生效:

source ~/.bash_profile

然后执行一键安装脚本:

# 使用官方脚本
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

# 使用国内 gitee 镜像脚本
/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"
# 卸载 homebrew:
/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/HomebrewUninstall.sh)"

2 homebrew 设置为使用国内源

设置为清华大学镜像仓库。执行如下命令即可:

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

# 更新 homebrew
brew update-reset

3 homebrew 常用命令参考:

  • 安装软件:brew install node
  • 卸载软件:brew uninstall node
  • 更新软件:brew upgrade node
  • 查看已安装的软件:brew list
  • 查看软件信息:brew info node
  • 查看软件的安装路径:brew --prefix node
  • 查看软件的版本:brew list --versions node
  • 查看软件的依赖关系:brew deps node
  • 查看软件的安装日志:brew log node

4 常见问题及解决

4.1 禁用自动更新和自动清理

当使用 brew 命令安装一个新的软件时,你可能会看到它会先执行 brew update。如果是很久没有执行过,而且已经通过 brew 安装了许多软件,那么这个过程会比较长,因为它还会检查所有已安装的软件是否过期并更新为最新版本。如果你不希望它自动的做这些动作,通过手动方式操作,可以在 ~/.bash_profile 中加入如下环境变量设置:

# 默认不执行 brew update
export HOMEBREW_NO_AUTO_UPDATE=1
# 默认不执行 brew cleanup
export HOMEBREW_NO_INSTALL_CLEANUP=1

如果你不希望禁用自动更新,也可以设置如下环境变量让自动更新检查的间隔久一些:

# 设置自动更新执行的时间间隔(秒)
export HOMEBREW_AUTO_UPDATE_SECS=13600
点赞 (0)

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

Captcha Code