本文為作者原創,轉載請注明出處(http://www.cnblogs.com/mar-q/)by 負贔屓
比較少用brew,只有之前安裝Opencv的時候用過一次,后面有人問我怎么裝,於是幫他研究了一下。MacOS的brew其實就是通過兩個git倉庫(brew和homebrew-core)來實現的源更新機制,如果程序員沒有科學的Internet方法,就只好換國內源了,國內brew源不是很多,首選中科大或者清華的源:
一、更換
下面這個更換步驟應該是大部分更換brew源的文章里都會介紹的,已經做了還存在bug的可以跳過。
###首選中科大源 cd "$(brew --repo)" git remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core" git remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git #bottles是Homebrew預編譯二進制軟件包 echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.bash_profile source ~/.bash_profile ###清華測試git源無法訪問,可能已經失效 cd "$(brew --repo)" git remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core" git remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles' >> ~/.bash_profile source ~/.bash_profile ###處理完成后升級 brew update ###重置 cd "$(brew --repo)” git remote set-url origin https://github.com/Homebrew/brew.git cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core” git remote set-url origin https://github.com/Homebrew/homebrew-core.git #最后刪掉~/.bash_profile的那句話
二、bug
無法執行brew update,或者更新后報錯:
error: Unable to get pack file http://mirrors.ustc.edu.cn/homebrew-core.git/objects/pack/pack-88812c2ec49654184974c8d70848953ff76433ae.pack The requested URL returned error: 416 Requested Range Not Satisfiable error: Unable to find 6bcbfc51973a7d4891676c15b06ec24e54b74e30 under http://mirrors.ustc.edu.cn/homebrew-core.git Cannot obtain needed object 6bcbfc51973a7d4891676c15b06ec24e54b74e30 error: fetch failed.
這是因為git更換remote url后倉庫更新狀況不一致導致的
#診斷問題 brew doctor
#reset倉庫的Head cd "$(brew --repo)” git fetch git reset --hard origin/master cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core” git fetch git reset --hard origin/master
#最后別忘了update brew update
如果依然報錯,或者執行git fetch就報錯,只好進到倉庫里刪東西啦:
在這個pack中會出現一個未完成的.tmp文件,這時候需要把它mv掉
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core mv .git/objects/pack/pack-88812c2ec49654184974c8d70848953ff76433ae.idx /tmp mv .git/objects/pack/pack-88812c2ec49654184974c8d70848953ff76433ae.pack.temp /tmp
或者在github的issue里據說還有一個方案:
git fsck
git gc
再次執行git fetch
三、終極解決方案
很多同學這么配置了依舊沒有生效,那就介紹兩個大招吧:
- 搭梯子
- 重新裝一波home-brew,參見官網https://brew.sh/
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
而且重裝后,前面的配置依舊生效,可以使用國內的源,因此推測,更新了源,而且reset了git head也不能執行brew update,那么很有可能是你的brew版本不兼容了。
不過這樣做有一個問題:會吃掉一部分磁盤空間(我的大概1g)