macOS安装Homebrew彻底免坑教程

567 阅读1分钟

Homebrew 依赖于Xcode Command Line Tools(不会安装Xcode,放心执行),所以需先执行:

xcode-select --install

一、官方(GitHub)

官网:brew.sh/index_zh-cn
官方GitHub:github.com/Homebrew

1. 安装

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

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

2. 检查是否已安装成功

$ brew -v

Homebrew 2.6.0
Homebrew/homebrew-core (git revision 5ebb5; last commit 2020-12-09)
Homebrew/homebrew-cask (git revision aea32; last commit 2020-11-24)

3. 查看Homebrew源

# 查看 brew 源
$ git -C "$(brew --repo)" remote -v
# 查看 homebrew-core 源
$ git -C "$(brew --repo homebrew/core)" remote -v
# 查看 homebrew-cask 源
$ git -C "$(brew --repo homebrew/cask)" remote -v

# 此外,还有不常用的homebrew-cask-fonts、homebrew-cask-drivers等
$ git -C "$(brew --repo homebrew/cask-fonts)" remote -v
$ git -C "$(brew --repo homebrew/cask-drivers)" remote -v

至此Homebrew就可以使用了,但是国外的存在连接不稳定、下载速度慢的缺点,可以说是致命缺点,实际使用时非常影响效率,下面来介绍下国内安装的方法,以及切换Homebrew镜像源的方法。

二、国内

1. 清华大学源(安装、换源)

mirrors.tuna.tsinghua.edu.cn/help/homebr…
mirrors.tuna.tsinghua.edu.cn/help/homebr…

1.1 下载安装脚本install.sh

$ git clone https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/install.git
$ cd install

1.2 编辑 install.sh

BREW_REPO="https://github.com/Homebrew/brew"
# 改成:
BREW_REPO="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git"

1.3 运行 install.sh 安装 Homebrew

1.4 替换现有上游

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

# 更换后测试工作是否正常
$ brew update

1.5 替换 Homebrew bintray 镜像

  • 临时替换

    $ export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles
    
  • 永久替换

    # zsh
    $ echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles' >> ~/.zshrc
    $ source ~/.zshrc
    
    # bash
    $ echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles' >> ~/.bash_profile
    $ source ~/.bash_profile
    

2. 中科大源(换源)

mirrors.ustc.edu.cn/help/brew.g…
mirrors.ustc.edu.cn/help/homebr…

  • 替换现有上游
# brew 程序本身
$ cd "$(brew --repo)"
$ git remote set-url origin https://mirrors.ustc.edu.cn/brew.git
# homebrew-core
$ cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
$ git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git
# homebrew-cask
$ cd "$(brew --repo)"/Library/Taps/homebrew/homebrew-cask
$ git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-cask.git
  • 替换 Homebrew bintray 镜像
# zsh
$ echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.zshrc
$ source ~/.zshrc

# bash
$ echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.bash_profile
$ source ~/.bash_profile

3. HomebrewCN(安装、换源)

gitee.com/cunkai/Home…

这个是国内的一个开源的,安装+自动换源(中科大),比较便捷。

# zsh
$ /bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"

# bash
$ /bin/bash -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"

4. 阿里源

developer.aliyun.com/mirror/home…

  • 替换现有上游
# brew 程序本身
$ git -C "$(brew --repo)" remote set-url origin https://mirrors.aliyun.com/homebrew/brew.git
# homebrew-core
$ git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.aliyun.com/homebrew/homebrew-core.git
# 暂时没有 homebrew-cask
  • 替换 Homebrew bintray 镜像
# zsh
$ echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.aliyun.com/homebrew/homebrew-bottles' >> ~/.zshrc
$ source ~/.zshrc

# bash
$ echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.aliyun.com/homebrew/homebrew-bottles' >> ~/.bash_profile
$ source ~/.bash_profile

目前不推荐Homebrew使用阿里源,这也是放到最后一个介绍的原因。目前存在不稳定、没有homebrew-cask甚至失效等问题。(截止2020-12-09)

5. 复原重置为官方地址

# brew 程序本身
$ git -C "$(brew --repo)" remote set-url origin https://github.com/Homebrew/brew.git
# homebrew-core
$ git -C "$(brew --repo homebrew/core)" remote set-url origin https://github.com/Homebrew/homebrew-core.git
# homebrew-cask
$ git -C "$(brew --repo homebrew/cask)" remote set-url origin https://github.com/Homebrew/homebrew-cask.git

# 更换后测试工作是否正常
brew update