Ken的杂谈
  • Ken的杂谈 (current)
  • 关于
  • 杂谈
    Java Spring Spring Boot Spring Cloud MyBatis C# .NET Core .NET ASP.NET Core ASP.NET ClassLibrary Mono 数据库 MySQL SQL Server 网络 缓存 Web Linux CentOS Ubuntu macOS Windows openEuler Nginx ServiceStack JS/JQ 正则 工具 IDE Grafana Exceptions CI/CD Solution 微服务 Arch Docker 杂谈
  • 系列
    Java 快速入门系列教程 Spring Boot 入门教程 Spring Boot 2.x 入门教程 Spring Cloud 入门教程 .NET Core 快速入门教程 ASP.NET Core 2.1 入门教程 CentOS 7 快速上手教程 Ubuntu快速上手入门教程 Hyper-V基础教程 Docker入门教程
  • GitHub

在macOS上安装&配置Homebrew

macOS @ 2023-02-15 23:25:10 · 阅读:(26693)

一、前言

Homebrew是一个开源的包管理工具,可以简化macOS环境下软件的安装,卸载、更新等等,尤其是对于开发工具的安装、管理非常有利于开发者便捷管理开发环境,另外Homebrew也可以用于Linux,不过由于Linux已经有了更成熟的yum、apt-get、dnf等包管理工具,Homebrew在macOS上更为流行和好用

1、本文主要内容

  • Homebrew安装与介绍
  • Homebrew国内镜像源安装
  • Homebrew常用命令说明
  • Homebrew源替换为国内镜像进行加速

2、本文环境信息

工具 本文环境 适用范围
Homebrew 3.6.21 *
操作系统 macOS 13 (Ventura) macOS 12 +

二、Homebrew安装

1、安装Git

Homebrew依赖Git进行包管理,在安装之前要先安装Git
从sourceforge下载安装包进行安装: https://sourceforge.net/projects/git-osx-installer/

# 查看Git版本以确认是否安装成功
git -v

2、Homebrew安装

2.1、官方源安装
从官网:https://brew.sh/ 复制安装脚本,执行即可

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

2.2、镜像源安装
Homebrew官方安装脚本支持镜像源安装,需要提前设置环境变量,这里以阿里云镜像为示例
需要注意的是,环境变量的URL地址是不带.git后缀的,因为install脚本

# zsh设置环境变量(适用zsh用户)
echo 'export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.aliyun.com/homebrew/brew"' >> ~/.zshrc
echo 'export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.aliyun.com/homebrew/homebrew-core"' >> ~/.zshrc
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.aliyun.com/homebrew/homebrew-bottles' >> ~/.zshrc
source ~/.zshrc

# zsh设置环境变量(适用zsh用户)
echo 'export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.aliyun.com/homebrew/brew"' >> ~/.bash_profile
echo 'export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.aliyun.com/homebrew/homebrew-core"' >> ~/.bash_profile
echo 'export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.aliyun.com/homebrew/homebrew-bottles"' >> ~/.bash_profile
source ~/.bash_profile

执行安装

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

# 安装过程中会看到以下提示,说明配置的镜像生效了
==> HOMEBREW_BREW_GIT_REMOTE is set to a non-default URL:
https://mirrors.aliyun.com/homebrew/brew.git will be used as the Homebrew/brew Git remote.
==> HOMEBREW_CORE_GIT_REMOTE is set to a non-default URL:
https://mirrors.aliyun.com/homebrew/homebrew-cask.git will be used as the Homebrew/homebrew-core Git remote.

3、查看基本信息

# 查看版本
brew -v

# 查看安装路径
brew --repo

三、Homebrew镜像加速

brew的软件源默认在github,由于网络问题,安装时会出现特别慢的情况,修改为国内镜像以达到加速效果

1、查看当前源

# 查看 brew 当前源
git -C "$(brew --repo)" remote -v

# 查看 homebrew-core 当前源
git -C "$(brew --repo homebrew/core)" remote -v

# 查看 homebrew-cask 当前源
git -C "$(brew --repo homebrew/cask)" remote -v

# 输出示例
origin    https://github.com/Homebrew/***brew***.git (fetch)
origin    https://github.com/Homebrew/***brew***.git (push)

2、修改为国内镜像

2.1、修改为Aliyun镜像(推荐)

# 修改 brew 源
git -C "$(brew --repo)" remote set-url origin https://mirrors.aliyun.com/homebrew/brew.git

# 修改 homebrew-core 源
git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.aliyun.com/homebrew/homebrew-core.git

# 修改 homebrew-cask 源 (可选)
git -C "$(brew --repo homebrew/cask)" remote set-url origin https://mirrors.aliyun.com/homebrew/homebrew-cask.git

# zsh 修改 homebrew bintray 源(适用zsh用户)
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.aliyun.com/homebrew/homebrew-bottles' >> ~/.zshrc
source ~/.zshrc

# bash 修改 homebrew bintray 源(适用原生bash用户)
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.aliyun.com/homebrew/homebrew-bottles' >> ~/.bash_profile
source ~/.bash_profile

2.2、修改为中科大镜像(备选)

# 修改 brew 源
git -C "$(brew --repo)" remote set-url origin https://mirrors.ustc.edu.cn/brew.git

# 修改 homebrew-core 源
git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git

# 修改 homebrew-cask 源 (可选)
git -C "$(brew --repo homebrew/cask)" remote set-url origin https://mirrors.ustc.edu.cn/homebrew-cask.git

# zsh 修改 homebrew bintray 源(适用zsh用户)
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.zshrc
source ~/.zshrc

# bash 修改 homebrew bintray 源(适用原生bash用户)
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.bash_profile
source ~/.bash_profile

3、更新brew

brew update

四、Homebrew常用命令

1、安装软件

# 搜索软件
brew search mysql

# 安装默认版本
brew install mysql

# 安装指定版本
brew mysql mysql@5.7

# 安装桌面软件
brew install --cask firefox

2、软件管理

# 查看已安装软件
brew list

# 查看软件信息
brew info mysql

# 查看软件安装路径
brew list mysql

# 升级所有软件
brew upgrade 

# 升级指定软件
brew upgrade wget 

# 卸载软件
brew uninstall wget

3、服务管理

# 查看所有服务
brew services list 

# 查看指定服务
brew services info mysql

# 启动服务
brew services start mysql 

# 重启服务
brew services restart mysql 

# 停用服务
brew services stop mysql 

# 移除服务
brew services remove mysql

# 移除所有未使用服务
brew services cleanup

4、其他

# 检查Homebrew
brew doctor

# 升级Homebrew
brew update

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

五、备注

1、可能碰到的问题

homebrew-cask不存在
问题现象

no such file or directory: /opt/homebrew/Library/Taps/homebrew/homebrew-cask

问题原因
Homebrew安装时,默认是不会安装Homebrew cask的,当我们首次使用brew install —cask xxx命令时才会有
这种情况通过Homebrew cask任意安装一个软件即可

brew install --cask firefox

2、相关阅读

https://mirrors.ustc.edu.cn/help/brew.git.html
https://mirrors.tuna.tsinghua.edu.cn/help/homebrew/


Ken的杂谈

本文由 ken.io 创作,采用CC BY 3.0 CN协议 进行许可。 可自由转载、引用、甚至修改,但需署名作者且注明出处。

macOS

随笔目录


    © Copyright 2012-2025 Ken的杂谈

    豫ICP备10025012号

    ASP.NET Core(6.0) on Linux