Homebrew替换国内源

Homebrew是什么

Homebrew 是一个MacOS 上的包管理器,你可以用它来安装管理软件,功能非常强大,基本上只要是MacOS上能安装的软件,你通过Homebrew 都能够安装。类似于Linux上的yum,或者nodejs的npm

有人可能会说,我要安装软件,直接到App Store上下载不就行了吗?但是并不是所有,或者说,大部分软件你在App Store上找不到的,特别是对于开发人员所需的软件更是,基本在App Store找不到。

什么是Homebrew的源

其实就是一个giturl,对应的是git仓库。
每次通过Homebrew安装软件,Homebrew会根据源,会比对、下载(或者merge)源仓库到本地,然后根据要下载的软件找到对应的xxx.rb文件,然后再根据rb文件中内容一步步的校验、下载、安装软件。

为什么要替换Homebrew的源

因为慢慢慢慢啊!Homebrew原始源是以下:

# homebrew源
https://github.com/Homebrew/brew.git
# homebrew-core源
https://github.com/Homebrew/homebrew-core.git
# homebrew-cask源
https://github.com/Homebrew/homebrew-cask.git

大家可以看到,都是github,国内访问这些源时,经常性的慢出翔,所以必须替换为国内源,加快速度。

开始替换(4个地方)

国内源有几个不错的:

以下是替换清华的源

# 替换brew.git源
git -C "$(brew --repo)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git
# 替换 homebrew-core.git源
git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git
# 替换 homebrew-cask.git源
git -C "$(brew --repo homebrew/cask)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask.git

其实替换这三个源就可以了,另外就是homebrew-bottles,这个配置个HOMEBREW_BOTTLE_DOMAIN环境变量,不过我发现我不配置也没啥问题:

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

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

以下是替换为中科大的源

# brew.git源
git -C "$(brew --repo)" remote set-url origin https://mirrors.ustc.edu.cn/brew.git
# homebrew-core.git源
git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git
# homebrew-cask.git源
git -C "$(brew --repo homebrew/cask)" remote set-url origin https://mirrors.ustc.edu.cn/homebrew-cask.git
# 配置homebrew-bottles
## bash用户
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.bash_profile
source ~/.bash_profile
## zsh用户
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.zshrc
source ~/.zshrc

复核配置是否成功

设置完之后,可以执行以下命令,查看配置是否已经替换成功

# 查看brew.git源
git -C "$(brew --repo)" config --get remote.origin.url
# 查看homebrew-core.git源
git -C "$(brew --repo homebrew/core)" config --get remote.origin.url
# 查看homebrew-cask.git源
git -C "$(brew --repo homebrew/cask)" config --get remote.origin.url

最后执行brew update来尝试一下看是否速度更快。

重置为官方源

如果你想重置为官方的源,可以如下操作:

# brew.git源
git -C "$(brew --repo)" remote set-url origin https://github.com/Homebrew/brew.git
# homebrew-core.git源
git -C "$(brew --repo homebrew/core)" remote set-url origin https://github.com/Homebrew/homebrew-core
# homebrew-cask.git源
git -C "$(brew --repo homebrew/cask)" remote set-url origin https://github.com/Homebrew/homebrew-cask

另外记住删除 HOMEBREW_BOTTLE_DOMAIN 环境变量

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Mac下使用国内镜像安装Homebrew First MBP上的brew很老了,就想把brew更新一下,顺便安装一...
    采姑娘的小魔钴阅读 65,730评论 20 65
  • Homebrew是一款自由及开放源代码的软件包管理系统,用以简化macOS系统上的软件安装过程。对于Appstor...
    岩杉Shawn阅读 14,451评论 2 7
  • Homebrew是一款自由而且开放源码的软件包管理系统,用以简化Mac OS系统的软件安装过程。 Homebre...
    JZ_Gordon阅读 881评论 0 2
  • homebrew是osx下的一个包管理工具,可以很方便的管理各类包。官方给出的定义是 macOS缺失的软件包管理器...
    ouisun阅读 17,076评论 1 63
  • 久违的晴天,家长会。 家长大会开好到教室时,离放学已经没多少时间了。班主任说已经安排了三个家长分享经验。 放学铃声...
    飘雪儿5阅读 7,599评论 16 22