Mac上安装Homebrew包管理器

Homebrew

它是 macOS 上的包管理器,用命令行安装、更新、卸载开发工具、命令行软件和部分图形应用,类似于Linux中的apt-get或yum。

安装xcode-select

对于 macOS 用户,系统自带 bash、git 和 curl,安装 CLT for Xcode 即可。

xcode-select --install

出现这行说明已经安装:xcode-select: note: Command line tools are already installed.

环境变量

Homebrew git repository

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"

Homebrew bottles

该镜像是 Homebrew 二进制预编译包的镜像(即 brew update 时所更新内容),请参考 Homebrew 镜像使用帮助

echo 'export HOMEBREW_PIP_INDEX_URL="https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple"' >> ~/.zprofile
echo 'export HOMEBREW_API_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/api"' >> ~/.zprofile
echo 'export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles"' >> ~/.zprofile

立即生效:

source ~/.zprofile

脚本安装

# 从镜像仓库安装
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

官方脚本:/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
科大脚本:/bin/bash -c "$(curl -fsSL https://mirrors.ustc.edu.cn/misc/brew-install.sh)"

程序路径

安装成功后需将 brew 程序的相关路径加入到环境变量中:

  • 以下针对基于 Apple Silicon CPU 设备上的 macOS 系统(命令行运行 uname -m 应输出 arm64)上的 Homebrew:

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

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

安装校验

brew -v

输出 Homebrew 5.x.x说明安装成功!

其他

文件权限问题

sudo mkdir -p /opt/homebrew
sudo chown -R $(whoami) /opt/homebrew

bottles镜像源

阿里云:

export HOMEBREW_PIP_INDEX_URL="http://mirrors.aliyun.com/pypi/simple"
export HOMEBREW_API_DOMAIN="https://mirrors.aliyun.com/homebrew/homebrew-bottles/api"
export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.aliyun.com/homebrew/homebrew-bottles"

科大源:

export HOMEBREW_PIP_INDEX_URL="http://mirrors.ustc.edu.cn/pypi/simple"
export HOMEBREW_API_DOMAIN="https://mirrors.ustc.edu.cn/homebrew-bottles/api"
export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.ustc.edu.cn/homebrew-bottles"

仓库目录说明

仓库类型 说明 默认地址
brew.git Homebrew 源代码仓库 https://github.com/Homebrew/brew
homebrew-core.git 核心软件包仓库 https://github.com/Homebrew/homebrew-core
homebrew-cask.git macOS 应用仓库 https://github.com/Homebrew/homebrew-cask
homebrew-bottles 预编译二进制包 https://ghcr.io/v2/homebrew

替换清华仓库

#设置Homebrew Git仓库地址
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"

#设置Homebrew 开发命令地址
brew tap --custom-remote homebrew/core https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git
brew tap --custom-remote homebrew/cask https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask.git

#更新Homebrew 配置
brew update

校验:

#查看tap源
brew tap

#查看git仓库
git -C "$(brew --repo)" remote -v

#查看包镜像
brew config | grep -E 'ORIGIN|HOMEBREW_API_DOMAIN|HOMEBREW_BOTTLE_DOMAIN|HOMEBREW_PIP_INDEX_URL|HOMEBREW_PREFIX'

复原官方仓库

(感谢 Snowonion Lee 提供说明)

  • 以下针对 macOS 系统上的 Homebrew

    # brew 程序本身,Homebrew / Linuxbrew 相同
    unset HOMEBREW_API_DOMAIN
    unset HOMEBREW_BREW_GIT_REMOTE
    git -C "$(brew --repo)" remote set-url origin https://github.com/Homebrew/brew
    
    # 以下针对 macOS 系统上的 Homebrew
    unset HOMEBREW_CORE_GIT_REMOTE
    BREW_TAPS="$(BREW_TAPS="$(brew tap 2>/dev/null)"; echo -n "${BREW_TAPS//$'\n'/:}")"
    for tap in core cask; do
        if [[ ":${BREW_TAPS}:" == *":homebrew/${tap}:"* ]]; then  # 只复原已安装的 Tap
            brew tap --custom-remote "homebrew/${tap}" "https://github.com/Homebrew/homebrew-${tap}"
        fi
    done
    
    # 重新拉取远程
    brew update
    

注:重置回默认远程后,用户应该删除 shell 的 profile 设置中的环境变量 HOMEBREW_BREW_GIT_REMOTEHOMEBREW_CORE_GIT_REMOTE 以免运行 brew update 时远程再次被更换。

官网:https://brew.sh
参考:https://mirrors.ustc.edu.cn/help/brew.git.html
引用:https://mirrors.tuna.tsinghua.edu.cn/help/homebrew

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容