0%

Homebrew 安装简介

官网 http://brew.sh/index_zh-cn.html

简介

Homebrew: 软件包管理
Homebrew-Core: 核心软件仓库
Homebrew-Cask: macOS 应用和大型二进制文件
Homebrew-Bottles: 预编译二进制软件包

  • homebrew/homebrew-core
  • homebrew/homebrew-dupes
  • homebrew/homebrew-php
  • homebrew/homebrew-science
  • homebrew/homebrew-nginx
  • homebrew/homebrew-apache
  • homebrew/homebrew-portable

安装Homebrew

homebrew主要分两部分:git repo(位于GitHub)和二进制bottles(位于bintray)
由于你懂的安装、更新一直很慢,提供两种方法,我更喜欢 proxy,这种配置更为方便~

proxy

proxy 方法安装比较方便,在拉取仓库时设置proxy,会显著提高速度

1
2
3
git config --global https.proxy http://127.0.0.1:1080
git config --global https.proxy https://127.0.0.1:1080
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"`

修改安装脚本

修改脚本主要将github的源换成镜像的地址,方便无proxy的用户

install 为自修改后的内容

1
/usr/bin/ruby -e "$(curl -fsSL https://gist.githubusercontent.com/l1b0k/329b70a22a469a5f73d6a292005ef9da/raw/b3318d9568229f07407b6cb84ba24fecd75785b5/install)"`

👇是具体分析过程


脚本下载到本地,并且编辑、保存

1
2
3
4
5
6
7
8
9
...
HOMEBREW_PREFIX = "/usr/local".freeze
HOMEBREW_REPOSITORY = "/usr/local/Homebrew".freeze
HOMEBREW_CACHE = "#{ENV["HOME"]}/Library/Caches/Homebrew".freeze
HOMEBREW_OLD_CACHE = "/Library/Caches/Homebrew".freeze
- BREW_REPO = "https://github.com/Homebrew/brew".freeze
+ BREW_REPO = "https://mirrors.ustc.edu.cn/brew.git".freeze
CORE_TAP_REPO = "https://github.com/Homebrew/homebrew-core".freeze
...

⚠️!CORE_TAP_REPO 在这里修改是无效的(已经有git的用户,这个代码逻辑走不到。。)
在安装脚本大约311 行,会调用update指令,调用时会初始化/usr/local/Homebrew/Library/Tap,这时brew就会自动更新homebrew-core,导致直接连接github去更新…

处理办法为主动设置仓库地址:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
    system git, "reset", "--hard", "origin/master"

system "ln", "-sf", "#{HOMEBREW_REPOSITORY}/bin/brew", "#{HOMEBREW_PREFIX}/bin/brew"

+ core_tap = "#{HOMEBREW_PREFIX}/Homebrew/Library/Taps/homebrew/homebrew-core"
+ system "/bin/mkdir", "-p", core_tap
+
+ Dir.chdir core_tap do
+ system git, "init", "-q"
+ system git, "config", "remote.origin.url", + "https://mirrors.ustc.edu.cn/homebrew-core.git"
+ system git, "config", "remote.origin.fetch", "+refs/heads/*:refs/remotes/origin/*"
+ system git, "config", "core.autocrlf", "false"
+ args = git, "fetch", "origin", "master:refs/remotes/origin/master",
+ "--tags", "--force"
+ system(*args)
+
+ system git, "reset", "--hard", "origin/master"
+ end

system "#{HOMEBREW_PREFIX}/bin/brew", "update", "--force"

然后执行安装,期间要输入账户密码,经过几分钟就完成安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
➜  Downloads /usr/bin/ruby install
==> This script will install:
/usr/local/bin/brew
/usr/local/share/doc/homebrew
/usr/local/share/man/man1/brew.1
/usr/local/share/zsh/site-functions/_brew
/usr/local/etc/bash_completion.d/brew
/usr/local/Homebrew
...
...
==> Next steps:
- Run `brew help` to get started
- Further documentation:
https://docs.brew.sh
➜ Downloads

换源

无论👆哪种安装方式都可以再执行下

1
2
3
4
5
6
7
8
9
10
11
# 替换brew
cd "$(brew --repo)"
git remote set-url origin https://mirrors.ustc.edu.cn/brew.git

# 替换homebrew-core
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git

# 替换bottles
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.zshrc
source ~/.zshrc

上面使用了zsh,bash请把~/.zshrc改为 ~/.bash_profile

恢复源

1
2
3
4
5
6
7
# 重置brew
cd "$(brew --repo)"
git remote set-url origin https://github.com/Homebrew/brew.git

# 重置homebrew-core
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git remote set-url origin https://github.com/Homebrew/homebrew-core.git

brew 常用指令

⚠️使用前顺手关闭分析跟踪
brew analytics off

更新brew自身
brew update

列出过时的软件包(已安装但不是最新版本)
brew outdated

更新过时的软件包(不跟包名就是更新所有包)
brew upgrade package_name

验证brew是否安装成功(也可以用来检查无效的包和依赖关系有问题的包)
brew doctor

查看软件包信息
brew info package_name

查找软件包
brew search package_name

安装软件包
brew install package_name

卸载软件
brew uninstall package_name

列出已安装的软件包
brew list

列出安装包的内容
brew list package_name

列出软件包的依赖关系
brew deps package_name

列出brew常用命令
brew help

卸载除go 之外的包
ls /usr/local/Cellar/ |grep -v go| xargs brew uninstall --ignore-dependencies

清理过时包和缓存
brew cleanup

10.12 权限修复(@Deprecated)

升级 10.12 Sierra 之后运行 brew update会报 /usr/local 无权写入
10.12 之前 brew 使用 /usr/local 作为主目录,目录所有者是 user
更新10.12之后 /usr/local 这个目录所有者变成了 root
Fix:
sudo chown $(whoami):admin /usr/local

之后再运行 brew update 就能正常运行,同时 Homebrew 已经提供对 10.12的支持,更新后主目录迁移到 /usr/local/Homebrew ,现在再把权限改回来

sudo chown root:wheel /usr/local

使用扩展源(caskroom)

brew 使用 tap 命令来处理软件源
homebrew/core 是默认的源,装完就能看到

1
2
➜  ~ brew tap
homebrew/core

caskroom 会包含其他商业的软件

  • java
  • goland
  • atom
  • intellij-idea
  • google-chrome
  • mpv

caskroom/version包含历史版本

  • java8

安装与换源

通过brew tap caskroom/cask 就可以添加

1
2
3
4
5
6
7
➜  ~ brew tap caskroom/cask
==> Tapping caskroom/cask
Cloning into '/usr/local/Homebrew/Library/Taps/caskroom/homebrew-cask'...
...
➜ ~ brew tap
caskroom/cask
homebrew/core

执行tap时,就会拉取仓库,如果嫌慢可以手动创建路径,配置镜像仓库(过程略)

1
2
3
4
5
6
7
# 替换 homebrew-cask
cd "$(brew --repo)"/Library/Taps/caskroom/homebrew-cask
git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-cask.git

# 恢复 homebrew-cask
cd "$(brew --repo)"/Library/Taps/caskroom/homebrew-cask
git remote set-url origin https://github.com/caskroom/homebrew-cask.git

常用命令

brew cask install goland

brew cask uninstall goland