> 文章列表 > Mac配置homebrew

Mac配置homebrew

Mac配置homebrew

mac配置homebrew

Homebrew是一款Mac OS平台下的软件包管理工具,拥有安装、卸载、更新、查看、搜索等很多实用的功能。简单的一条指令,就可以实现包管理,而不用你关心各种依赖和文件路径的情况,十分方便快捷。Homebrew主要有四个部分组成: brew、homebrew-core 、homebrew-bottles、homebrew-cask。

名称 说明
brew Homebrew 源代码仓库
homebrew-core Homebrew 核心软件仓库
homebrew-bottles Homebrew 预编译二进制软件包
homebrew-cask MacOS 客户端应用

安装

个人建议官网安装:https://brew.sh/index_zh-cn

开源安装脚本库:CunKai/HomebrewCN

intel芯片:

/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"

M1芯片:

/bin/zsh -c "$(curl -fsSL https://gitee.com/huwei1024/HomebrewCN/raw/master/Homebrew.sh)"

因为M1芯片的包安装位置不在是以前的/usr/local/

而是/opt/homebrew,所以要将配置文件里的环境变量改过来

# 首先进入根目录
cd ~
# 创建.zshrc文件
touch .zshrc
# 打开文件进行编辑
open -e .zshrc
# 如果有旧的环境就修改,没有就新增
export PATH="/opt/homebrew/bin:$PATH"
export PATH="/opt/homebrew/sbin:$PATH"
# 使用command + s
# 生效环境变量
source .zshrc
# 测试
brew -v

Homebrew更新国内源或重置官方源

打印当前的源

cd "$(brew --repo)"
git remote -v
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git remote -v
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-cask"
git remote -v

切换为清华源

# 替换brew.git
cd "$(brew --repo)" && git remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git
# 替换homebrew-core.git
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core" && git remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/homebrew-core.git
# 替换homebrew-cask.git
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-cask" && git remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/homebrew-cask.git
# zsh 终端配置
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles' >> ~/.zshrc
# 修改使其立即生效
source ~/.zshrc
# bash 终端配置
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles' >> ~/.bash_profile
# 修改使其立即生效
source ~/.bash_profile
# 刷新源
brew update

切换腾讯源

# 替换brew.git
cd "$(brew --repo)" && git remote set-url origin https://mirrors.cloud.tencent.com/git/homebrew/brew.git
# 替换homebrew-core.git
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core" && git remote set-url origin https://mirrors.cloud.tencent.com/homebrew-core.git
# 替换homebrew-cask.git
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-cask" && git remote set-url origin https://mirrors.cloud.tencent.com/homebrew-cask.git
# zsh 终端配置
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.cloud.tencent.com/homebrew-bottles' >> ~/.zshrc
# 修改使其立即生效
source ~/.zshrc
# bash 终端配置
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.cloud.tencent.com/homebrew-bottles' >> ~/.bash_profile
# 修改使其立即生效
source ~/.bash_profile
# 刷新源
brew update

切换为中科大镜像源

# 替换brew.git
cd "$(brew --repo)" && git remote set-url origin https://mirrors.ustc.edu.cn/brew.git
# 替换homebrew-core.git
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core" && git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git
# 替换homebrew-cask.git
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-cask" && git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-cask.git
# zsh 终端配置
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.zshrc
# 修改使其立即生效
source ~/.zshrc
# bash 终端配置
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.bash_profile
# 修改使其立即生效
source ~/.bash_profile
# 刷新源
brew update

切换为阿里镜像源

# 替换brew.git
cd "$(brew --repo)" && git remote set-url origin https://mirrors.aliyun.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
# zsh 终端配置
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.aliyun.com/homebrew/homebrew-bottles' >> ~/.zshrc
# 修改使其立即生效
source ~/.zshrc
# bash 终端配置
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.aliyun.com/homebrew/homebrew-bottles' >> ~/.bash_profile
# 修改使其立即生效
source ~/.bash_profile
# 刷新源
brew update

切换为官方镜像

# 重置 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-cask.git 为官方源
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-cask" && git remote set-url origin https://github.com/Homebrew/homebrew-cask
# zsh 注释掉 HOMEBREW_BOTTLE_DOMAIN 配置,前面加#符号就行
vi ~/.zshrc
export HOMEBREW_BOTTLE_DOMAIN=xxxxxxxxx
# 修改使其立即生效
source ~/.zshrc
# bash 注释掉 HOMEBREW_BOTTLE_DOMAIN 配置,前面加#就行
vi ~/.bash_profile
export HOMEBREW_BOTTLE_DOMAIN=xxxxxxxxx
# 修改使其立即生效
source ~/.bash_profile
# 刷新源
brew update

常用命令

brew –help             #查看brew的帮助
brew install git       #安装git软件
brew install --cask cakebrew #安装UI界面的软件
brew uninstall git     #卸载git软件
brew search git        #搜索git软件
brew list              #显示已经安装软件列表
brew list python3      #显示已安装软件的安装路径
brew update            #更新索引,把所有的Formula目录更新,并且会对本机已经安装并有更新的软件用*标明。
brew upgrade git       #更新Git软件
brew upgrade           #更新所有旧软件
brew [info | home] [FORMULA…] #查看软件信息
brew cleanup -n        #查看可清理的旧版本,不执行实际操作。
brew cleanup git       #清理指定包(Git)的旧版本
brew cleanup           #清理所有包的老旧版本
brew cleanup --prune=all #缓存清理,brew下载的所有安装包(/Users/zhanglei/Library/Caches/Homebrew)
brew outdated          #查看那些已安装的程序需要更新
brew home  *           #用浏览器打开
brew info *            #显示软件内容信息
brew deps *            #显示包依赖(如:brew deps pango --tree)
brew prune	           #清理无效项
brew config            #查看brew配置
brew unlink php@7.4      #删除软链接
brew link php@8.1        #新建软链接
brew link --overwrite php@8.1 #强制新建软链接--overwrite是强制的意思
brew services list     #可以查看当前已管理的服务,并且可看到启动状态。
brew services cleanup  #清除已卸载无用的启动配置文件。
# 锁定不想更新的包
brew pin php56      # 锁定某个包
brew unpin php56    # 取消锁定
brew info                # 显示安装了包数量,文件数量,和总占用空间
brew deps --installed --tree   # 查看已安装的包的依赖,树形显示

完整卸载一个brew安装的软件,包括其依赖.

使用brew uninstall git卸载软件只会卸载软件本身而不会同时卸载其依赖包,使用以下命令可完全卸载,并且不会影响到其他软件。第一步:brew tap beeftornado/rmtree
第二步:brew rmtree git
第三步:brew cleanup