Mac必备神器Homebrew

本文详细介绍了在Mac上安装Homebrew的全过程,包括使用国内镜像源提升下载速度,以及Homebrew的基本用法。通过替换源,解决安装过程中的网络问题,确保稳定快速的安装体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

安装brew:

git clone git://mirrors.ustc.edu.cn/homebrew-core.git//usr/local/Homebrew/Library/Taps/homebrew/homebrew-core --depth=1

(注意:如果有/usr/local/Homebrew/Library/Taps/homebrew/homebrew-core目录,可以不执行,也可以,直接把这个目录删掉,再执行)

安装brew cask:

git clone git://mirrors.ustc.edu.cn/homebrew-cask.git//usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask --depth=1

(注意:如果有/usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask目录,可以不执行,也可以,直接把这个目录删掉,再执行)

替换成国内源:

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

1、Homebrew是什么?

引用官方的一句话:Homebrew是Mac OS 不可或缺的套件管理器。

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

2、Homebrew的安装方法

官网给出的安装方法:将以下命令粘贴到终端

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

但这种方法并不适用国内的Mac用户,因为网络资源的原因,电脑下载是龟速,实在是无法忍受,不信你自己试试就知道了。

解决下载慢有两个办法:

一是替换镜像源,将下载资源改为国内镜像资源即可(推荐)

二是科学上网,通过全局代理来进行安装,也是解决网络问题的一种方法(不推荐,不爱喝茶)

下面来说一下,怎样替换镜像源:

步骤一: 获取install文件:将以下命令粘贴到终端 + 回车

curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install >> brew_install

步骤二:更改文件中的链接资源,将原有的链接资源替换成清华大学的镜像资源

把这两句用#注释掉

BREW_REPO = “https://github.com/Homebrew/brew“.freeze
CORE_TAP_REPO = “https://github.com/Homebrew/homebrew-core“.freeze

修改为这两句

BREW_REPO = "git://mirrors.ustc.edu.cn/brew.git".freeze
CORE_TAP_REPO = "git://mirrors.ustc.edu.cn/homebrew-core.git".freeze

步骤三:安装,运行修改了的brew_install,然后是漫长的等待

/usr/bin/ruby ~/brew_install

执行之后你会看到如下界面:

出现这个因为源不通,代码无法下载到本地,解决方法是更换成国内镜像源,执行如下命令,更换到中科院的镜像:

git clone git://mirrors.ustc.edu.cn/homebrew-core.git//usr/local/Homebrew/Library/Taps/homebrew/homebrew-core --depth=1

然后把Homebrew-core的镜像地址也设置为中科院的国内镜像

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

执行更新brew命令:

brew update


接着执行brew检测命令:

brew doctor

如上图出现警告是正常情况,因为我们更改了镜像源。到目前为止,海外用户或者已经设置系统全局代理的用户就可以使用brew安装你所需要的软件了。国内用户咱们继续操作,不然龟速下载搞得我想摔电脑!

让我们把默认源替换为国内USTC源:

(1) 替换核心软件仓库:

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

(2) 替换cask软件仓库:

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

(3) 替换Bottle源:

bash用户(shell用户):

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

zsh用户:

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

Homebrew基本用法:

假设需要安装的软件是 wget

操作命令更新 Homebrewbrew update更新所有安装过的软件包brew upgrade更新指定的软件包brew upgrade wget查找软件包brew search wget安装软件包brew install wget卸载软件包brew remove wget列出已安装的软件包brew list查看软件包信息brew info wget列出软件包的依赖关系brew deps wget列出可以更新的软件包brew outdated

卸载方法:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall)"


参考:

Homebrew 中文主页

https://brew.sh/index_zh-cn.html

Homebrew Bottles 源使用帮助

http://mirrors.ustc.edu.cn/help/homebrew-bottles.html

Homebrew Cask 源使用帮助

http://mirrors.ustc.edu.cn/help/homebrew-cask.git.html

Homebrew Core 源使用帮助

http://mirrors.ustc.edu.cn/help/homebrew-core.git.html

Homebrew国内如何自动安装(国内地址)

 

自动脚本(全部国内地址)(在终端中复制粘贴回车下面脚本)

苹果电脑 常规安装脚本(推荐 完全体 几分钟安装完成):

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

苹果电脑 极速安装脚本(精简版 几秒钟安装完成):

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

苹果电脑 卸载脚本:

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

常见错误去下方地址查看

https://gitee.com/cunkai/HomebrewCN/blob/master/error.md


Linux电脑 安装脚本:

rm Homebrew.sh ; wget https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh ; bash Homebrew.sh

Linux电脑 卸载脚本:

rm HomebrewUninstall.sh ; wget https://gitee.com/cunkai/HomebrewCN/raw/master/HomebrewUninstall.sh ; bash HomebrewUninstall.sh
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值