!文章内容如有错误或排版问题,请提交反馈,非常感谢!
Homebrew是MacOS的软件包管理工具,类似Ubuntu的apt、ArchLinux的pacman,拥有安装、卸载、更新、查看、搜索等很多实用的功能。简单的一条指令,就可以实现包管理,而不用你关心各种依赖和文件路径的情况,十分方便快捷。
Homebrew的安装与使用
Homebrew 的安装
Homebrew的安装只需在命令行中输入一条命令即可。具体命令可在官网获得。
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Homebrew使用
#安装软件 brew install 软件名 #卸载软件 brew uninstall 软件名 #搜索软件 brew search 软件名 #查看已安装软件列表 brew list #删除软件 brew cleanup 软件名 #查看需要更新的软件 brew outdated #更新软件 brew upgrade 软件名 #更新Homebrew brew update #查看Homebrew配置信息: brew config
Homebrew的配置
Homebrew默认的软件源在国外,安装时下载速度非常的慢,像其他包管理工具一样可以修改软件源。
Homebrew主要由四个部分组成: brew、homebrew-core、homebrew-cask、homebrew-bottles,它们对应的功能如下:
- homebrew:源代码仓库
- homebrew-core:Homebrew核心源
- homebrew-cask:提供macos应用和大型二进制文件的安装
- homebrew-bottles:预编译二进制软件包
查看当前源
#查看brew.git当前源 cd "$(brew --repo)" && git remote -v #查看homebrew-core.git当前源 cd "$(brew --repo homebrew/core)" && git remote -v
切换软件源
MacOS在10.15系统开始,默认的shell都换成了zsh,所以这里只介绍终端为zsh的方法。以中科大源为例:
#替换brew.git: cd "$(brew --repo)" && git remote set-url origin https://mirrors.ustc.edu.cn/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 #替换homebrew-cask.git: cd "$(brew --repo)/Library/Taps/homebrew/homebrew-cask" && git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-cask.git #替换homebrew-bottles镜像 echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles/bottles' >> ~/.zprofile #修改使其立即生效 source ~/.zshrc
其他源:
- 阿里云:https://mirrors.aliyun.com/homebrew/brew.git
- 腾讯云:https://mirrors.cloud.tencent.com/homebrew/brew.git
- 清华:https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git
- 北外:https://mirrors.bfsu.edu.cn/git/homebrew/brew.git
恢复默认源
git -C "$(brew --repo)" remote set-url origin https://github.com/Homebrew/brew.git git -C "$(brew --repo homebrew/core)" remote set-url origin https://github.com/Homebrew/homebrew-core.git git -C "$(brew --repo homebrew/cask)" remote set-url origin https://github.com/Homebrew/homebrew-cask.git brew update
homebrew-bottles配置只能手动删除,将~/.zprofile文件中的HOMEBREW_BOTTLE_DOMAIN=https://mirrors.xxx.com内容删除,并执行source ~/.zprofile。
其他内容
目前cask是从GitHub上读取软件源,而GitHubApi对访问有限制,如果使用比较频繁的话,可以申请ApiToken,然后在环境变量中配置到HOMEBREW_GITHUB_API_TOKEN。
在.zprofile中追加,注意替换yourtoken:
echo 'export HOMEBREW_GITHUB_API_TOKEN=yourtoken' >> ~/.zprofile source ~/.zprofile
注意:因为cask是从GitHub下载软件,所以目前是无法加速的。
参考链接: