Back

Mac下强大的包管理工具 - Homebrew

我绝对不允许还有Mac用户不会用Homebrew

Homebrew是MacOS系统下的包管理工具。如果你用过Linux,你可能对包管理工具很熟悉。不过想要使用Homebrew其实不需要去理解包管理到底是什么。这篇博客目的是用通俗易懂的方式帮助你理解并使用Homebrew。

Homebrew官网

简介

现在假如我想在Macbook上下载IINA视频播放器,下载流程应该像下面这样 (我几乎不使用app store, 因为里面大多数情况下没有我想要的软件,而且软件版本往往不是最新的):

  1. 打开浏览器搜索软件名称"IINA"
  2. 在众多的链接中找到软件的官网,同时要甄别非官网的软件站 - “https://iina.io/"
  3. 从官网中找到下载链接,并下载dmg/pkg文件 - “https://dl-portal.iina.io/IINA.v1.1.2.dmg"
  4. 运行下载的dmg/pkg文件进行安装

首先抛开这几步的重复性,我身边的许多人甚至无法从搜索引擎给出的众多广告和盗版网站中挑出软件的官网。那么如果使用Homebrew,安装IINA的过程就是这样的:

  1. 打开命令行终端
  2. 输入brew install --cask iina

一行命令行,剩下了许多操作。从某种程度上来讲,homebrew可以被称作是Mac上功能更强大的App Store。

而其实Homebrew的功能远比这强大的多,下面是一些使用的例子:

  1. 在电脑上安装python(还有jupyter notebook): brew install python, brew install jupyter
  2. 使用开源软件ntfs-3g解锁Macbook的NTFS读写功能: brew install ntfs-3g
  3. 装个HP打印机驱动: brew install homebrew/cask-drivers/apple-hewlett-packard-printer-drivers
  4. 许多常见的软件也在: brew install --cask qq, brew install --cask zoom, brew install --cask vscode

安装

Homebrew被GFW墙了,不过中国境内也有镜像站,因此国内与国外安装方式会不同

电脑有代理/在中国境外安装(官网安装方式)

首先要在电脑中安装命令行工具(Command Line Tools (CLT) for Xcode)

xcode-select --install

可能会提示输入密码,在命令行输入密码时屏幕没有任何反应,输入完毕按回车即可。

安装成功后,输入以下代码

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

此代码执行的操作是从Homebrew的Github Repo中下载安装脚本install.sh, 并运行(安装过程可能也会询问输入密码)

安装完成后,运行

brew doctor

这行命令会检测Hombrew状态,供用户查看Homebrew是否安装成功

在中国境内安装

中国境内可以使用清华源(清华大学的Homebrew国内镜像)进行安装和使用: 清华大学Homebrew镜像站

首先要在电脑中安装命令行工具(Command Line Tools (CLT) for Xcode)

xcode-select --install

可能会提示输入密码,在命令行输入密码时屏幕没有任何反应,输入完毕按回车即可。

为了让安装脚本使用清华的服务器,需要设置几行服务器变量

export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git"
export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git"
export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles"

设置之后从清华的服务器中clone homebrew的repo,运行里面的安装脚本。

git clone --depth=1 https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/install.git brew-install
/bin/bash brew-install/install.sh

运行后可以删除安装文件

rm -rf brew-install

注: 如果你的Mac是M1芯片架构,那么需要运行以下代码 (不知道自己是不是M1? 命令行运行uname -m,如果返回arm64则是m1,如果返回x86_64则是Intel)

test -r ~/.bash_profile && echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.bash_profile
test -r ~/.zprofile && echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zprofile

eval "$(/opt/homebrew/bin/brew shellenv)" 意思是在一个分开的Shell中运行brew shellenv, 此命令会运行一个shellenv.sh 的脚本,设置系统中的环境变量。
第一行: 如果电脑中安装了bash,则在每次bash开启时运行eval命令
第二行: 如果电脑中安装了zsh,则在每次zsh开启时运行eval命令

运行后重启shell,或者source刚刚更改的文件:

test -r ~/.bash_profile && source ~/.bash_profile
test -r ~/.zprofile && source ~/.zprofile

至此安装结束,可以用brew doctor查看安装是否成功

更换国内源

Homebrew默认使用的是国外服务器作为软件源,因此导致访问速度及慢,所以可以换为国内的镜像。

目前国内的源有腾讯源, 清华源, 中科大源

下面以清华源为例讲解换源过程,各个源的设置方法点进相应链接即可

更换索引镜像

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/homebrew-core.git
git -C "$(brew --repo homebrew/cask)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask.git
git -C "$(brew --repo homebrew/cask-fonts)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask-fonts.git
git -C "$(brew --repo homebrew/cask-drivers)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask-drivers.git
git -C "$(brew --repo homebrew/cask-versions)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask-versions.git
git -C "$(brew --repo homebrew/command-not-found)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-command-not-found.git

更换后应用更改

brew update-reset

更换二进制预编辑文件镜像

腾讯源, 清华源, 中科大源

test -r ~/.bash_profile && echo 'export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles"' >> ~/.bash_profile
test -r ~/.bash_profile && secho 'export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles"' >> ~/.zprofile
export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles"

第一行: 为bash添加环境变量,更改homebrew-bottles源
第二行: 为zsh添加环境变量,更改homebrew-bottles源
第三行: 在当前shell使用homebrew-bottles源

换回Homebrew官方源

辅助软件

Cakebrew

launchrocket

卸载

参考资料&资源网站

Homebrew 官网

Homebrew 深度应用

程序员 Homebrew 使用指北 - 少数派

Homebrew Setup Guide

腾讯软件源(腾讯源)

清华大学开源镜像软件站(清华源)

中科大开源镜像站(中科大源)

Licensed under CC BY-NC-SA 4.0
Last updated on Sep 17, 2022 17:06 EDT
Built with Hugo
Theme Stack designed by Jimmy