一个专注于IT互联网运维的技术博客

MacOS软件管理工具Homebrew的安装和使用

2019.01.19

Homebrew 是一款 MacOS 平台下的软件包管理工具,类似 Redhat 的 RPM 和 Debian 的 APT,正如官网的简介“The missing package manager for macOS (or Linux)”,Homebrew 拥有安装、卸载、更新、查看、搜索软件包等很多实用的功能。

1、安装 Homebrew

安装 Homebrew 非常简单,打开 MacOS 的终端,直接执行 Homebrew 的 Ruby 安装脚本即可:

ityoudao:~ wxy$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/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
==> The following new directories will be created:
/usr/local/sbin
/usr/local/Caskroom
==> The Xcode Command Line Tools will be installed.

Press RETURN to continue or any other key to abort
==> /usr/bin/sudo /bin/mkdir -p /usr/local/sbin /usr/local/Caskroom
Password:
==> /usr/bin/sudo /bin/chmod g+rwx /usr/local/sbin /usr/local/Caskroom
==> /usr/bin/sudo /bin/chmod 755 /usr/local/share/zsh /usr/local/share/zsh/site-functions
==> /usr/bin/sudo /usr/sbin/chown wxy /usr/local/sbin /usr/local/Caskroom
==> /usr/bin/sudo /usr/bin/chgrp admin /usr/local/sbin /usr/local/Caskroom
==> Searching online for the Command Line Tools
==> /usr/bin/sudo /usr/bin/touch /tmp/.com.apple.dt.CommandLineTools.installondemand.in-progress
==> Installing Command Line Tools (macOS Mojave version 10.14) for Xcode-10.2
==> /usr/bin/sudo /usr/sbin/softwareupdate -i Command\ Line\ Tools\ (macOS\ Mojave\ version\ 10.14)\ for\ Xcode-10.2
Software Update Tool


Downloading Command Line Tools (macOS Mojave version 10.14) for Xcode
Downloaded Command Line Tools (macOS Mojave version 10.14) for Xcode
Installing Command Line Tools (macOS Mojave version 10.14) for Xcode
Done with Command Line Tools (macOS Mojave version 10.14) 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...
remote: Enumerating objects: 2602, done.
remote: Counting objects: 100% (2602/2602), done.
remote: Compressing objects: 100% (21/21), done.
remote: Total 3670 (delta 2583), reused 2597 (delta 2581), pack-reused 1068
接收对象中: 100% (3670/3670), 803.87 KiB | 529.00 KiB/s, 完成.
处理 delta 中: 100% (2890/2890), 完成 943 个本地对象.
来自 https://github.com/Homebrew/brew
   ae0332a0f..bcc5f042b  master     -> origin/master
 * [新标签]              2.0.5      -> 2.0.5
 * [新标签]              2.0.6      -> 2.0.6
 * [新标签]              2.1.0      -> 2.1.0
 * [新标签]              2.1.1      -> 2.1.1
 * [新标签]              2.1.2      -> 2.1.2
正在检出文件: 100% (1472/1472), 完成.
HEAD 现在位于 bcc5f042b Merge pull request #6143 from letmaik/letmaik/HOMEBREW_CURL_RETRIES
Updated 1 tap (homebrew/core).
==> New Formulae
aliyun-cli         cfn-lint           embree             gel                kubeaudit          nlohmann-json      proper             solr@7.7
allureofthestars   cmix               fasttext           gitleaks           lazygit            operator-sdk       proteinortho       terrahub
appstream-glib     cpp-gsl            faudio             go@1.11            leela-zero         ospray             protobuf@3.6       vapoursynth-imwri
boringtun          cql                ffsend             inlets             libgr              osqp               pyinstaller        vapoursynth-ocr
breezy             csound             frpc               itk                libzt              otf2               s2geometry         vapoursynth-sub
buildkit           cubelib            frps               kahip              llvm@7             phpstan            scws               volt
ccls               cypher-shell       gcc@8              ktlint             mdbook             pipx               sk                 zabbix-cli
==> Updated Formulae
openssl ✔                              gcc                                    liblas                                 prometheus
...
gawk                                   libjson-rpc-cpp                        proj
==> Deleted Formulae
at-spi2-atk           gtk-engines           minisat               plan9port             ruby@2.3              tmux-cssh             zxing-cpp
at-spi2-core          gtk-murrine-engine    monax                 protobuf@2.5          safe                  tomcat@6
go@1.4                js-test-driver        node@6                protobuf@2.6          solr@5.5              typesafe-activator
go@1.8                libutf                pdftoedn              rlvm                  solr@6.6              xmoto
==> 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
  • 安装开始 Homebrew 会提示将要安装的脚本、创建的目录和安装 Xcode 工具,按回车键确认安装。

2、使用 Homebrew

查询 Homebrew 帮助文档

brew --help 查看 brew 命令的帮助菜单:

ityoudao:~ wxy$ brew --help
Example usage:
  brew search [TEXT|/REGEX/]
  brew info [FORMULA...]
  brew install FORMULA...
  brew update
  brew upgrade [FORMULA...]
  brew uninstall FORMULA...
  brew list [FORMULA...]

Troubleshooting:
  brew config
  brew doctor
  brew install --verbose --debug FORMULA

Contributing:
  brew create [URL [--no-fetch]]
  brew edit [FORMULA...]

Further help:
  brew commands
  brew help [COMMAND]
  man brew
  https://docs.brew.sh

man brew查看 brew 命令的 man 文档:

ityoudao:~ wxy$ man brew

brew home使用浏览器访问 Homebrew 官网:

ityoudao:~ wxy$ brew home

brew search 搜索软件

ityoudao:~ wxy$ brew search nginx
==> Formulae
nginx

brew info 查看软件包信息

ityoudao:~ wxy$ brew info nginx
nginx: stable 1.15.12 (bottled), HEAD
HTTP(S) server and reverse proxy, and IMAP/POP3 proxy server
https://nginx.org/
Not installed
From: https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git/Formula/nginx.rb
==> Dependencies
Required: openssl ✘, pcre ✘
==> Options
--HEAD
	Install HEAD version
==> Caveats
Docroot is: /usr/local/var/www

The default port has been set in /usr/local/etc/nginx/nginx.conf to 8080 so that
nginx can run without sudo.

nginx will load all files in /usr/local/etc/nginx/servers/.

To have launchd start nginx now and restart at login:
  brew services start nginx
Or, if you don't want/need a background service you can just run:
  nginx
==> Analytics
install: 33,046 (30 days), 112,298 (90 days), 430,676 (365 days)
install_on_request: 31,267 (30 days), 105,472 (90 days), 396,686 (365 days)
build_error: 0 (30 days)

brew deps 显示软件包依赖信息

ityoudao:~ wxy$ brew deps nginx
openssl
pcre

brew install 安装软件包

ityoudao:~ wxy$ brew install nginx
Updating Homebrew...
==> Installing dependencies for nginx: openssl and pcre
==> Installing nginx dependency: openssl
==> Downloading https://homebrew.bintray.com/bottles/openssl-1.0.2r.mojave.bottle.tar.gz
==> Downloading from https://akamai.bintray.com/c1/c1f8c06740398325c7028213b20b18c5de39763fbc81e5819c78a06ee0621170?__gda__=exp=1558234855~hmac=d6f77945d2d52
######################################################################## 100.0%
==> Pouring openssl-1.0.2r.mojave.bottle.tar.gz
==> Caveats
A CA file has been bootstrapped using certificates from the SystemRoots
keychain. To add additional certificates (e.g. the certificates added in
the System keychain), place .pem files in
  /usr/local/etc/openssl/certs

and run
  /usr/local/opt/openssl/bin/c_rehash

openssl is keg-only, which means it was not symlinked into /usr/local,
because Apple has deprecated use of OpenSSL in favor of its own TLS and crypto libraries.

If you need to have openssl first in your PATH run:
  echo 'export PATH="/usr/local/opt/openssl/bin:$PATH"' >> ~/.bash_profile

For compilers to find openssl you may need to set:
  export LDFLAGS="-L/usr/local/opt/openssl/lib"
  export CPPFLAGS="-I/usr/local/opt/openssl/include"

==> Summary
🍺  /usr/local/Cellar/openssl/1.0.2r: 1,795 files, 12.1MB
==> Installing nginx dependency: pcre
==> Downloading https://homebrew.bintray.com/bottles/pcre-8.43.mojave.bottle.tar.gz
==> Downloading from https://akamai.bintray.com/08/08e7414a7641d1e184c936537ff67f72f52649374d2308b896d4146ccc2c08fe?__gda__=exp=1558234873~hmac=36e0ef23d9d3c
######################################################################## 100.0%
==> Pouring pcre-8.43.mojave.bottle.tar.gz
🍺  /usr/local/Cellar/pcre/8.43: 204 files, 5.5MB
==> Installing nginx
==> Downloading https://homebrew.bintray.com/bottles/nginx-1.15.12.mojave.bottle.tar.gz
==> Downloading from https://akamai.bintray.com/f2/f2084e7e6ec97658001039be1be07aaaaae760427c990b1075cf4bf27d98a23c?__gda__=exp=1558234883~hmac=320061f53ad38
######################################################################## 100.0%
==> Pouring nginx-1.15.12.mojave.bottle.tar.gz
==> Caveats
Docroot is: /usr/local/var/www

The default port has been set in /usr/local/etc/nginx/nginx.conf to 8080 so that
nginx can run without sudo.

nginx will load all files in /usr/local/etc/nginx/servers/.

To have launchd start nginx now and restart at login:
  brew services start nginx
Or, if you don't want/need a background service you can just run:
  nginx
==> Summary
🍺  /usr/local/Cellar/nginx/1.15.12: 25 files, 2MB
==> Caveats
==> openssl
A CA file has been bootstrapped using certificates from the SystemRoots
keychain. To add additional certificates (e.g. the certificates added in
the System keychain), place .pem files in
  /usr/local/etc/openssl/certs

and run
  /usr/local/opt/openssl/bin/c_rehash

openssl is keg-only, which means it was not symlinked into /usr/local,
because Apple has deprecated use of OpenSSL in favor of its own TLS and crypto libraries.

If you need to have openssl first in your PATH run:
  echo 'export PATH="/usr/local/opt/openssl/bin:$PATH"' >> ~/.bash_profile

For compilers to find openssl you may need to set:
  export LDFLAGS="-L/usr/local/opt/openssl/lib"
  export CPPFLAGS="-I/usr/local/opt/openssl/include"

==> nginx
Docroot is: /usr/local/var/www

The default port has been set in /usr/local/etc/nginx/nginx.conf to 8080 so that
nginx can run without sudo.

nginx will load all files in /usr/local/etc/nginx/servers/.

To have launchd start nginx now and restart at login:
  brew services start nginx
Or, if you don't want/need a background service you can just run:
  nginx

brew list 列出已经安装的软件包

ityoudao:~ wxy$ brew list
gdbm		git		openssl		pcre2		readline	telnet
gettext		nginx		pcre		python		sqlite		xz

brew list <软件包名称> 列出该软件包的所有文件,类似 rpm -qf <软件包名称>

ityoudao:~ wxy$ brew list nginx
/usr/local/Cellar/nginx/1.15.12/.bottle/etc/ (15 files)
/usr/local/Cellar/nginx/1.15.12/bin/nginx
/usr/local/Cellar/nginx/1.15.12/homebrew.mxcl.nginx.plist
/usr/local/Cellar/nginx/1.15.12/html -> ../../../var/www
/usr/local/Cellar/nginx/1.15.12/share/man/man8/nginx.8
/usr/local/Cellar/nginx/1.15.12/share/nginx/ (2 files)

brew update 升级所有软件

brew update 升级所有软件包,把所有的 Formula 目录更新,并且会对本机已经安装并有更新的软件用“ * ”标明:

ityoudao:~ wxy$ brew update
Already up-to-date.

brew upgrade 升级具体软件包

brew upgrade <软件包名称>升级指定软件包:

ityoudao:~ wxy$ brew upgrade nginx
Updating Homebrew...
Error: nginx 1.15.12 already installed

brew uninstall 卸载软件包

ityoudao:~ wxy$ brew uninstall nginx
Uninstalling /usr/local/Cellar/nginx/1.15.12... (25 files, 2MB)

3、修改 Homebrew 软件源

可以修改 Homebrew 的 formula 索引的镜像(即 brew update 时所更新内容),使用国内的镜像可以加快软件下载速度,比如清华大学开源软件镜像站.

查看当前上游信息:

// 查看brew.git:
ityoudao:~ wxy$ cd "$(brew --repo)"
ityoudao:Homebrew wxy$ git remote get-url origin
https://github.com/Homebrew/brew

// 查看homebrew-core.git
ityoudao:Homebrew wxy$ cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
ityoudao:homebrew-core wxy$ git remote get-url origin
https://github.com/Homebrew/homebrew-core

替换现有上游:

// 替换brew.git:
ityoudao:homebrew-core wxy$ cd "$(brew --repo)"
ityoudao:Homebrew wxy$ git remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git
ityoudao:Homebrew wxy$ git remote get-url origin
https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git

// 替换homebrew-core.git:
ityoudao:Homebrew wxy$ cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
ityoudao:homebrew-core wxy$ git remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git
ityoudao:homebrew-core wxy$ git remote get-url origin
https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git

// 更新
ityoudao:homebrew-core wxy$ brew update
Already up-to-date.

如果更换后的 Homebrew 软件源有问题,可以重置 brew 上游:

// 重置brew.git:
ityoudao:homebrew-core wxy$ cd "$(brew --repo)"
ityoudao:Homebrew wxy$ git remote set-url origin https://github.com/Homebrew/brew.git
ityoudao:Homebrew wxy$ git remote get-url origin
https://github.com/Homebrew/brew.git

// 重置homebrew-core.git:
ityoudao:Homebrew wxy$ cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
ityoudao:homebrew-core wxy$ git remote set-url origin https://github.com/Homebrew/homebrew-core.git
ityoudao:homebrew-core wxy$ git remote get-url origin
https://github.com/Homebrew/homebrew-core.git

// 更新
ityoudao:homebrew-core wxy$ brew update
Already up-to-date.
发表评论