概述

brew 官网:https://brew.sh/
Homebrew 是 macOS 下的包管理工具,类似于 centos 下的 yum,ubuntu下的apt 可以很方便地进行安装/卸载/更新各种软件包,
macOS软件包管理——Homebrew - 图1

安装

  1. /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

卸载

https://raw.githubusercontent.com/Homebrew/install/master/uninstall
image.png

  1. /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/uninstall.sh)"

升级

  1. brew update-reset

安装软件

以 nginx 为例,执行下面命令即可,安装目录在 /usr/local/Cellar

  1. brew install nginx

更新软件

  1. brew upgrade nginx

卸载软件

  1. brew remove nginx

cask

  1. brew cask install firefox

其他命令

  1. brew list # 列出当前安装的软件
  2. brew search nginx # 查询与 nodejs 相关的可用软件
  3. brew info nginx # 查询 nodejs 的安装信息

安装指定版本

如果需要指定版本,可以在 brew search 查看有没有需要的版本,在 @ 后面指定版本号,例如 brew install thrift@0.9

  1. $ brew list
  2. autoconf gdbm libplist mongodb pkg-config sqlite xz
  3. automake go libtool nginx python storm zookeeper
  4. cmake kibana libusb openssl readline tomcat
  5. coreutils libgpg-error libyaml openssl@1.1 redis tree
  6. elasticsearch libksba maven pcre ruby usbmuxd

eg:

  1. brew install gradle@6

brew services

brew services 是一个非常强大的工具,可以用来管理各种服务的启停,有点像 linux 里面的 services,非常方便,以 elasticsearch 为例

  1. brew install elasticsearch # 安装 elasticsearch
  2. brew services start elasticsearch # 启动 elasticsearch
  3. brew services stop elasticsearch # 停止 elasticsearch
  4. brew services restart elasticsearch # 重启 elasticsearch
  5. brew services list # 列出当前的状态

安装elasticsearch时会告诉使用brew services start elasticsearch可以让服务在后台运行和开机自启动

  1. brew install elasticsearch
  2. ==> Downloading https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.2.4.tar.gz
  3. ######################################################################## 100.0%
  4. ==> Caveats
  5. Data: /usr/local/var/lib/elasticsearch/elasticsearch_baxiang/
  6. Logs: /usr/local/var/log/elasticsearch/elasticsearch_baxiang.log
  7. Plugins: /usr/local/var/elasticsearch/plugins/
  8. Config: /usr/local/etc/elasticsearch/
  9. To have launchd start elasticsearch now and restart at login:
  10. brew services start elasticsearch
  11. Or, if you don't want/need a background service you can just run:
  12. elasticsearch
  13. ==> Summary
  14. 🍺 /usr/local/Cellar/elasticsearch/6.2.4: 112 files, 30.8MB, built in 13 minutes 23 seconds

查看服务状态,其中mongodb,redis开机自启动

  1. $brew services list
  2. Name Status User Plist
  3. elasticsearch stopped
  4. kibana stopped
  5. mongodb started baxiang /Users/baxiang/Library/LaunchAgents/homebrew.mxcl.mongodb.plist
  6. nginx stopped
  7. redis started baxiang /Users/baxiang/Library/LaunchAgents/homebrew.mxcl.redis.plist
  8. tomcat stopped
  9. zookeeper stopped

brew services 服务相关配置以及日志路径
配置路径:/usr/local/etc/

  1. $ ls /usr/local/etc
  2. bash_completion.d mongod.conf openssl@1.1 redis.conf.default
  3. elasticsearch nginx redis-sentinel.conf zookeeper
  4. kibana openssl redis.conf

日志路径:/usr/local/var/log

  1. ls /usr/local/var/log
  2. elasticsearch mongodb nginx redis.log zookeeper

疑难问题

HomeBrew update更新后的问题

  1. /usr/local/Homebrew/Library/Homebrew/global.rb:12:in `require': cannot load such file -- active_support/core_ext/object/blank (LoadError)
  2. from /usr/local/Homebrew/Library/Homebrew/global.rb:12:in `<top (required)>'
  3. from /usr/local/Homebrew/Library/Homebrew/brew.rb:23:in `require_relative'
  4. from /usr/local/Homebrew/Library/Homebrew/brew.rb:23:in `<main>'

执行命令

  1. brew update-reset

源修改

执行brew命令安装的时候,跟3个仓库地址有关
1 brew.git
2 homebrew-core.git
3 homebrew-bottles
把三个仓库地址全部替换成国内Alibaba提供的地址

1 替换brew.git仓库地址

  1. cd "$(brew --repo)"
  2. git remote set-url origin https://mirrors.aliyun.com/homebrew/brew.git
  3. # 还原为官方提供的 brew.git 仓库地址
  4. cd "$(brew --repo)"
  5. git remote set-url origin https://github.com/Homebrew/brew.git

替换homebrew-core.git仓库地址

  1. cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
  2. git remote set-url origin https://mirrors.aliyun.com/homebrew/homebrew-core.git

替换/还原homebrew-bottles,首先查看shell版本

  1. echo $SHELL


如果你的输出结果是 /bin/zsh,zsh终端执行下面的命令

  1. echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.aliyun.com/homebrew/homebrew-bottles' >> ~/.zshrc
  2. source ~/.zshrc

如果你的输出结果是 /bin/bash 执行下面命令:

  1. echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.bash_profile
  2. source ~/.bash_profile

.恢复homebrew国内镜像源配置

  • 重置brew.git

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

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

    1. 将刚添加到~/.bash_profile文件的语句注释掉即可

Updating Homebrew

运行命令brew install node,结果界面一直卡在Updating Homebrew上,有两种解决办法

方法一:关闭brew每次执行命令时的自动更新(推荐)

  1. vim ~/.bash_profile
  2. # 新增一行
  3. export HOMEBREW_NO_AUTO_UPDATE=true

方法二 比较暴力,出现Updating Homebrew的时候ctrl+c一下就行

错误处理

  1. fatal: Unable to create '/usr/local/Homebrew/.git/index.lock': File exists.

直接删除

  1. rm -f /usr/local/Homebrew/.git/index.lock