2020Mac最新安装brew 亲测有效

4,020 阅读2分钟

首先是进官网安装,brew.sh/ 官网 官网

这个是安装不上的,不知道什么时候起,github慢了,就这个就不行了

很久都没有找到新的方法,今天记录一下,总算可以了

苹果系统macOS下使用国内镜像安装Homebrew

先进入终端,输入 cd 进入当前用户的根文档

  • 下载安装脚本到文件,修改安装脚本并安装

下载到文件

curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install >> brew_install

修改文件 sodu vim brew_install 注释掉这行

BREW_REPO = "https://github.com/Homebrew/brew".freeze和CORE_TAP_REPO = "https://github.com/Homebrew/homebrew-core".freeze

增加以下两行

BREW_REPO = "git://mirrors.ustc.edu.cn/brew.git".freeze
CORE_TAP_REPO = "git://mirrors.ustc.edu.cn/homebrew-core.git".freeze

  • 开始安装命令
/usr/bin/ruby ~/brew_install
unpingdeMacBook-Air:~ junpingxie$ /usr/bin/ruby ~/brew_install
==> This script will install:
/usr/local/bin/brew
/usr/local/share/doc/homebrew
/usr/local/share/man/man1/brew.1
/usr/local/share/zsh/site-functions/_brew
/usr/local/etc/bash_completion.d/brew
/usr/local/Homebrew

Press RETURN to continue or any other key to abort
==> Downloading and installing Homebrew...
HEAD is now at 87c35ac Merge pull request #6995 from issyl0/fix-brew-edit-with-graphical-editor
Already up-to-date.
==> Installation successful!

==> Homebrew has enabled anonymous aggregate formulae and cask analytics.
Read the analytics documentation (and how to opt-out) here:
  https://docs.brew.sh/Analytics

==> Homebrew is run entirely by unpaid volunteers. Please consider donating:
  https://github.com/Homebrew/brew#donations
==> Next steps:
- Run `brew help` to get started
- Further documentation:
    https://docs.brew.sh

显示安装成功了,高兴不高兴,以为这就完了吗,不是

你输入更新命令brew update,还是不能成功

原因是两个包的源还没有改成清华大学的源

替换Homebrew源


cd "$(brew --repo)"
git remote set-url origin git://mirrors.ustc.edu.cn/brew.git

cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git remote set-url origin git://mirrors.ustc.edu.cn/brew.git

设置 bintray镜像

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

应用生效 source ~/.bash_profile

再次执行 brew update

junpingdeMacBook-Air:~ junpingxie$ brew update
Already up-to-date.

完成了

查看安装命令

brew list

安装包

brew install xx

使用 Alibaba 的 Homebrew 镜像源进行加速

在国内的网络环境,使用 Homebrew 安装软件的过程中可能会长时间卡在 Updating Homebrew 这个步骤。

例:执行 brew install composer 命令

➜  ~ brew install composer
Updating Homebrew... # 如果碰到长时间卡在这里,参考以下处理方法

方法1:按住 control + c 取消本次更新操作

➜  ~ brew install composer
Updating Homebrew...

按住 control + c 之后命令行会显示 ^C,就代表已经取消了 Updating Homebrew 操作

大概不到 1 秒钟之后就会去执行我们真正需要的安装操作了

➜  ~ brew install composer
Updating Homebrew...
^C==> Satisfying dependencies
==> Downloading https://getcomposer.org/download/1.7.2/composer.phar
...

这个方法是临时的、一次性的

方法2:使用 Alibaba 的 Homebrew 镜像源进行加速

平时我们执行 brew 命令安装软件的时候,跟以下三个仓库有关:

brew.git

homebrew-core.git

homebrew-bottles

通过以下操作将这 3 个仓库地址全部替换为 Alibaba 提供的地址

1. 替换/还原 brew.git 仓库地址

# 替换成阿里巴巴的 brew.git 仓库地址:
cd "$(brew --repo)"
git remote set-url origin https://mirrors.aliyun.com/homebrew/brew.git

#=======================================================

# 还原为官方提供的 brew.git 仓库地址
cd "$(brew --repo)"
git remote set-url origin https://github.com/Homebrew/brew.git

2. 替换/还原 homebrew-core.git 仓库地址

# 替换成阿里巴巴的 homebrew-core.git 仓库地址:
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git remote set-url origin https://mirrors.aliyun.com/homebrew/homebrew-core.git

#=======================================================

# 还原为官方提供的 homebrew-core.git 仓库地址
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git remote set-url origin https://github.com/Homebrew/homebrew-core.git

3. 替换/还原 homebrew-bottles 访问地址

这个步骤跟你的 macOS 系统使用的 shell 版本有关系

所以,先来查看当前使用的 shell 版本

echo $SHELL

# 如果你的输出结果是 /bin/zsh,参考下方 zsh 终端操作方式
# 如果你的输出结果是 /bin/bash,参考下方 bash 终端操作方式

3.1 zsh 终端操作方式

# 替换成阿里巴巴的 homebrew-bottles 访问地址:
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.aliyun.com/homebrew/homebrew-bottles' >> ~/.zshrc
source ~/.zshrc

#=======================================================

# 还原为官方提供的 homebrew-bottles 访问地址
vi ~/.zshrc
# 然后,删除 HOMEBREW_BOTTLE_DOMAIN 这一行配置
source ~/.zshrc

3.2 bash 终端操作方式

# 替换 homebrew-bottles 访问 URL:
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.aliyun.com/homebrew/homebrew-bottles' >> ~/.bash_profile
source ~/.bash_profile

#=======================================================

# 还原为官方提供的 homebrew-bottles 访问地址
vi ~/.bash_profile
# 然后,删除 HOMEBREW_BOTTLE_DOMAIN 这一行配置
source ~/.bash_profile

安装了node npm 不能用的问题

Warning: The post-install step did not complete successfully

多方查询后,有人提到可能是 brew 安装目录的权限问题,解决办法是:

sudo chown -R $(whoami) $(brew --prefix)/*

安装node 最稳定版本

brew install node@12 && brew link --force --overwrite node@12

Linux 还是安装 不了

github.com/Homebrew/in…

这个文件中有github的地址,一样进行更改

执行命令 /bin/bash ./install.sh

这个版本的安装脚本已经没有CORE_TAP_REPO了,所以下载homebrew core的时候依然去Github下载,非常慢,可以在brew.git下载完,control-c结束掉。

把仓库https://mirrors.cloud.tencent.com/homebrew/homebrew-core.git克隆到/home/linuxbrew/.linuxbrew/Homebrew/Library/Taps/homebrew/

看提示