7

macOS安装配置Homebrew

 2 years ago
source link: https://www.biaodianfu.com/homebrew.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Homebrew是MacOS的软件包管理工具,类似Ubuntu的apt、Arch Linux的pacman,拥有安装、卸载、更新、查看、搜索等很多实用的功能。简单的一条指令,就可以实现包管理,而不用你关心各种依赖和文件路径的情况,十分方便快捷。

homebrew.png

Homebrew的安装与使用

Homebrew的安装

Homebrew的安装只需在命令行中输入一条命令即可。具体命令可在官网获得。

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
/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
# 安装软件
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
# 查看 brew.git 当前源
cd "$(brew --repo)" && git remote -v
# 查看 homebrew-core.git 当前源
cd "$(brew --repo homebrew/core)" && git remote -v

切换软件源

Mac OS在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
# 替换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
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上读取软件源,而GitHub Api对访问有限制,如果使用比较频繁的话,可以申请Api Token,然后在环境变量中配置到HOMEBREW_GITHUB_API_TOKEN。

在.zprofile中追加,注意替换yourtoken:

echo 'export HOMEBREW_GITHUB_API_TOKEN=yourtoken' >> ~/.zprofile
source ~/.zprofile
echo 'export HOMEBREW_GITHUB_API_TOKEN=yourtoken' >> ~/.zprofile
source ~/.zprofile

注意:因为cask是从GitHub下载软件,所以目前是无法加速的。

参考链接:


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK