因為最近入手一個新款的Mac,M1芯片的,安裝了好久的homebrew,一直出現問題,也是參考了好多的文章,現在終于安裝成功了,下面給大家分享一下安裝的教程!!!
====================================================================
本文簡單的介紹了M1芯片Mac上安裝homebrew的方法,

文章目錄
- 一、腳本說明
- (1)安裝 ARM 版 Homebrew
- 二、安裝說明(建議先看一遍再操作)
- 其他的安裝形式
- (1)安裝 X86 版 Homebrew
- (2)多版本共存
- 三、設定鏡像
- 3.1 中科大源
- 3.2 清華大學源
- 3.3 恢復默認源
- 四、其他相關
- 4.1 cask
- 4.2 如何卸載Homebrew
- 五、總結
一、腳本說明
Homebrew是一款包管理工具,目前支持macOS和linux系統,主要有四個部分組成: brew、homebrew-core 、homebrew-cask、homebrew-bottles,
(1)安裝 ARM 版 Homebrew
ARM版Homebrew需要安裝在/opt/homebrew路徑下,早期的時候需要手動創建目錄執行命令,目前使用最新腳本不需要手動操作,
直接執行:Homebrew默認安裝腳本:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
如果你等待一段時間之后遇到下面提示,就說明無法訪問官方腳本地址:
curl: (7) Failed to connect to raw.githubusercontent.com port 443: Connection refused
請按照下面 「安裝說明」中步驟執行,
二、安裝說明(建議先看一遍再操作)
(1)提前設定homebrew-core鏡像源并執行:
安裝腳本的好處是可穩定訪問,安裝默認使用中科大鏡像:
# 中科大
HOMEBREW_CORE_GIT_REMOTE=https://mirrors.ustc.edu.cn/homebrew-core.git
/bin/bash -c "$(curl -fsSL https://cdn.jsdelivr.net/gh/ineo6/homebrew-install/install.sh)"
然后還需要設定下環境變數,在~/.zshrc或者~/.bashrc檔案末尾追加下面代碼,
export PATH=/opt/homebrew/bin:$PATH
注意:如果你確定使用的終端是zsh,那請編輯.zshrc檔案,否則請操作.bashrc檔案,
以.bashrc為例,在終端執行下面代碼可以直接追加內容并讓檔案生效:
echo export PATH=/opt/homebrew/bin:$PATH >> ~/.bashrc
# 重啟一下
source ~/.bashrc

如果命令執行中卡在下面資訊:
==> 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
如果出現successful就表示成功

成功執行之后繼續執行前文的安裝命令:
/bin/bash -c "$(curl -fsSL https://cdn.jsdelivr.net/gh/ineo6/homebrew-install/install.sh)"
最后看到==> Installation successful!就說明安裝成功了,

最后更新下:
brew update
更新成功的話,如圖所示:

如果更新的時候出現
zsh: command not found: brew
就執行下面這句腳本
/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"

然后按照下面的細節執行:

最終成功的截圖,如圖所示:

其他的安裝形式
(1)安裝 X86 版 Homebrew
因為目前很多軟體包沒有支持ARM架構,我們也可以考慮使用x86版的Homebrew,
在命令前面添加arch -x86_64,就可以按X86模式執行該命令,比如:
# 設定homebrew-core鏡像
HOMEBREW_CORE_GIT_REMOTE=https://mirrors.ustc.edu.cn/homebrew-core.git
arch -x86_64 /bin/bash -c "$(curl -fsSL https://cdn.jsdelivr.net/gh/ineo6/homebrew-install/install.sh)"
(2)多版本共存
如果你同時安裝了ARM和X86兩個版本,那你需要設定別名,把命令區分開,
同樣是.zshrc或者.bashrc里面添加:
alias abrew='arch -arm64 /opt/homebrew/bin/brew'
alias ibrew='arch -x86_64 /usr/local/bin/brew'
abrew、ibrew可以根據你的喜好自定義,
然后再執行source ~/.zshrc或source ~/.bashrc命令更新檔案,
三、設定鏡像
brew、homebrew/core是必備專案,homebrew/cask、homebrew/bottles按需設定,
通過 brew config 命令可以查看相關配置資訊,
3.1 中科大源
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
# 長期替換homebrew-bottles
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.bash_profile
source ~/.bash_profile
注意bottles可以臨時設定,在終端執行下面命令:
export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles
3.2 清華大學源
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
# 長期替換homebrew-bottles
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles' >> ~/.bash_profile
source ~/.bash_profile
3.3 恢復默認源
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配置只能手動洗掉,將 ~/.bash_profile 檔案中的 HOMEBREW_BOTTLE_DOMAIN=https://mirrors.xxx.com內容洗掉,并執行 source ~/.bash_profile,
至此,安裝和設定操作都完成了,
四、其他相關
4.1 cask
目前cask是從GitHub上讀取軟體源,而GitHub Api對訪問有限制,如果使用比較頻繁的話,可以申請Api Token,然后在環境變數中配置到HOMEBREW_GITHUB_API_TOKEN,
在.bash_profile中追加:
export HOMEBREW_GITHUB_API_TOKEN=yourtoken
注意:因為cask是從GitHub下載軟體,所以目前是無法加速的,
4.2 如何卸載Homebrew
使用官方腳本同樣會遇到uninstall地址無法訪問問題,可以使用下面腳本:
/bin/bash -c "$(curl -fsSL https://cdn.jsdelivr.net/gh/ineo6/homebrew-install/uninstall.sh)"
五、總結
在前面的程序中我們把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"
更新后的方案使用HOMEBREW_CORE_GIT_REMOTE來預設homebrew-core源,不過這個設定方法很可能無效!!!
參考網站:
清華大學開源軟體鏡像站
中科大源
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/249449.html
標籤:其他
上一篇:shell2:shell變數
下一篇:sqoop安裝
