Skip to main content
  1. 经验技巧分享/
  2. 系统运维、优化/

在 linux 上使用Brew

··376 字
howto

brew.sh 是 macos 系统上非常好用的软件安装器。正如官方网站上的宣传语 The Missing Package Manager for macOS (or Linux)macOS(或 Linux)缺失的软件包的管理器),因为在 macos 上,官方只提供了 appstore 来安装软件,并没有内置类似于 linux apt 工具。对于开发者来说,大量的命令行工具需要手工是非常麻烦的。因此就有了 brew.sh。前不久,看到 brew.sh 开始支持 linux 了。虽然可能部分软件安装会存在问题,但是基本上可以满足日常需要。

brew.sh 有一个与 apt 最大差别的特性,就是brew.sh把软件安装在用户空间而非系统空间。使用 apt 安装的软件,是全系统所有用户共用。而brew.sh可以把程序安装在非系统目录,这就降低了使用系统命令不小心越权的风险。而且可以装上与系统内置软件不同的版本。

在 windows 上也有类似的软件安装器,请看本站文章

安装 brew #

使用官方的一键安装脚本,

> /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
==> Checking for `sudo` access (which may request your password).
==> Select the Homebrew installation directory
- Enter your password to install to /home/linuxbrew/.linuxbrew (recommended)
- Press Control-D to install to /home/ubuntu/.linuxbrew
- Press Control-C to cancel installation
[sudo] password for ubuntu:

这个脚本会检查当前用户是否具有 sudo 执行权限,不同权限会安装在不同的位置。

  • 有sudo权限 : /home/linuxbrew/.linuxbrew
  • 无sudo权限 : /home/$USER/.linuxbrew

可阅读本站文章 《 普通用户使用 sudo 来执行需要 root 权限的命令 》看如何设置 sudo

输入当前用户密码(注意不是 root 用户密码),

[sudo] password for ubuntu:
==> This script will install:
/home/linuxbrew/.linuxbrew/bin/brew
/home/linuxbrew/.linuxbrew/share/doc/homebrew
/home/linuxbrew/.linuxbrew/share/man/man1/brew.1
/home/linuxbrew/.linuxbrew/share/zsh/site-functions/_brew
/home/linuxbrew/.linuxbrew/etc/bash_completion.d/brew
/home/linuxbrew/.linuxbrew/Homebrew
==> The following new directories will be created:
/home/linuxbrew/.linuxbrew/bin
/home/linuxbrew/.linuxbrew/etc
/home/linuxbrew/.linuxbrew/include
/home/linuxbrew/.linuxbrew/lib
/home/linuxbrew/.linuxbrew/sbin
/home/linuxbrew/.linuxbrew/share
/home/linuxbrew/.linuxbrew/var
/home/linuxbrew/.linuxbrew/opt
/home/linuxbrew/.linuxbrew/share/zsh
/home/linuxbrew/.linuxbrew/share/zsh/site-functions
/home/linuxbrew/.linuxbrew/var/homebrew
/home/linuxbrew/.linuxbrew/var/homebrew/linked
/home/linuxbrew/.linuxbrew/Cellar
/home/linuxbrew/.linuxbrew/Caskroom
/home/linuxbrew/.linuxbrew/Frameworks

Press RETURN to continue or any other key to abort

看到这里似乎已经明白,想卸载 brew.sh的话,直接删除目录 /home/linuxbrew/.linuxbrew 即可。

敲下回车后等待,需要好一会, brew 的安装脚本会从 github.com 上克隆代码以及软件资源信息库。如果你的电脑或服务器无法访问 github.com 的话,可能需要科学上网。本站使用腾讯云服务器,实测可以正常安装,只是速度非常慢。如果你使用米国的 vps,那么会飞起来。

# 看看国内的龟速
==> /usr/bin/sudo /bin/mkdir -p /home/linuxbrew/.linuxbrew/Homebrew
==> /usr/bin/sudo /bin/chown -R ubuntu:ubuntu /home/linuxbrew/.linuxbrew/Homebrew
==> Downloading and installing Homebrew...
remote: Enumerating objects: 183461, done.
remote: Counting objects: 100% (68/68), done.
remote: Compressing objects: 100% (60/60), done.
Receiving objects:   1% (2107/183461), 1.22 MiB | 9.00 KiB/s

配置环境变量 #

path #

.profile.bashrc 中添加环境变量:

# 在配置文件中添加以下语句
eval $(/home/linuxbrew/.linuxbrew/bin/brew shellenv)

上面的命令会在 shell 环境中添加以下环境变量:

➜  /home/linuxbrew/.linuxbrew/bin/brew shellenv
export HOMEBREW_PREFIX="/home/linuxbrew/.linuxbrew";
export HOMEBREW_CELLAR="/home/linuxbrew/.linuxbrew/Cellar";
export HOMEBREW_REPOSITORY="/home/linuxbrew/.linuxbrew/Homebrew";
export PATH="/home/linuxbrew/.linuxbrew/bin:/home/linuxbrew/.linuxbrew/sbin${PATH+:$PATH}";
export MANPATH="/home/linuxbrew/.linuxbrew/share/man${MANPATH+:$MANPATH}:";
export INFOPATH="/home/linuxbrew/.linuxbrew/share/info:${INFOPATH:-}";

关闭自动更新功能 #

brew 自行更新是默认行为,会在你每一次执行 brew 命令前自行执行更新。因为到 github.com 的速度实在太慢,建议关闭。并且,通过 brew 管理的软件已达5~6千个,这时即使网络速度很快,这个更新检查动作也会带来非常明显的卡顿。

站长的习惯是几天更新一下,没必要老更新

在你的 shell 配置文件中 .profile.bashrc 中,设置 HOMEBREW_NO_AUTO_UPDATE1,以此关闭掉自动更新。

export HOMEBREW_NO_AUTO_UPDATE=1

编译库变量 #

如果你经常安装一些需要在本地编译的 c 程序,可以使用brew安装依赖库,但需要添加库路径中才能生效。

# 在 .profile 或 .bashrc 中配置
export PKG_CONFIG_PATH=/usr/lib/pkgconfig:/usr/share/pkgconfig:$PKG_CONFIG_PATH
export PKG_CONFIG_PATH=$HOMEBREW_PREFIX/lib/pkgconfig:$PKG_CONFIG_PATH
export LD_LIBRARY_PATH=$HOMEBREW_PREFIX/lib:$LD_LIBRARY_PATH

更新源的镜像站 #

brew 安装时,依赖 github.com 上的软件安装信息库。成功安装 brew 后,会把自动 git clone 这个信息库到本地,安装软件时,根据这个信息库中提供的安装命令从远程服务器下载预编译的软件包,或需要在本地重新编译的源代码包与编译命令。

如果你侥幸成功地在 linux 上安装了 brew,需要经常更新,每次都这么慢那还得了!!! 除了上节所提及的 禁止自动更新 的方法外,使用国内镜像源。

可以设置镜像的:

  • homebrew: brew 本身的源码,对应目录 HOMEBREW_HOME/Homebrew
  • homebrew-core: 对应目录 HOMEBREW_HOME/Homebrew/Library/Taps/homebrew/homebrew-core
  • homebrew-cask: home-cask 表示 桌面 GUI 程序,linux 用不着。
  • homebrew-bottles: 软件二进制包,不指定 repo,而是指定软件包的下载地址。

上面的 HOMEBREW_HOME/home/linuxbrew/.linuxbrew

国内有几个常用镜像(可能还有其它的):

上面的镜像链接均为帮助说明,主要是修改以下内容:

> # 替换代码源仓库,如更换为清华的镜像
> git -C "$(brew --repo)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git
> # 替换软件安装信息库,如更换为清华镜像
> git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/linuxbrew-core.git
> # 设置软件包下载地址,如设置为清华镜像
>export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles"