在 Linux 上安装和使用 Homebrew 包管理器
Homebrew brew
![](/data/attachment/album/202112/09/090614sptwo73z73f7fw7u.jpg)


Homebrew（家酿），也被称为 Brew，是一个主要为 macOS 创建的命令行包管理器。


[Homebrew](https://brew.sh/) 在 macOS 用户中相当流行，因为更多的开发者创造了可以用 Homebrew 轻松安装的命令行工具。


这种流行导致了 Linuxbrew 的诞生，它是 Homebrew 的一个 Linux 移植版。由于它主要是 Git 和 Ruby 组成的，而 Linux 和 macOS 都是类 Unix 的系统，所以 Brew 在两种操作系统上都能很好地工作。


Linuxbrew 项目最终与 Homebrew 项目合并，现在只有一个 Brew 项目，叫做 Homebrew。


为什么我叫它 Brew，而不是 Homebrew？因为命令以 `brew` 开头。你会在后面的章节中看到它的详细内容。


### 当你已经有了 apt、dnf、snap 时，为什么还要在 Linux 上使用 Homebrew 包管理器？


我知道这种感觉。你已经有一个由你的发行版提供的好的 [包管理器](https://itsfoss.com/package-manager/)。除此之外，你还有 Snap、Flatpak 和其他通用软件包系统。


在你的 Linux 系统上你真的需要 Homebrew 包管理器吗？答案取决于你的需求。


你看，除了发行版的包管理器和通用包管理器，你会遇到需要其他包管理器的情况，比如 [Pip](https://itsfoss.com/install-pip-ubuntu/) （用于 Python 应用）和 [Cargo](https://itsfoss.com/install-rust-cargo-ubuntu-linux/) （用于 Rust 软件包）。


想象一下，你遇到了一个很好的命令行工具，想试试。它的软件库提到它可以使用 `brew` 或源代码来安装。在这种情况下，在你的系统上有 `brew` 可能会有帮助。毕竟，都 2021 了，[从源代码安装](https://itsfoss.com/install-software-from-source-code/) 并不时髦（也不舒服）。


换句话说，如果你遇到一些有趣的命令行工具只提供 `brew` 安装选项，你会有一个额外的选择。


### 在 Ubuntu 和其他 Linux 发行版上安装 Homebrew


安装是相当容易的。你只需要确保你已经有了所有的依赖项。


#### 步骤 1：安装依赖项


你需要有相对较新版本的 gcc 和 glibc。你可以 [在 Ubuntu 上安装 build-essential 包](https://itsfoss.com/build-essential-ubuntu/) 来获得它们。除此之外，你还需要 [安装 Git](https://itsfoss.com/install-git-ubuntu/)、Curl 和 procps（用于监控系统进程）。


在基于 Ubuntu 和 Debian 的系统中，你可以像这样一起安装所有这些东西：



```
sudo apt-get install build-essential procps curl file git

```

![Iinstall dependencies for Homebrew in Ubuntu/Debian](/data/attachment/album/202112/09/090620wou7fktsevo2fntd.png)


对于其他发行版，请使用你的包管理器并安装这些依赖项。


#### 步骤 2：安装 Homebrew


你可以看到为什么你需要 [安装 Curl](https://itsfoss.com/install-curl-ubuntu/)。它允许你 [在终端下载安装脚本文件](https://itsfoss.com/download-files-from-linux-terminal/)。


只要输入这个命令：



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

```

当要求输入回车键时，请按下回车。


![Installing Homebrew on Ubuntu](/data/attachment/album/202112/09/090620wl63mhoxxhywiyqr.png)


在脚本最后，它建议运行几个命令，将其添加到 `PATH` 变量中。Homebrew 实际上是安装在你的主目录中，然后软链接到 `/usr/local` 目录中。


![Run the suggested command under Next steps to add Homebrew to PATh variable](/data/attachment/album/202112/09/090621p12c1vjvsv2vzovz.png)


你可以 [在终端方便地复制和粘贴](https://itsfoss.com/copy-paste-linux-terminal/)。只要选择它所建议的命令，按 `Ctrl+Shift+C` 复制，按 `Ctrl+Shift+V` 粘贴。


或者，你也可以直接复制粘贴这个命令：



```
echo 'eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"' >> $HOME/.bash_profile

```

然后是这个：



```
eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"

```

![Adding brew commands to the PATH](/data/attachment/album/202112/09/090621as5lrr2m7zlrodpx.png)


#### 步骤 3：验证 brew 的安装


就快完成了。只需通过使用 `brew doctor` 命令来验证 `brew` 命令是否可以运行：



```
brew doctor

```

`brew doctor` 命令会告诉你是否有任何问题。


你可以通过安装示例 hello 项目再次验证：



```
brew install hello

```

如果你没有看到任何错误，你可以在 Linux 上享用家酿了。


### 使用 brew 命令来安装、删除和管理软件包


让我快速告诉你几个可以用来安装、删除和管理软件包的 `brew` 命令。


由于 Homebrew 安装在你的主目录中，你不需要 `sudo` 来运行它（就像 Pip 和 Cargo）。


要用 `brew` 安装一个软件包，请使用安装选项：



```
brew install package_name

```

这里没有自动完成软件包名称的功能。你需要知道确切的软件包名称。


要删除一个 `brew` 软件包，你可以使用 `remove` 或 `uninstall` 选项。两者的作用是一样的。



```
brew remove package_name

```

你也可以用这个命令列出已安装的 `brew` 软件包：



```
brew list

```

你也可以用 `autoremove` 选项删除不需要的依赖：



```
brew autoremove

```

在下一张截图中，我只用 `brew` 安装了两个软件包，但它也显示了这些软件包的依赖关系。即使在移除软件包后，依赖关系仍然存在。`autoremove` 终于把它们删除了。


![Listing and removing brew apckages](/data/attachment/album/202112/09/090621cwwe2evtsnj68tv8.png)


还有很多 `brew` 命令选项，但这不在本教程的范围内。你可以随时 [翻阅它的文档](https://docs.brew.sh/Manpage) 并进一步探索。


### 从 Linux 中删除 Homebrew


如果不加入从你的 Linux 系统中删除 Homebrew 的步骤，本教程就不完整。


根据 [GitHub 仓库中提到的步骤](https://github.com/homebrew/install#uninstall-homebrew)，你必须下载并使用这个命令运行卸载脚本：



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

```

你会被要求输入 `Y` 键来确认删除。


![Removing Homebrew from Linux](/data/attachment/album/202112/09/090621ry044mh0ukyht3gj.png)


当 Homebrew 的卸载完成后，它会列出它所遗留的文件和目录：


![Remaining files after Homebrew removal](/data/attachment/album/202112/09/090622mh1h1dhavo7zvshd.png)


让你自己去删除这些文件和目录。


### 总结


正如我前面解释的，Homebrew 为你已经有的东西提供了一个扩展。如果你偶然发现一个只有 `brew` 安装方式的应用，在你的 Linux 系统上安装 Homebrew 会很方便。


你对这个话题有什么要补充的，或者分享你的问题或意见？请在评论区留言。




---


via: <https://itsfoss.com/homebrew-linux/>


作者：[Abhishek Prakash](https://itsfoss.com/author/abhishek/) 选题：[lujun9972](https://github.com/lujun9972) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
![Warp Terminal](/assets/images/warp-terminal.webp)


![Warp Terminal](/assets/images/warp-terminal.webp)

Homebrew, also known as Brew, is a command line package manager primarily created for macOS.

[Homebrew](https://brew.sh/?ref=itsfoss.com) grew quite popular among macOS users as more developers created command line tools that could be easily installed with Homebrew.

This popularity resulted in the creation of Linuxbrew, a Linux port for Homebrew. Since it is primarily Git and Ruby, and Linux and macOS are both Unix-like systems, Brew works good on both kind of operating systems.

Linuxbrew project eventually merged with Homebrew project and now you just have one Brew project called Homebrew.

Why am I calling it brew, instead of Homebrew? Because the command starts with brew. You’ll see it in detail in a later section.

## Why use Homebrew package manager on Linux when you have got apt, dnf, snap etc?

I know the feeling. You already have a good [package manager](https://itsfoss.com/package-manager/) provided by your distribution. In addition to that, you have Snap, Flatpak and other universal package system.

Do you really need Homebrew package manager on your Linux system? The answer depends on your requirement, really.

See, apart from the distribution’s package manager and universal packages, you’ll come across situations where you need other package managers like [Pip](https://itsfoss.com/install-pip-ubuntu/) (for Python applications) and [Cargo](https://itsfoss.com/install-rust-cargo-ubuntu-linux/) (for Rust packages).

Imagine you came across a good command line utility and want to try it. It’s repository mentions that it can be installed using brew or source code only. In such a case, having brew on your system could be helpful. After all, [installing from source code](https://itsfoss.com/install-software-from-source-code/) in the 2020s is not fashionable (and comfortable).

In other words, you’ll have an additional option in case you come across some interesting CLI tool that provides only brew installation option.

## Install Homebrew on Ubuntu and other Linux distributions

The installation is quite easy. You just have to make sure that you have got all the dependencies.

### Step 1: Install dependencies

You need to have relatively newer version of gcc and glibc. You can [install build-essential package on Ubuntu](https://itsfoss.com/build-essential-ubuntu/) to get them. Apart from that, you also need to [install Git](https://itsfoss.com/install-git-ubuntu/), Curl and procps (used for monitoring system process).

You can install all of them together like this in Ubuntu and Debian based systems:

`sudo apt-get install build-essential procps curl file git`


![install dependencies for homebrew linux](https://itsfoss.com/content/images/wordpress/2021/11/install-dependencies-for-homebrew-linux-800x493.png)

For other distributions, please use your package manager and install these dependencies.

### Step 2: Install Homebrew

You can see why you needed to [install Curl](https://itsfoss.com/install-curl-ubuntu/). It allows you to [download the installation script file in the terminal](https://itsfoss.com/download-files-from-linux-terminal/).

Just enter this command:

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


When asked for RETURN key, press enter:

![installing homebrew ubuntu](https://itsfoss.com/content/images/wordpress/2021/11/installing-homebrew-ubuntu-800x453.png)

At the end of the script competition, it recommends to run a few commands to add it to the PATH variable. Homebrew is actually installed in your home directory and then soft linked to the /usr/local directory.

![adding homebrew to path linux](https://itsfoss.com/content/images/wordpress/2021/11/adding-homebrew-to-path-Linux-800x442.png)

You can [copy and paste in terminal](https://itsfoss.com/copy-paste-linux-terminal/) easily. Just select the command it suggests and press Ctrl+Shift+C to copy and Ctrl+Shift+V to paste.

Alternatively, you can just copy paste this command:

`echo 'eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"' >> $HOME/.bash_profile`


And then this:

`eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"`


![post installation steps for brew](https://itsfoss.com/content/images/wordpress/2021/11/post-installation-steps-for-brew.png)

### Step 3: Verify brew installation

You are almost done. Just verify that brew command is ready to run by using the brew doctor command:

`brew doctor`


The brew doctor command will tell you if there is any issue.

You may double verify by installing the sample hello project:

`brew install hello`


If you see no errors, you can enjoy the Homebrew package manager on Linux.

## Using brew command for installing, removing and managing packages

Let me quickly tell you a few brew commands you can use for installing, removing and managing packages.

Since Homebrew is installed in your home directory, you do not need sudo to run it (just like Pip and Cargo).

To install a package with brew, use the install option:

`brew install package_name`


There is no autocompletion for the package name here. You need to know the exact package name.

To remove a brew package, you can use either **remove** or **uninstall** option. Both works the same.

`brew remove package_name`


You can also list the installed brew packages with this command:

`brew list`


You can also remove the unneeded dependencies with the autoremove option:

`brew autoremove`


In the next screenshot, I had only two packages installed with brew but it also shows the dependencies installed for those packages. Even after removing the package, dependencies remained. The autoremove finally removed them.

![brew remove packages](https://itsfoss.com/content/images/wordpress/2021/11/brew-remove-packages-800x572.png)

There are a lot more brew command options but that is out of scope for this tutorial. You can always [go through their documentation](https://docs.brew.sh/Manpage?ref=itsfoss.com) and explore it further.

## Removing Homebrew from Linux

This tutorial won’t complete without adding the steps for removing Homebrew from your Linux system.

As per the [steps mentioned on its GitHub repository](https://github.com/homebrew/install?ref=itsfoss.com#uninstall-homebrew), you have to download and run the uninstall script using this command:

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


You’ll be asked to confirm the removal by entering the Y key.

![removing homebrew from linux](https://itsfoss.com/content/images/wordpress/2021/11/removing-homebrew-from-Linux-800x539.png)

When the uninstallation of Homebrew completes, it lists the files and directories it has leftover:

![remaining files after homebrew removal](https://itsfoss.com/content/images/wordpress/2021/11/remaining-files-after-homebrew-removal-800x464.png)

I let you remove the files and directories on your own.

## Conclusion

As I explained earlier, Homebrew provides an extension to what you have already got. If you stumble upon an application that has only brew as installation method, having Homebrew installed on your Linux system will come handy.

Anything you want to add to this topic or share your question or opinion? Please use the comment section.