Homebrew 4 如何更换镜像源

521 阅读1分钟

自2023-02-16发布的 Homebrew 4.0.0 起,Homebrew终于抛弃了使用Git的方式维护软件信息而采用JSON文件的方式,这也意味着 HOMEBREW_INSTALL_FROM_API 会成为默认的行为。因此对于大部分用户来说不再需要homebrew/core仓库,而会新引入homebrew/api仓库。

网上众多介绍更换homebrew镜像源的文章都已经过时了,这里记录下最新的操作方式

  1. 手动清理下不再使用的Homebrew tap信息,会释放空间,后续执行brew update好像也会默认执行

    brew untap homebrew/cask
    brew untap homebrew/core
    
  2. 设置brew镜像源

    这个只需要执行一次,会帮忙设置git remote,所以不需要加到~/.zshrc里面

    export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git"
    brew update
    

    这一步也可以手动设置,效果是一样的

    cd "$(brew --repo)" git remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git
    
  3. 设置API访问地址

    4.0.0版本之后换成了API方式,动态请求网络(默认为https://formulae.brew.sh/api)获得JSON文件,请求的地址需要以环境变量的形式来指定(如果使用的是bash,则将.zshrc修改为.bashrc

    echo 'export HOMEBREW_API_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/api"' >> ~/.zshrc
    source ~/.zshrc
    brew update
    
  4. 设置homebrew-servicehomebrew-cask-fontshomebrew-cask-versionshomebrew-command-not-found的镜像源(这些不一定都有用到)

    brew tap --custom-remote --force-auto-update homebrew/cask-fonts https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask-fonts.git
    brew tap --custom-remote --force-auto-update homebrew/cask-versions https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask-versions.git
    brew tap --custom-remote --force-auto-update homebrew/command-not-found https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-command-not-found.git
    brew tap --custom-remote --force-auto-update homebrew/services https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-services.git
    brew update
    

重要参考了清华大学的Homebrew 镜像源使用帮助 ,写得非常详细,此外也可以考虑使用中科大的Homebrew镜像