0%

mac包管理工具Homebrew

本文介绍macOS下的软件包管理工具Homebrew,适合mac新手。

简介

Homebrew 是macOS下的一个软件包管理器,用于安装各种命令行工,是macOS开发环境搭建必不可少的工具。起作用类似centOS下的yum和Debian、Ubuntu下的apt,可以用命令行实现软件的安装、卸载、更新等。

要求

  • Intel CPU

  • OS X 10.9 及以上

  • Command Line Tools for Xcode

    Command Line Tools for Xcode中包含了常用的命令行开发工具,比如gcc/g++编译器、 make、git等,是安装很多其他软件的基础。在 Terminal 中执行如下命令以安装 Command Line Tools for Xcode:

    1
    xcode-select --install

在弹出的窗口点击“Install”即可。

此处安装的Command Line Tools可能不是最新版,可以到“App Store”的“更新”中查看是 否有相关更新。如果有,则升级到最新版。 今后 App Store 来的系统大版本号升级的时候,可能需要更新 Command line tools, 小版本升级无需更新。

安装

homebrew的安装方法现已更新,从ruby转到了bash,网上的很多方法都是基于ruby的,可能无法完成安装,最新的安装操作引导请参考homebrew主页

在终端中执行如下命令以安装Homebrew:

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

也可以根据网上的说法,首先打开https://raw.githubusercontent.com/Homebrew/install/master/install.sh,将页面代码全选复制进新建的install.sh文件,然后在bash下运行该脚本。

当然,这种做法主要是为了解决国内访问brew官方镜像速度低而导致安装速度慢的问题,因此如果总是由于安装速度慢导致安装失败,则可以尝试该方法,但在运行install.sh前需要将其内brew-repo所指向的镜像仓库链接改为国内镜像,如清华、科大等镜像仓库。此处以清华镜像为例,找到下面这行代码:

1
BREW_REPO="https://github.com/Homebrew/brew"

将其改为:

1
BREW_REPO="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git"

然后在终端执行该脚本,进行安装。

Homebrew的常见用法

1
2
3
4
5
brew search xxx     # 模糊搜索某个软件
brew install xxx # 安装某个软件
brew upgrade xxx # 升级某个软件
brew uninstall xxx # 卸载某个软件
brew list xxx # 列出某个软件的所有相关文件

运行brew help查看详细用法,当然你也可以查看官方文档以获取更多帮助。

几个相关概念:

  • Formulae:Formulae指定了如何安装某个软件,因而每个软件对应一个Formulae
  • Bottle:从软件源码编译安装软件会比较慢,Bootle是已经编译好的二进制版本,以加快软件安装速度
  • Tap:软件仓库,包含了一系列软件的Fromulae

常用的软件仓库Taps

1. homebrew-core

Homebrew核心软件仓库。

2. Homebrew-Cask

Homebrew只能用于安装命令行软件,不能用于安装带图形界面的软件。Homebrew-Cask是Homebrew的一个扩展,可以用于安装带图形界面软件,可以提供macOS应用和大型二进制文件。

注意:
Caskroom 的 Git 地址在 2018 年 5 月 25 日从 https://github.com/caskroom/homebrew-cask 迁移到了 https://github.com/Homebrew/homebrew-cask

在终端执行如下命令以安装Homebrew-Cask:

1
2
brew tap homebrew/cask    # caskroom主仓库
brew tap homebrew/cask-fonts # caskroom字体仓库

常见用法:

1
2
3
4
5
brew cask search xxx     # 模糊搜索某个软件
brew cask install xxx # 安装某个软件
brew cask uninstall xxx # 卸载某个软件
brew cask reinstall # 重新安装某个软件
brew cask cleanup # 清理缓存

安装字体:

1
brew cask install font-source-code-pro

更新源

由于homebrew官方源都是存放在Github上的,因此中国大陆用户的访问速度被极大限制,所以下一步需要做的就是更换为国内源。

国内源可选的也比较多,此处推荐使用清华源或者科大源

可以通过下面的命令更新源(此处以科大源为例)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 替换brew程序本身:
cd "$(brew --repo)"
# 替换为科大源
git remote set-url origin https://mirrors.ustc.edu.cn/brew.git
# 替换为清华源
$ git remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git

# 替换homebrew-core.git:
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
# 科大源
git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git
# 清华源
git remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git

# 替换homebrew-cask.git:
cd "$(brew --repo)"/Library/Taps/caskroom/homebrew-cask
# 科大
git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-cask.git
# 清华
git remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask.git


# 替换homebrew-bottles:
# 科大
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles' >> ~/.bash_profile
# 清华
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles' >> ~/.bash_profile
# 使环境变量生效
source ~/.bash_profile

# 更新源、应用生效
brew update

如果你之前折腾过不少导致你的Homebrew有点问题,那么可以尝试使用如下方案:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 诊断Homebrew的问题:
$ brew doctor

# 重置brew.git设置:
$ cd "$(brew --repo)"
$ git fetch
$ git reset --hard origin/master

# homebrew-core.git同理:
$ cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
$ git fetch
$ git reset --hard origin/master

# 应用生效:
$ brew update

重置更新源 某些时候也有换回官方源的需求

1
2
3
4
5
6
7
# 重置brew.git:
$ cd "$(brew --repo)"
$ git remote set-url origin https://github.com/Homebrew/brew.git

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

以上如果有任何的不成功就用 brew doctor 查找原因。