diff --git a/help/_posts/1970-01-01-homebrew.md b/help/_posts/1970-01-01-homebrew.md index be5ff31..6380ffe 100644 --- a/help/_posts/1970-01-01-homebrew.md +++ b/help/_posts/1970-01-01-homebrew.md @@ -8,100 +8,104 @@ mirrorid: homebrew **注:该镜像是 Homebrew/Linuxbrew 的 formula 索引的镜像(即 `brew update` 时所更新内容)。本镜像站同时提供相应的二进制预编译包的镜像,请参考 [Homebrew bottles 镜像使用帮助](https://{{ site.hostname }}/help/homebrew-bottles/) 和 [Linuxbrew bottles 镜像使用帮助](https://{{ site.hostname }}/help/linuxbrew-bottles/)。** -### 首次安装 Homebrew +### 首次安装 Homebrew / Linuxbrew -首先,下载安装脚本的镜像(也可以直接从官方下载,即 `https://raw.githubusercontent.com/Homebrew/install/master/install.sh` ): +首先,需要确保系统中安装了 git 和 curl。其中 macOS 系统自带,而 Linux 用户安装 git 即可 (curl 为 git 的依赖项会自动安装)。 + +接着,在终端输入以下两行命令设置环境变量: ```bash -git clone https://{{ site.hostname }}/git/homebrew/install.git -cd install +# macOS 用户,请使用以下两句命令 +export HOMEBREW_CORE_GIT_REMOTE="https://{{ site.hostname }}/git/homebrew/homebrew-core.git" +export HOMEBREW_BOTTLE_DOMAIN="https://{{ site.hostname }}/homebrew-bottles" +# Linux 用户,请使用以下两句命令 +# export HOMEBREW_CORE_GIT_REMOTE="https://{{ site.hostname }}/git/homebrew/linuxbrew-core.git" +# export HOMEBREW_BOTTLE_DOMAIN="https://{{ site.hostname }}/linuxbrew-bottles" ``` -并编辑其中的 `install.sh`: +最后,在终端运行以下命令以安装 Homebrew/Linuxbrew: ```bash -BREW_REPO="https://github.com/Homebrew/brew" -# 改成: -BREW_REPO="https://{{ site.hostname }}/git/homebrew/brew.git" -``` - -接着,运行 `install.sh` 以安装 Homebrew: - -```bash -# brew 运行时会从 Homebrew bottles 中拉取一个包 -export HOMEBREW_BOTTLE_DOMAIN=https://{{ site.hostname }}/homebrew-bottles -# 如果是 Linux 用户,请使用以下这句命令 -# export HOMEBREW_BOTTLE_DOMAIN=https://{{ site.hostname }}/linuxbrew-bottles -export HOMEBREW_CORE_GIT_REMOTE=https://{{ site.hostname }}/git/homebrew/homebrew-core.git -bash install.sh +# 获取并修改安装脚本中的仓库源,运行以安装 Homebrew/Linuxbrew +/bin/bash -c "$( + curl -fsSL https://github.com/Homebrew/install/raw/master/install.sh | + sed 's|^BREW_REPO=.*$|BREW_REPO="https://{{ site.hostname }}/git/homebrew/brew.git"|g' +)" ``` 这样在首次安装的时候也可以使用镜像。 -### 替换现有上游 +_* Linux 用户需要注意,安装 Linuxbrew 后请将相关路径加入自己的环境变量中:_ ```bash -# brew 程序本身,Homebrew/Linuxbrew 相同 -git -C "$(brew --repo)" remote set-url origin https://{{ site.hostname }}/git/homebrew/brew.git +test -d ~/.linuxbrew && eval "$(~/.linuxbrew/bin/brew shellenv)" +test -d /home/linuxbrew/.linuxbrew && eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)" +test -r ~/.bash_profile && echo "eval \"\$($(brew --prefix)/bin/brew shellenv)\"" >>~/.bash_profile +test -r ~/.profile && echo "eval \"\$($(brew --prefix)/bin/brew shellenv)\"" >>~/.profile +test -r ~/.zshrc && echo "eval \"\$($(brew --prefix)/bin/brew shellenv)\"" >>~/.zshrc +``` -# 以下针对 mac OS 系统上的 Homebrew +_参考了 [https://docs.brew.sh/Homebrew-on-Linux](https://docs.brew.sh/Homebrew-on-Linux)。_ + +### 替换现有仓库上游 + +替换 brew 程序本身的源,Homebrew/Linuxbrew 相同: + +```bash +git -C "$(brew --repo)" remote set-url origin https://{{ site.hostname }}/git/homebrew/brew.git +``` + +以下针对 macOS 系统上的 Homebrew +```bash +# 手动设置 git -C "$(brew --repo homebrew/core)" remote set-url origin https://{{ site.hostname }}/git/homebrew/homebrew-core.git git -C "$(brew --repo homebrew/cask)" remote set-url origin https://{{ site.hostname }}/git/homebrew/homebrew-cask.git git -C "$(brew --repo homebrew/cask-fonts)" remote set-url origin https://{{ site.hostname }}/git/homebrew/homebrew-cask-fonts.git git -C "$(brew --repo homebrew/cask-drivers)" remote set-url origin https://{{ site.hostname }}/git/homebrew/homebrew-cask-drivers.git git -C "$(brew --repo homebrew/cask-versions)" remote set-url origin https://{{ site.hostname }}/git/homebrew/homebrew-cask-versions.git -# 以下针对 Linux 系统上的 Linuxbrew -git -C "$(brew --repo homebrew/core)" remote set-url origin https://{{ site.hostname }}/git/homebrew/linuxbrew-core.git - -# 更换后测试工作是否正常 -brew update +# 或使用下面的几行命令自动设置 +BREW_TAPS="$(brew tap)" +for tap in core cask{,-fonts,-drivers,-versions}; do + if echo "$BREW_TAPS" | grep -qE "^homebrew/${tap}\$"; then + # 将已有 tap 的上游设置为 tuna 并设置 auto update (原 auto update 只针对托管在 GitHub 上的上游有效) + git -C "$(brew --repo homebrew/${tap})" remote set-url origin https://{{ site.hostname }}/git/homebrew/homebrew-${tap}.git + git -C "$(brew --repo homebrew/${tap})" config homebrew.forceautoupdate true + else # 在 tap 缺失时自动安装(如不需要请删除此行和下面一行) + brew tap --force-auto-update homebrew/${tap} https://{{ site.hostname }}/git/homebrew/homebrew-${tap}.git + fi +done ``` -### 复原 +以下针对 Linux 系统上的 Linuxbrew: +```bash +git -C "$(brew --repo homebrew/core)" remote set-url origin https://{{ site.hostname }}/git/homebrew/linuxbrew-core.git +``` -_(感谢Snowonion Lee提供说明)_ +更换上游后需重新设置 git 仓库 HEAD: +```bash +brew update-reset +``` + +### 复原仓库上游 + +_(感谢 Snowonion Lee 提供说明)_ ```bash # brew 程序本身,Homebrew/Linuxbrew 相同 git -C "$(brew --repo)" remote set-url origin https://github.com/Homebrew/brew.git -# 以下针对 mac OS 系统上的 Homebrew -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 -git -C "$(brew --repo homebrew/cask-fonts)" remote set-url origin https://github.com/Homebrew/homebrew-cask-fonts.git -git -C "$(brew --repo homebrew/cask-drivers)" remote set-url origin https://github.com/Homebrew/homebrew-cask-drivers.git -git -C "$(brew --repo homebrew/cask-versions)" remote set-url origin https://github.com/Homebrew/homebrew-cask-versions.git +# 以下针对 macOS 系统上的 Homebrew +BREW_TAPS="$(brew tap)" +for tap in core cask{,-fonts,-drivers,-versions}; do + if echo "$BREW_TAPS" | grep -qE "^homebrew/${tap}\$"; then + git -C "$(brew --repo homebrew/${tap})" remote set-url origin https://github.com/Homebrew/homebrew-${tap}.git + fi +done # 以下针对 Linux 系统上的 Linuxbrew git -C "$(brew --repo homebrew/core)" remote set-url origin https://github.com/Homebrew/linuxbrew-core.git -# 更换后测试工作是否正常 -brew update +# 重新设置 git 仓库 HEAD +brew update-reset ``` - -### 安装 Linuxbrew 时使用 tuna 镜像 - -#### 使用 Homebrew/install - -Linuxbrew/install 现在已经废弃,被合并入 Homebrew/install。故可遵循「首次安装Homebrew」进行操作。 - -在安装 Linuxbrew 前,需要安装 git。 - -#### 直接安装 brew - -参考了 [https://docs.brew.sh/Homebrew-on-Linux](https://docs.brew.sh/Homebrew-on-Linux) - -直接运行以下命令 - -```bash -git clone https://{{ site.hostname }}/git/homebrew/brew.git ~/.linuxbrew/Homebrew -mkdir ~/.linuxbrew/bin -ln -s ~/.linuxbrew/Homebrew/bin/brew ~/.linuxbrew/bin -export HOMEBREW_CORE_GIT_REMOTE=https://{{ site.hostname }}/git/homebrew/homebrew-core.git -export HOMEBREW_BOTTLE_DOMAIN=https://{{ site.hostname }}/linuxbrew-bottles -eval $(~/.linuxbrew/bin/brew shellenv) -brew update -``` - -需要注意运行后请将相关路径加入自己的环境变量中。