【原创】MacOS 安装 Homebrew

安装准备

1、简介

Homebrew 是 MacOS 上的第三方库的管理工具,它可以非常方便地去管理 MacOS 上的第三方软件。比如,安装、更新、卸载一个 wget 工具,只需要通过 brew 命令即可完成。

2、安装环境

  • MacOS 10.15.6

  • Homebrew 2.5.1

  • curl 7.6.41

  • git 2.24.3

安装步骤

1、一键安装

homebrew 官网给出的安装教程可以说是相当的简单了,就给了一条命令,如下所示:

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

如果用这条命令安装当然是可以的。但是,我们知道 github 在国内没有部署服务节点,离咱们最近的可能也是新加坡的节点了。

而使用一键安装的命令在安装的过程中是需要连接 gihub 并下载资源的。所以,国内用户使用一键安装可能花费的时间是相当长的。

示例:

jiangzhuolin@bogon ~ % /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)" 
==> 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
==> The following existing directories will be made group writable:
/usr/local/bin
==> The following existing directories will have their owner set to jiangzhuolin:
/usr/local/bin
==> The following existing directories will have their group set to admin:
/usr/local/bin
==> The following new directories will be created:
...
==> The Xcode Command Line Tools will be installed.
Press RETURN to continue or any other key to abort    # 这里记得按 RETURN (回车) 键继续
...
==> Installing Command Line Tools for Xcode-11.5
==> /usr/bin/sudo /usr/sbin/softwareupdate -i Command\ Line\ Tools\ for\ Xcode-11.5
Software Update Tool
Downloading Command Line Tools for Xcode
Downloaded Command Line Tools for Xcode
Installing Command Line Tools for Xcode
Done with Command Line Tools for Xcode
Done.
==> /usr/bin/sudo /bin/rm -f /tmp/.com.apple.dt.CommandLineTools.installondemand.in-progress
==> /usr/bin/sudo /usr/bin/xcode-select --switch /Library/Developer/CommandLineTools
==> Downloading and installing Homebrew...
==> Downloading and installing Homebrew...
remote: Enumerating objects: 88, done.
remote: Counting objects: 100% (88/88), done.
remote: Compressing objects: 100% (87/87), done.
Receiving objects:   2% (3134/153716), 1.45 MiB | 8.00 KiB/s 

可以从我上面的示例看见,这下载速度那可以相当的慢呀,8 KB/s 的速度仿佛是让我们回到了上个世纪村网通的场面。

>>> 2020-11-11 更新开始:

在之前写这篇文章的时候,raw.githubusercontent.com 虽然慢但还能连接,但目前国内很多地区该域名都被屏蔽了,执行一键安装命令可能会报如下的错:

$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
curl: (7) Failed to connect to raw.githubusercontent.com port 443: Connection refused

故更新解决方法如下:

在当前用户可读写的任意目录下执行如下命令即可进行安装,基本等同于一键安装,只是把 github 上的 install 项目中的所有源码均拉到本地了。

git clone https://github.com/Homebrew/install; sh install/install.sh

示例:

$ pwd
/Users/jiangzhuolin
$ git clone https://github.com/Homebrew/install; sh install/install.sh
Cloning into 'install'...
remote: Enumerating objects: 17, done.
remote: Counting objects: 100% (17/17), done.
remote: Compressing objects: 100% (17/17), done.
remote: Total 1583 (delta 4), reused 10 (delta 0), pack-reused 1566
Receiving objects: 100% (1583/1583), 470.55 KiB | 3.00 KiB/s, done.
Resolving deltas: 100% (809/809), done.
######
Password:
==> 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...
remote: Enumerating objects: 2370, done.
remote: Counting objects: 100% (2370/2370), done.
remote: Total 5019 (delta 2370), reused 2370 (delta 2370), pack-reused 2649
Receiving objects: 100% (5019/5019), 1.44 MiB | 75.00 KiB/s, done.
Resolving deltas: 100% (3913/3913), completed with 1062 local objects.
From https://github.com/Homebrew/brew
 * [new branch]          dependabot/bundler/Library/Homebrew/ruby-macho-2.4.0 -> origin/dependabot/bundler/Library/Homebrew/ruby-macho-2.4.0
 * [new branch]          dependabot/bundler/Library/Homebrew/sorbet-runtime-0.5.6042 -> origin/dependabot/bundler/Library/Homebrew/sorbet-runtime-0.5.6042
   adb9922ed..f2d46efc1  master     -> origin/master
 * [new tag]             2.5.2      -> 2.5.2
 * [new tag]             2.5.3      -> 2.5.3
 * [new tag]             2.5.4      -> 2.5.4
 * [new tag]             2.5.5      -> 2.5.5
 * [new tag]             2.5.6      -> 2.5.6
 * [new tag]             2.5.7      -> 2.5.7
 * [new tag]             2.5.8      -> 2.5.8
 * [new tag]             2.5.9      -> 2.5.9
HEAD is now at f2d46efc1 Merge pull request #9107 from Homebrew/dependabot/bundler/Library/Homebrew/sorbet-0.5.6042
...
==> 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
No analytics data has been sent yet (or will be during this `install` run).
...
==> 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

注:这里下载的源码中的 install 目录中的 install.sh 脚本与下面第二步中下载 install.sh 是一个意思,所以如果你觉得这种直接安装比较慢,想用第二步中的先更改镜像源,再安装的方式,直接对 install.sh 中的镜像源进行修改即可。

>>> 2020-11-11 更新结束

2、使用国内镜像安装

从一键安装命令里我们可以知道,其他一键安装的命令就是去执行了一个 github 上的一个 shell 脚本,而真正的安装是这个 shell 脚本在运行。

我们可以通过浏览器打开一键安装命令中的链接就可以看见 shell 安装脚本里的内容了。

如下是我截取的一部分内容:

(1) 下载 install.sh

执行如下命令从 github 上下载 install.sh 的安装脚本,这个脚本本身不大,所以下载速度尚可。

curl -o install.sh https://raw.githubusercontent.com/Homebrew/install/master/install.sh

示例:

jiangzhuolin@bogon ~ % pwd
/Users/jiangzhuolin
jiangzhuolin@bogon ~ % curl -o install.sh https://raw.githubusercontent.com/Homebrew/install/master/install.sh
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
.                                 Dload  Upload   Total   Spent    Left  Speed
100 18940  100 18940    0     0   2421      0  0:00:07  0:00:07 --:--:--  4053
jiangzhuolin@bogon ~ % ls -lh
total 40
drwx------@  5 jiangzhuolin  staff   160B 12 26  2019 Desktop
drwx------+  3 jiangzhuolin  staff    96B 11 27  2019 Documents
drwx------+  5 jiangzhuolin  staff   160B  6 26 01:27 Downloads
drwx------@ 57 jiangzhuolin  staff   1.8K  6 26 01:44 Library
drwx------+  4 jiangzhuolin  staff   128B 12  3  2019 Movies
drwx------+  3 jiangzhuolin  staff    96B 11 27  2019 Music
drwx------+  4 jiangzhuolin  staff   128B 12  3  2019 Pictures
drwxr-xr-x+  4 jiangzhuolin  staff   128B 11 27  2019 Public
-rw-r--r--   1 jiangzhuolin  staff    18K  9 17 00:22 install.sh

不过从上面可以看出 18 KB 的文件我花了 7 秒才下载下来...

(2) 更改 install.sh 脚本中的 BREW_REPO

在 install.sh 脚本所在的目录执行如下命令将 BREW_REPO 由 github 更改为国内的镜像源,如清华大学镜像站

sed -in 's#BREW_REPO="https://github.com/Homebrew/brew"#BREW_REPO="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git"#g' install.sh

示例:

jiangzhuolin@bogon ~ % ls -l
total 40
drwx------@  5 jiangzhuolin  staff    160 12 26  2019 Desktop
drwx------+  3 jiangzhuolin  staff     96 11 27  2019 Documents
drwx------+  5 jiangzhuolin  staff    160  6 26 01:27 Downloads
drwx------@ 57 jiangzhuolin  staff   1824  6 26 01:44 Library
drwx------+  4 jiangzhuolin  staff    128 12  3  2019 Movies
drwx------+  3 jiangzhuolin  staff     96 11 27  2019 Music
drwx------+  4 jiangzhuolin  staff    128 12  3  2019 Pictures
drwxr-xr-x+  4 jiangzhuolin  staff    128 11 27  2019 Public
-rw-r--r--   1 jiangzhuolin  staff  18940  9 17 00:22 install.sh
jiangzhuolin@bogon ~ % 
jiangzhuolin@bogon ~ % sed -in 's#BREW_REPO="https://github.com/Homebrew/brew"#BREW_REPO="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git"#g' install.sh

替换前:

替换后:

可以看到第 35 行的内容已经被替换了。

(3) 执行 install.sh 脚本安装

HOMEBREW_CORE_GIT_REMOTE=https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git bash install.sh

示例:

到这时,下面的安装过程就与一键安装相同的流程了,但安装速度就快很多了。

运行验证

1、查看 homebrew 版本

jiangzhuolin@bogon ~ % brew --version
Homebrew 2.5.1
Homebrew/homebrew-core (git revision 096ba; last commit 2020-09-16)

2、homebrew 常用命令

(1) 安装应用

brew install

示例:

jiangzhuolin@bogon ~ % brew install wget
==> Downloading https://homebrew.bintray.com/bottles/gettext-0.21.catalina.bottle.tar.gz
==> Downloading from https://d29vzk4ow07wi7.cloudfront.net/cdea54f52b7c36ebcb5fe26a1cf736d7cd6fd5f2fd016d
######################################################################## 100.0%
==> Downloading https://homebrew.bintray.com/bottles/libunistring-0.9.10.catalina.bottle.tar.gz
...
openssl@1.1 is keg-only, which means it was not symlinked into /usr/local,
because macOS provides LibreSSL.
If you need to have openssl@1.1 first in your PATH run:
  echo 'export PATH="/usr/local/opt/openssl@1.1/bin:$PATH"' >> ~/.zshrc
For compilers to find openssl@1.1 you may need to set:
  export LDFLAGS="-L/usr/local/opt/openssl@1.1/lib"
  export CPPFLAGS="-I/usr/local/opt/openssl@1.1/include"

(2) 查看 brew 安装的应用

brew list

示例:

jiangzhuolin@bogon ~ % brew list
gettext        libidn2        libunistring    openssl@1.1    wget

brew list --cask 可以查看通过 cask 安装的应用。

(3) 卸载应用

brew uninstall

示例:

jiangzhuolin@bogon ~ % brew uninstall wget
Uninstalling /usr/local/Cellar/wget/1.20.3_2... (50 files, 4.0MB)

Warning: The following may be wget configuration files and have not been removed!
If desired, remove them manually with `rm -rf`:
  /usr/local/etc/wgetrc

(4) 更新指定应用

brew upgrade {app_name}

示例:

jiangzhuolin@bogon ~ % brew upgrade wget
Warning: wget 1.20.3_2 already installed

(5) 更新 brew

brew update

示例:

jiangzhuolin@bogon ~ % brew update
Already up-to-date.

附录

Homebrew 官方网站:

https://brew.sh/

homebrew 国内镜像站

https://mirrors.tuna.tsinghua.edu.cn/help/homebrew/

Homebrew github 地址:

https://github.com/Homebrew/brew

caskroom cask github 地址:

https://github.com/Caskroom/caskroom.github.io

You may also like...

发表回复

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