Mac如何安装Homebrew

打印 上一主题 下一主题

主题 900|帖子 900|积分 2700

一、简介

Homebrew是一款包管理工具,现在支持macOS和Linux体系。主要有四个部分构成:brew、homebrew-core 、homebrew-cask、homebrew-bottles
名称分析brewHomebrew源代码堆栈homebrew-coreHomebrew 核心源homebrew-cask提供 macOS 应用和大型二进制文件的安装homebrew-bottles预编译二进制软件包

二、官方脚本下载安装及设置环境变量

首先我们可以进入官网地址获取下载链接,如下:

在安装完成后如果遇到command not found: brew的错误,一般是环境变量无效,这里则必要使用之前先容使用过的echo $SHELL来确认终端范例,如下

从前每个新用户的默认shell是/bin/bash,那么这里我们必要设置文件为~/.bash_profile,从macOS Catalina(10.15.x) 版开始,Mac使用/bin/zsh作为默认Shell,其对应的设置文件为~/.zshrc
别的我们还需确定下我们电脑CPU的型号,这里可以通过uname -m来检察,如下

M1芯片ARM版Homebrew最终会被安装在/opt/homebrew路径下,而之前Intel芯片的Mac则会被安装到/usr/local/Homebrew路径下。
所以综上所述,我们在设置Homebrew环境变量的时候,必要联合差别的情况来举行相应的设置,如下:
M1芯片




  1. echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zprofile
  2. eval "$(/opt/homebrew/bin/brew shellenv)"
复制代码


  • bash
  1. echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.bash_profile
  2. eval "$(/opt/homebrew/bin/brew shellenv)"
复制代码
Intel芯片



  • zsh
  1. echo 'eval "$(/usr/local/Homebrew/bin/brew shellenv)"' >> ~/.zprofile
  2. eval "$(/usr/local/Homebrew/bin/brew shellenv)"
复制代码


  • bash
  1. echo 'eval "$(/usr/local/Homebrew/bin/brew shellenv)"' >> ~/.bash_profile
  2. eval "$(/usr/local/Homebrew/bin/brew shellenv)"
复制代码
三、GitHub加速

在使用官方脚本举行下载安装时,常常会遇到下面的错误提示:
   curl: (7) Failed to connect to raw.githubusercontent.com port 443: Operation timed out
  由于官方脚本无法使用的原因是raw.githubusercontent.com访问很不稳定,这里我们可以采用写入hosts的方式,可以一定程度解决GitHub资源无法访问的标题。
3.1、Mac

这里我们可以打开/etc/hosts文件,在其中添加相关设置即可,具体操作可参考:


  • 主站:https://github.com/ineo6/hosts
  • 镜像地址:https://gitee.com/ineo6/hosts
最后我们必要革新缓存,如下
  1. sudo killall -HUP mDNSResponder
复制代码
3.2、Windows

别的如果是Windows环境,同理我们将上述文章中内容追加到C:/windows/system32/drivers/etc/hosts文件中,革新缓存即可。
  1. ipconfig /flushdns
复制代码
PS:如果以为必要本身定时去上述hosts文件中的内容太过于麻烦的话,这里推荐可使用通过SwitchHosts来自动更新,操作很简朴,支持跨平台,添加一条hosts规则并启用,然后就可以按设置的时间间隔自动与最新的hosts保持同步了。
四、镜像脚本安装

如果通过官方提供的方式无法成功安装,这里推荐使用Homebrew的镜像脚原来举行安装,这里比较推荐两个,首先是如下脚本:
  1. /bin/bash -c "$(curl -fsSL https://cdn.jsdelivr.net/gh/ineo6/homebrew-install/install.sh)"
复制代码
该脚本用了中科大镜像加速访问,仅修改堆栈地址部分,不会产生安全隐患。
关于中科大所提供的Homebrew镜像服务



  • https://mirrors.ustc.edu.cn/help/brew.git.html?highlight=homebrew
  • https://lug.ustc.edu.cn/wiki/mirrors/help/brew.git/
关于清华所提供的Homebrew镜像服务



  • https://mirror.tuna.tsinghua.edu.cn/help/homebrew/
若出现Error: Checksum mismatch. 错误,报错代码如下
   curl: (56) LibreSSL SSL_read: SSL_ERROR_SYSCALL, errno 54
Error: Checksum mismatch.
Expected: b065e5e3783954f3e65d8d3a6377ca51649bfcfa21b356b0dd70490f74c6bd86
Actual: e8a348fe5d5c2b966bab84052062f0317944122dea5fdfdc84ac6d0bd513c137
Archive: /Users/rocky/Library/Caches/Homebrew/portable-ruby-2.6.3_2.yosemite.bottle.tar.gz
To retry an incomplete download, remove the file above.
Error: Failed to install Homebrew Portable Ruby (and your system version is too old)!
Failed during: /usr/local/bin/brew update

--force
  这里是由于Homebrew目次下的portable-ruby-2.6.3_2.yosemite.bottle.tar.gz文件引起的安装停止,只必要到上面对应的路径里,删掉这个文件,重新实行安装命令即可。
如果上述命令实行卡在如下信息处
   ==> Tapping homebrew/core
Cloning into ‘/opt/homebrew/Library/Taps/homebrew/homebrew-core’…
  请Control+C停止脚本实行如下命令:
  1. cd "$(brew --repo)/Library/Taps/"
  2. mkdir homebrew && cd homebrew
  3. git clone git://mirrors.ustc.edu.cn/homebrew-core.git
复制代码
安装cask 同样也有安装失败或者卡住的标题,解决方法也是一样:
  1. cd "$(brew --repo)/Library/Taps/"
  2. cd homebrew
  3. git clone https://mirrors.ustc.edu.cn/homebrew-cask.git
复制代码
成功实行之后继续实行安装命令,最后看到如何命令就分析安装成功了
   ==> Installation successful!
  最后使用下面的命令举行更新即可
  1. brew update
复制代码
如果遇到解决不了的标题,先卸载Homebrew再重新安装,官方提供的卸载脚本如下:
  1. /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/uninstall.sh)"
复制代码
当然使用官方脚本同样会遇到uninstall地址无法访问标题,那么就可以使用下面脚本:
  1. /bin/bash -c "$(curl -fsSL https://cdn.jsdelivr.net/gh/ineo6/homebrew-install/uninstall.sh)"
复制代码
这里删除不仅可以通过脚本举行卸载删除,还可以在文件夹中举行删除,如下
  1. # 目录替换为真实的brew位置
  2. sudo rm -rf 目录
复制代码
别的在Mac电脑的终端中,还可以实行open /opt/homebrew/打开文件夹界面举行删除。
PS:各个环境下Homebrew的安装路径:



  • Intel安装目次: /usr/local/Homebrew
  • m1 arm安装目次: /opt/homebrew
  • linux安装目次:/home/linuxbrew
第二种方法就更加的轻便了,我们只必要把下面的脚本内容复制到终端实行即可
  1. /bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"
复制代码
然后根据终端中出现的提示举行选择即可,如果后续必要卸载也是比较方便的,如下:
  1. /bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/HomebrewUninstall.sh)"
复制代码
五、更换镜像源

上述先容到,Homebrew通常用来下载安装软件的,但它在下载软件时非常慢。为了提拔下载速度,必要更改Homebrew的安装源,将其替换成国内镜像。
5.1、中科大

这里用由中科大负责托管维护的Homebrew镜像为例。其中brew、homebrew-core是必备项目,homebrew-cask、homebrew-bottles按需设置。可通过 brew config 命令可以检察相关设置信息。
  1. # brew
  2. git -C "$(brew --repo)" remote set-url origin https://mirrors.ustc.edu.cn/brew.git
  3. # homebrew-core
  4. git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git
  5. # homebrew-cask
  6. git -C "$(brew --repo homebrew/cask)" remote set-url origin https://mirrors.ustc.edu.cn/homebrew-cask.git
  7. brew update
复制代码
别的如需设置bottles镜像,还必要设置环境变量,这里设置文件的判断上面已经先容过来,这里就不再赘述了,按照差别的情况选择下面的其中一种设置即可。
  1. # zsh
  2. echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles/bottles' >> ~/.zprofile
  3. source ~/.zprofile
  4. # bash
  5. echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles/bottles' >> ~/.bash_profile
  6. source ~/.bash_profile
复制代码
除了中科大的镜像源外,我们还可以使用其他的镜像源举行设置
5.2、清华

  1. # brew
  2. git -C "$(brew --repo)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git
  3. # homebrew-core
  4. git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git
  5. # homebrew-cask
  6. git -C "$(brew --repo homebrew/cask)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask.git
  7. brew update
复制代码
  1. # zsh
  2. echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/bottles' >> ~/.zprofile
  3. source ~/.zprofile
  4. # bash
  5. echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/bottles' >> ~/.bash_profile
  6. source ~/.bash_profile
复制代码
5.3、腾讯

  1. # brew
  2. git -C "$(brew --repo)" remote set-url origin https://mirrors.cloud.tencent.com/homebrew/brew.git
  3. # homebrew-core
  4. git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.cloud.tencent.com/homebrew/homebrew-core.git
  5. # homebrew-cask
  6. git -C "$(brew --repo homebrew/cask)" remote set-url origin https://mirrors.cloud.tencent.com/homebrew/homebrew-cask.git
  7. brew update
复制代码
  1. # zsh
  2. echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.cloud.tencent.com/homebrew-bottles/bottles' >> ~/.zprofile
  3. source ~/.zprofile
  4. # bash
  5. echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.cloud.tencent.com/homebrew-bottles/bottles' >> ~/.bash_profile
  6. source ~/.bash_profile
复制代码
5.4、北京外国语

  1. # brew
  2. git -C "$(brew --repo)" remote set-url origin https://mirrors.bfsu.edu.cn/git/homebrew/brew.git
  3. # homebrew-core
  4. git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.bfsu.edu.cn/git/homebrew/homebrew-core.git
  5. # homebrew-cask
  6. git -C "$(brew --repo homebrew/cask)" remote set-url origin https://mirrors.bfsu.edu.cn/git/homebrew/homebrew-cask.git
  7. brew update
复制代码
  1. # zsh
  2. echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.bfsu.edu.cn/homebrew-bottles/bottles' >> ~/.zprofile
  3. source ~/.zprofile
  4. # bash
  5. echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.bfsu.edu.cn/homebrew-bottles/bottles' >> ~/.bash_profile
  6. source ~/.bash_profile
复制代码
5.5、阿里

  1. # brew
  2. git -C "$(brew --repo)" remote set-url origin https://mirrors.aliyun.com/homebrew/brew.git
  3. # homebrew-core
  4. git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.aliyun.com/homebrew/homebrew-core.git
  5. # homebrew-cask
  6. # 缺少
  7. brew update
复制代码
  1. # zsh
  2. echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.aliyun.com/homebrew/homebrew-bottles' >> ~/.zprofile
  3. source ~/.zprofile
  4. # bash
  5. echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.aliyun.com/homebrew/homebrew-bottles' >> ~/.bash_profile
  6. source ~/.bash_profile
复制代码
六、恢复默认源

如果我们想要恢复Homebrew的默认镜像源也黑白常的简朴,其中brew、homebrew-core、homebrew-cask可通过实行如下脚本命令举行恢复
  1. # brew
  2. git -C "$(brew --repo)" remote set-url origin https://github.com/Homebrew/brew.git
  3. # homebrew-core
  4. git -C "$(brew --repo homebrew/core)" remote set-url origin https://github.com/Homebrew/homebrew-core.git
  5. # homebrew-cask
  6. git -C "$(brew --repo homebrew/cask)" remote set-url origin https://github.com/Homebrew/homebrew-cask.git
  7. brew update
复制代码
其中homebrew-bottles设置只能手动删除,将~/.zprofile文件中的HOMEBREW_BOTTLE_DOMAIN=https://mirrors.xxx.com内容删除,并实行source ~/.zprofile即可
七、Homebrew常用命令



  • 检察版本
  1. brew -v
复制代码


  • 更新Homebrew
  1. brew update
复制代码


  • 检察安装列表
  1. brew list
复制代码


  • 搜索、安装、更新、卸载软件,以git为例
  1. # 搜索软件
  2. brew search git
  3. # 安装软件
  4. brew install git
  5. # 更新软件
  6. brew upgrade git
  7. # 卸载软件
  8. brew uninstall git
复制代码


  • 检察必要更新的软件
  1. brew outdated
复制代码


  • 清理旧版本
  1. # 查看可清理的旧版本包,不执行实际操作
  2. brew cleanup -n
  3. # 清理所有包的旧版本
  4. brew cleanup
  5. # 清理指定包的旧版本
  6. brew cleanup git
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

北冰洋以北

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表