MacOS包管理器brew的安装和使用

  |  

摘要: 本文记录了 MacOS 中的包管理器 homebrew 的安装过程和常用功能

【对算法,数学,计算机感兴趣的同学,欢迎关注我哈,阅读更多原创文章】
我的网站:潮汐朝夕的生活实验室
我的公众号:算法题刷刷
我的知乎:潮汐朝夕
我的github:FennelDumplings
我的leetcode:FennelDumplings


brew 安装与报错解决

brew 又叫 Homebrew,是 MacOS 上的软件包管理工具,能在 Mac 中方便的安装软件或者卸载软件,只需要一个命令,非常方便。类似于 ubuntu 系统下的 apt 的功能。

brew 的官网 中有安装命令:

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

不过直接运行的话可能会报错:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
==> Checking for `sudo` access (which may request your password)...
Password:
==> This script will install:
/opt/homebrew/bin/brew
/opt/homebrew/share/doc/homebrew
/opt/homebrew/share/man/man1/brew.1
/opt/homebrew/share/zsh/site-functions/_brew
/opt/homebrew/etc/bash_completion.d/brew
/opt/homebrew

Press RETURN/ENTER to continue or any other key to abort:
==> /usr/bin/sudo /usr/sbin/chown -R chengzhaoxi:admin /opt/homebrew
==> Downloading and installing Homebrew...
HEAD is now at e1fca9583 Merge pull request #13465 from Homebrew/dependabot/bundler/Library/Homebrew/sorbet-static-and-runtime-0.5.10109
Error: Fetching /opt/homebrew/Library/Taps/homebrew/homebrew-cask failed!
Fetching /opt/homebrew failed!
fatal: Could not resolve HEAD to a revision
Failed during: /opt/homebrew/bin/brew update --force --quiet

可以在浏览器中打开 https://github.com/Homebrew/install/blob/master/install.sh,复制到本地的 shell 脚本文件 brew_install.sh。并做以下修改:

1
2
# HOMEBREW_BREW_DEFAULT_GIT_REMOTE="https://github.com/Homebrew/brew"
# HOMEBREW_CORE_DEFAULT_GIT_REMOTE="https://github.com/Homebrew/homebrew-core"

将上面两行改为下面两行。

1
2
HOMEBREW_CORE_DEFAULT_GIT_REMOTE="git://mirrors.ustc.edu.cn/homebrew-core.git"
HOMEBREW_BREW_DEFAULT_GIT_REMOTE="git://mirrors.ustc.edu.cn/brew.git"

然后直接运行 shell 脚本安装:

1
bash brew_install.sh

安装后,brew 的位置在 /opt/homebrew/bin/brew,需要将相应的 bin 路径添加到 PATH,在 .zshrc 文件文件中增加下面一行:

1
export PATH=/opt/homebrew/bin/brew:$PATH

然后运行以下命令

1
brew update

会报错:

1
2
fatal: Could not resolve HEAD to a revision
Already up-to-date.

我们首先要找到 homebrew 相应的路径,运行

1
brew update --verbose

结果如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Checking if we need to fetch /opt/homebrew...
Checking if we need to fetch /opt/homebrew/Library/Taps/homebrew/homebrew-cask...
Fetching /opt/homebrew...
Checking if we need to fetch /opt/homebrew/Library/Taps/homebrew/homebrew-core...
Fetching /opt/homebrew/Library/Taps/homebrew/homebrew-core...
Updating /opt/homebrew...
Branch 'master' set up to track remote branch 'master' from 'origin'.
Switched to and reset branch 'master'
Your branch is up to date with 'origin/master'.
Switched to and reset branch 'stable'
Current branch stable is up to date.

Updating /opt/homebrew/Library/Taps/homebrew/homebrew-core...
fatal: Could not resolve HEAD to a revision

Already up-to-date.

进入 /opt/homebrew/Library/Taps/homebrew/homebrew-core,运行以下命令:

1
2
sudo git fetch --prune origin
sudo git pull --rebase origin master

然后再运行 brew update 就可以了。

可能还会报下面的错:

1
2
fatal: unable to access 'https://github.com/Homebrew/homebrew-cask/': Failed to connect to github.com port 443: Operation timed out
Error: Fetching /opt/homebrew/Library/Taps/homebrew/homebrew-cask failed!

解决方法如下:

1
2
git config --global --unset http.proxy
git config --global --unset https.proxy

下面安装 wget 作为测试。

1
2
brew search wget
brew install wget

brew 常见选项及其用法

卸载

1
brew uninstall wget

查找,正则表达式要包含在 / 中。

1
2
brew search tmux
brew search /tmux*/

安装

1
brew install tmux

列出已安装的软件

1
brew list

更新brew

1
brew update

用浏览器打开brew的官方网站

1
brew home

显示软件信息

1
brew info

显示包依赖

1
brew deps

Share