OS X brew 更换源

什么是 brew

Brew 是OS X 上的一个软件包管理器,有些类似 apt-get 和 yum,它依托于Github。

项目地址: https://github.com/Homebrew/homebrew

使用帮助: http://brew.sh

但是国内有时候真是让人无语,于是还是考虑给 brew 换个国内的源。

brew 的镜像源

更换 brew 源

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
cd /usr/local/
# 关联远程仓库
git remote set-url origin http://mirrors.ustc.edu.cn/homebrew.git

# 临时目录
mkdir ~/tmp/
cd ~/tmp/
git clone http://mirrors.ustc.edu.cn/homebrew.git

# 替换文件
sudo rm -rf /usr/local/.git
sudo rm -rf /usr/local/Library
sudo cp -R homebrew/.git /usr/local/
sudo cp -R homebrew/Library /usr/local/

# 更新
brew update

排错

如果在执行 brew update 后出现如下错误

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
brew update

Stashing your changes:
M .travis.yml
M README.md
M SUPPORTERS.md
M share/doc/homebrew/Bottles.md
M share/doc/homebrew/Brew-Test-Bot-For-Core-Contributors.md
M share/doc/homebrew/El_Capitan_and_Homebrew.md
M share/doc/homebrew/Formula-Cookbook.md
M share/doc/homebrew/Gems,-Eggs-and-Perl-Modules.md
M share/doc/homebrew/Interesting-Taps-&-Branches.md
M share/doc/homebrew/New-Maintainer-Checklist.md
M share/doc/homebrew/Xcode.md
M share/doc/homebrew/brew.1.html
M share/man/man1/brew.1

fatal: Unable to create '/usr/local/.git/index.lock': Permission denied
Cannot save the current index state
Error: Failure while executing: git stash save --include-untracked --quiet

提示权限不足

1
2
3
4
sudo chgrp -R admin /usr/local
# 确保目录归属管理组
sudo chmod -R g+w /usr/local
# 确保管理组可写

最后使用 brew update 就可以更新了。