美文网首页宝藏库
Homebrew | Mac包管理器

Homebrew | Mac包管理器

作者: 掉了根筷子 | 来源:发表于2021-05-13 21:15 被阅读0次

Homebrew作为Mac os系统的包管理器,给软件的安装和更新等带来了极大地便利,但使用过程中总会遇到各种的问题。网上现有的Homebrew教程大多零散而杂乱,水平也层次不齐。本篇作为个人使用Homebrew的一些记录,包括一些问题的解决。希望能帮助到一些人。


1.Homebrew结构

Homebrew官网:https://brew.sh

Homebrew由4个部分组成,分别为brewhomebrew-corehomebrew-caskhomebrew-bottles。各部分的作用可以参考下表

名称 说明
brew Homebrew程序本体仓库
Homebrew-core Homebrew核心源
Homebrew-cask 安装macOS应用、大型二进制文件
Homebrew-bottles 预编译的二进制软件包

相对来说,brewhomebrew/core是必备项目,而homebrew/caskhomebrew/bottles可按需设置。


2.Homebrew安装及卸载

Homebrew的下载和安装相对比较容易。本文默认你是在拥有代理的情况下操作,均以官方的下载途径为准,避免不必要的麻烦。

2.1| 安装:

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

2.2|卸载

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

下载这个卸载的script后,可以运行 /bin/bash uninstall.sh --help 来看更多的操作选项


3.Homebrew更换国内源及重置

个人不是很推荐换源。在有代理的情况下优先推荐使用代理。未说明清楚恢复源的顺序,仍将换源记录在内。

3.1|换源

清华源的各类说明信息较为完备,个人较为推荐,因此说明均以清华源为例进行说明。如果你乐意,可以直接进入清华镜像站Homebrew页面查看教程

3.1.1 更换本体源 [brew.git]

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

3.1.2 更换核心软件仓库 [homebrew-core.git]

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

3.1.3 更换cask软件仓库[homebrew-cask.git](中科大源)

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

3.1.4 更换Bottles仓库 [homebrew-bottles]

1.长期更换

如果你用的bash,那么可以直接输入

# bash
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles' >> ~/.bash_profile
source ~/.bash_profile

*说明:
上述语句等效为在配置文件中添加了一项说明,然后再加载了这个依赖。
如果你的shell用的是bash(mac默认是bash),那么配置文件的路径为~/.bash_profile
如果你的shell用的是zsh(一般玩多了就会换成zsh),那么配置文件的路径为
~/.zshrc

综上,如果你用zsh,仅需将代码中所有的.bash_profile换成.zshrc即可。即:

# zsh
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles' >> ~/.zshrc
source ~/.zshrc

2.暂时使用

命令行中输入

export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles

3.2|重置官方源

3.2.1 重制本体源 [brew.git]

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

3.2.2 重制核心软件仓库 [homebrew-core.git]

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

3.2.3 重制cask软件仓库 [homebrew-core.git]

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

3.2.4 重制bottles

注释掉配置文件(zshrc或bash_profile)中所有有关homebrew-bottles的配置,重新加载配置文件,即可恢复官方源。


4.brew常用命令

# 安装
brew install [package]
# 卸载
brew uninstall [package]
# 更新hombrew自身
brew update
# 列出所有过期的包
brew outdated
# 更新[某一个]包
brew upgrade [package]
# 显示包的相关信息
brew info [package]
# 列出所有已安装的包
brew list
# 清除包缓存
brew cleanup
# 搜索某个包的信息
brew search [package]
# 显示某个包的相关信息
brew info [package]
# 查看某个包的依赖
brew debs [package]

5.配置代理

我的解决方案是直接配置了zsh的系统代理,即在zshrc中输入:

export https_proxy=http://127.0.0.1:7890 http_proxy=http://127.0.0.1:7890 all_proxy=socks5://127.0.0.1:7890

6.一些问题的解决

5.1 shallow clone

报错信息:

 homebrew-core is a shallow clone
 homebrew-cask is a shallow clone

解决方法:删除 homebrew-corehomebrew-cask,重新安装即可。

cd /usr/local/Homebrew/Library/Taps/homebrew
rm -rf homebrew-core
rm -rf homebrew-cask

重装:

# homebrew-core只需要upgrade即可自动安装
brew upgrade
# 重装cask
brew tap homebrew/cask

相关文章

网友评论

    本文标题:Homebrew | Mac包管理器

    本文链接:https://www.haomeiwen.com/subject/zgwxjltx.html