概述
brew 官网:https://brew.sh/
Homebrew 是 macOS 下的包管理工具,类似于 centos 下的 yum,ubuntu下的apt 可以很方便地进行安装/卸载/更新各种软件包,
安装
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
卸载
https://raw.githubusercontent.com/Homebrew/install/master/uninstall
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/uninstall.sh)"
升级
brew update-reset
安装软件
以 nginx 为例,执行下面命令即可,安装目录在 /usr/local/Cellar
brew install nginx
更新软件
brew upgrade nginx
卸载软件
brew remove nginx
cask
brew cask install firefox
其他命令
brew list # 列出当前安装的软件
brew search nginx # 查询与 nodejs 相关的可用软件
brew info nginx # 查询 nodejs 的安装信息
安装指定版本
如果需要指定版本,可以在 brew search 查看有没有需要的版本,在 @ 后面指定版本号,例如 brew install thrift@0.9
$ brew list
autoconf gdbm libplist mongodb pkg-config sqlite xz
automake go libtool nginx python storm zookeeper
cmake kibana libusb openssl readline tomcat
coreutils libgpg-error libyaml openssl@1.1 redis tree
elasticsearch libksba maven pcre ruby usbmuxd
eg:
brew install gradle@6
brew services
brew services 是一个非常强大的工具,可以用来管理各种服务的启停,有点像 linux 里面的 services,非常方便,以 elasticsearch 为例
brew install elasticsearch # 安装 elasticsearch
brew services start elasticsearch # 启动 elasticsearch
brew services stop elasticsearch # 停止 elasticsearch
brew services restart elasticsearch # 重启 elasticsearch
brew services list # 列出当前的状态
安装elasticsearch时会告诉使用brew services start elasticsearch
可以让服务在后台运行和开机自启动
brew install elasticsearch
==> Downloading https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.2.4.tar.gz
######################################################################## 100.0%
==> Caveats
Data: /usr/local/var/lib/elasticsearch/elasticsearch_baxiang/
Logs: /usr/local/var/log/elasticsearch/elasticsearch_baxiang.log
Plugins: /usr/local/var/elasticsearch/plugins/
Config: /usr/local/etc/elasticsearch/
To have launchd start elasticsearch now and restart at login:
brew services start elasticsearch
Or, if you don't want/need a background service you can just run:
elasticsearch
==> Summary
🍺 /usr/local/Cellar/elasticsearch/6.2.4: 112 files, 30.8MB, built in 13 minutes 23 seconds
查看服务状态,其中mongodb,redis开机自启动
$brew services list
Name Status User Plist
elasticsearch stopped
kibana stopped
mongodb started baxiang /Users/baxiang/Library/LaunchAgents/homebrew.mxcl.mongodb.plist
nginx stopped
redis started baxiang /Users/baxiang/Library/LaunchAgents/homebrew.mxcl.redis.plist
tomcat stopped
zookeeper stopped
brew services 服务相关配置以及日志路径
配置路径:/usr/local/etc/
$ ls /usr/local/etc
bash_completion.d mongod.conf openssl@1.1 redis.conf.default
elasticsearch nginx redis-sentinel.conf zookeeper
kibana openssl redis.conf
日志路径:/usr/local/var/log
ls /usr/local/var/log
elasticsearch mongodb nginx redis.log zookeeper
疑难问题
HomeBrew update更新后的问题
/usr/local/Homebrew/Library/Homebrew/global.rb:12:in `require': cannot load such file -- active_support/core_ext/object/blank (LoadError)
from /usr/local/Homebrew/Library/Homebrew/global.rb:12:in `<top (required)>'
from /usr/local/Homebrew/Library/Homebrew/brew.rb:23:in `require_relative'
from /usr/local/Homebrew/Library/Homebrew/brew.rb:23:in `<main>'
执行命令
brew update-reset
源修改
执行brew命令安装的时候,跟3个仓库地址有关
1 brew.git
2 homebrew-core.git
3 homebrew-bottles
把三个仓库地址全部替换成国内Alibaba提供的地址
1 替换brew.git仓库地址
cd "$(brew --repo)"
git remote set-url origin https://mirrors.aliyun.com/homebrew/brew.git
# 还原为官方提供的 brew.git 仓库地址
cd "$(brew --repo)"
git remote set-url origin https://github.com/Homebrew/brew.git
替换homebrew-core.git仓库地址
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git remote set-url origin https://mirrors.aliyun.com/homebrew/homebrew-core.git
替换/还原homebrew-bottles,首先查看shell版本
echo $SHELL
如果你的输出结果是 /bin/zsh,zsh终端执行下面的命令
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.aliyun.com/homebrew/homebrew-bottles' >> ~/.zshrc
source ~/.zshrc
如果你的输出结果是 /bin/bash 执行下面命令:
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.bash_profile
source ~/.bash_profile
.恢复homebrew国内镜像源配置
重置brew.git
cd "$(brew --repo)"
git remote set-url origin https://github.com/Homebrew/brew.git
重置homebrew-core.git
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git remote set-url origin https://github.com/Homebrew/homebrew-core.git
重置homebrew-bottles
将刚添加到~/.bash_profile文件的语句注释掉即可
Updating Homebrew
运行命令brew install node,结果界面一直卡在Updating Homebrew上,有两种解决办法
方法一:关闭brew每次执行命令时的自动更新(推荐)
vim ~/.bash_profile
# 新增一行
export HOMEBREW_NO_AUTO_UPDATE=true
方法二 比较暴力,出现Updating Homebrew的时候ctrl+c一下就行
错误处理
fatal: Unable to create '/usr/local/Homebrew/.git/index.lock': File exists.
直接删除
rm -f /usr/local/Homebrew/.git/index.lock