macOS 工具 - Homebrew


文章目录

    • 关于 Homebrew
      • 语义
    • 安装 brew
    • 常用操作
      • 包管理
    • 切换源
    • 设置bottles镜像
    • 报错
  • 参考


关于 Homebrew

  • 官网: https://brew.sh
  • github: https://github.com/Homebrew
  • brew command documentation https://docs.brew.sh/Manpage
  • Further Documentation https://docs.brew.sh
  • 中文说明:https://brew.sh/index_zh-cn.html

语义

  • formula: Homebrew package definition built from upstream sources
  • cask: Homebrew package definition that installs macOS native applications
  • keg: installation destination directory of a given formula version e.g. /usr/local/Cellar/foo/0.1
  • rack: directory containing one or more versioned kegs e.g. /usr/local/Cellar/foo
  • keg-only: a formula is keg-only if it is not symlinked into Homebrew’s prefix (e.g. /usr/local)
  • cellar: directory containing one or more named racks e.g. /usr/local/Cellar
  • Caskroom: directory containing one or more named casks e.g. /usr/local/Caskroom
  • external command: brew subcommand defined outside of the Homebrew/brew GitHub repository
  • tap: directory (and usually Git repository) of formulae, casks and/or external commands
  • bottle: pre-built keg poured into the cellar/rack instead of building from upstream sources
    预先编译好的包,不需要现场下载编译源码,速度会快很多。

安装 brew

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

卸载

/usr/bin/ruby -e "$(curl -fsSL https://cdn.jsdelivr.net/gh/ineo6/homebrew-install/uninstall)"

常用操作


# 查看 Homebrew 版本
$ brew -v
Homebrew 2.5.8-62-g084f2e7
Homebrew/homebrew-core (git revision d7b6c8; last commit 2020-11-04)
Homebrew/homebrew-cask (git revision 9c073; last commit 2020-11-04)

# 更新 Homebrew
$ brew update

# 查看包安装位置
$ ECHO  "$(brew --repo)"
/usr/local/Homebrew

# 查看 Homebrew 帮助信息
$ brew -h
Example usage:
  brew search [TEXT|/REGEX/]
  brew info [FORMULA...]
  brew install FORMULA...
  brew update
  brew upgrade [FORMULA...]
  brew uninstall FORMULA...
  brew list [FORMULA...]

Troubleshooting:
  brew config
  brew doctor
  brew install --verbose --debug FORMULA

Contributing:
  brew create [URL [--no-fetch]]
  brew edit [FORMULA...]

Further help:
  brew commands
  brew help [COMMAND]
  man brew
  https://docs.brew.sh
  

包管理

# 安装 package
$ brew install <packageName>

# 安装指定版本package;如:安装 12 版本的 node  
$ brew install node@12

# 卸载
$ brew uninstall <packageName>

# 查询可用包
$ brew search <packageName>

# 查看已安装包列表
$ brew list

# 查看任意包信息
$ brew info <packageName>
  • 有些包版本不再更新,可能也会在 brew 上下线(无法使用 brew 安装),如 openssl 1.0.2
  • Homebrew 会将套件安装到 /usr/local/Cellar 下的独立目录,并将文件软链接至 /usr/local/bin

Homebrew 的文件路径:

  • Homebrew :brew 程序所在路径,homebrew 的仓库位置默认位于 /usr/local/Homebrew
  • Cellar :文件夹存放的是所有包安装所在路径,包括二进制,文档和配置文件,按照这样 Cellar/包名/版本号/ 的形式来安放。
  • keg:具体某个包所在的目录,是 cellar 的子目录。
  • opt :由于版本号随着跟新而改变的,所以需要一个固定不变的路径作为我们访问二进制和文档的路径,这就是 opt 的作用。
  • bin :所有包安装之后二进制都会链接到这个路径下
  • share :所有包安装之后的文档都会链接到这个路径下
  • etc :同上,所有包的配置文件
  • lib :同上,所有包相关库文件
  • Caskroom :app 文件

切换源


为了方便,这里提供一份切换为 中科大源 和切换为原始源 的命令
需要继续修改 bottles镜像,然后update 才生效

cd "$(brew --repo)"
git remote set-url origin https://mirrors.ustc.edu.cn/brew.git
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-cask"
git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-cask.git
 

切换为原始的:

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


详细解释

# 1、替换 brew.git
$ cd "$(brew --repo)"
$ git remote set-url origin https://mirrors.ustc.edu.cn/brew.git
# 也可写为 git -C "$(brew --repo)" remote set-url origin https://mirrors.ustc.edu.cn/brew.git

# 清华大学 https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git 
# 中科大:https://mirrors.ustc.edu.cn/brew.git
# 阿里云:https://mirrors.aliyun.com/homebrew/brew.git
# 腾讯云:https://mirrors.cloud.tencent.com/homebrew/brew.git
# 原:https://github.com/Homebrew/brew.git

# 2、替换 homebrew-core.git:
$ cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
$ git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git

# 清华大学   https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git 
# 中科大:https://mirrors.ustc.edu.cn/homebrew-core.git
# 阿里云:https://mirrors.aliyun.com/homebrew/homebrew-core.git
# 腾讯云:https://mirrors.cloud.tencent.com/homebrew/homebrew-core.git
# 原:	https://github.com/Homebrew/homebrew-core.git

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



设置bottles镜像

从macOS Catalina(10.15.x) 版开始,Mac使用zsh作为默认shell,使用的配置文件:~/.zprofile

echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.zprofile
 
source ~/.zprofile
 # 清华大学 https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles'
# 中科大:https://mirrors.ustc.edu.cn/homebrew-bottles 
# 腾讯云:https://mirrors.cloud.tencent.com/homebrew-bottles

如果是以前的macOS版本,Mac使用bash作为默认shell,使用的配置文件:~/.bash_profile

echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.bash_profile
 
source ~/.bash_profile

如果要把源切换为原来的,也需要注释掉环境变量中的 HOMEBREW_BOTTLE_DOMAIN
然后重开一个 terminal 环境


最后使修改生效,并显示进度
$ brew upgrade --verbose


报错


报错1:

homebrew-core is a shallow clone.

如果执行 brew update 遇到下属情况,一般切换源可以解决

执行报错

$ brew update
Error: 
  homebrew-core is a shallow clone.
  homebrew-cask is a shallow clone.
To `brew update`, first run:
  git -C /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core fetch --unshallow
  git -C /usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask fetch --unshallow
These commands may take a few minutes to run due to the large size of the repositories.
This restriction has been made on GitHub's request because updating shallow
clones is an extremely expensive operation due to the tree layout and traffic of
Homebrew/homebrew-core and Homebrew/homebrew-cask. We don't do this for you
automatically to avoid repeatedly performing an expensive unshallow operation in
CI systems (which should instead be fixed to not use shallow clones). Sorry for
the inconvenience!

$   git -C /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core fetch --unshallow
fatal: unable to access 'https://github.com/Homebrew/homebrew-core/': LibreSSL SSL_read: error:02FFF03C:system library:func(4095):Operation timed out, errno 60

处理方法

$  git clone git://mirrors.ustc.edu.cn/homebrew-core.git/ /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core --depth=1
 
Cloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-core'...
remote: Counting objects: 5105, done.
remote: Compressing objects: 100% (4904/4904), done.
remote: Total 5105 (delta 44), reused 886 (delta 5)
Receiving objects: 100% (5105/5105), 4.14 MiB | 1.17 MiB/s, done.
Resolving deltas: 100% (44/44), done.

$ cd "$(brew --repo)" 
$ git remote set-url origin https://mirrors.ustc.edu.cn/brew.git

$ cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
$ git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git

# 测试,获取 url 
$ git remote  get-url origin
https://mirrors.ustc.edu.cn/brew.git

$ brew update
Already up-to-date.

 


报错2

Not a valid ref: refs/remotes/origin/master

打开路径 /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core, 如果只有一个 .git 文件,也会导致 update 失败,需要重新拉去。

$ cd /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core

$ git fetch --prune origin

$ git pull --rebase origin master

$ brew update

参考

  • M了个J:Mac安装Homebrew
    https://www.cnblogs.com/mjios/p/14497925.html#toc_title_2

伊织 2021-11-29

你可能感兴趣的:(软件工具/使用技巧,Homebrew,brew,mac)