Homebrew是一款包管理工具,目前支持macOS和linux系统。主要有四个部分组成:brew、homebrew-core、homebrew-cask、homebrew-bottles。
本文主要介绍Homebrew安装方式以及如何加速访问,顺便普及一些必要的知识。
- curl: (7) Failed to connect to raw.githubusercontent.com port 443: Operation timed out
使用官方脚本遇到上面的错误提示:请按照下面「安装步骤」步骤执行。
如果有更换镜像源的想法,强烈推荐使用镜像助手获取执行脚本。
- /bin/bash -c "$(curl -fsSL https://gitee.com/ineo6/homebrew-install/raw/master/install.sh)"
如果命令执行中卡在下面信息(如提示有差异,请反馈给我):
- ==> Tapping homebrew/core
- Cloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-core'...
请Control + C中断脚本执行如下命令:
- cd "$(brew --repo)/Library/Taps/"
- mkdir homebrew && cd homebrew
- git clone git://mirrors.ustc.edu.cn/homebrew-core.git
cask同样也有安装失败或者卡住的问题,解决方法也是一样:
- cd "$(brew --repo)/Library/Taps/"
- cd homebrew
- git clone https://mirrors.ustc.edu.cn/homebrew-cask.git
成功执行之后继续执行前文的安装命令:
- /bin/bash -c "$(curl -fsSL https://gitee.com/ineo6/homebrew-install/raw/master/install.sh)"
最后看到==> Installation successful!就说明安装成功了。
最最后更新下:
- brew update
重要信息 ↓↓↓↓↓↓↓
最新更新:目前一键安装脚本不再需要单独设置下面的镜像内容,内容仅作为备份保留。
brew、homebrew/core是必备项目,homebrew/cask、homebrew/bottles按需设置。
通过brew config命令可以查看相关配置信息。
- git -C "$(brew --repo)" remote set-url origin https://mirrors.ustc.edu.cn/brew.git
-
- git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git
-
- git -C "$(brew --repo homebrew/cask)" remote set-url origin https://mirrors.ustc.edu.cn/homebrew-cask.git
-
- brew update
- git -C "$(brew --repo)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git
-
- git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git
-
- git -C "$(brew --repo homebrew/cask)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask.git
-
- brew update
更多可选源请访问镜像助手。
设置环境变量需要注意终端Shell的类型,请看下面说明:
镜像以中科大源为例。
从macOS Catalina(10.15.x) 版开始,Mac使用zsh作为默认Shell,对应文件是.zprofile,所以命令为:
- echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles/bottles' >> ~/.zprofile
- source ~/.zprofile
如果是macOS Mojave及更低版本,并且没有自己配置过zsh,对应文件则是.bash_profile:
- echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles/bottles' >> ~/.bash_profile
- source ~/.bash_profile
注意:上述区别仅仅是.zprofile和.bash_profile不同,文章如有再次提及编辑.zprofile,均按此方法替换。
如果想使用清华源:
- 把
- https://mirrors.ustc.edu.cn/homebrew-bottles/bottles
-
- 替换为
- https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/bottles
至此,安装和设置操作都完成了。
- 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
注意:因为cask是从GitHub下载软件,所以目前是无法加速的。
使用官方脚本同样会遇到uninstall地址无法访问问题,可以使用下面脚本:
- /bin/bash -c "$(curl -fsSL https://gitee.com/ineo6/homebrew-install/raw/master/uninstall.sh)"
你可以点此homebrew-install创建issue,这也是最及时、最标准的反馈方式。
在前面的过程中我们把brew和homebrew-core的地址都指向到中科大镜像。
原理是通过修改install.sh脚本,在里面预设镜像地址。
- STAT="stat --printf"
- CHOWN="/bin/chown"
- CHGRP="/bin/chgrp"
- GROUP="$(id -gn)"
- TOUCH="/bin/touch"
- fi
- # 这里替换了BREW_REPO
- BREW_REPO="https://mirrors.ustc.edu.cn/brew.git"