• Mac安装Homebrew的正确姿势


    世上无难事,只要找到 Homebrew 的正确安装方式。

    Homebrew 是什么

    Homebrew是 mac的包管理器,仅需执行相应的命令,就能下载安装需要的软件包,可以省掉自己去下载、解压、拖拽(安装)等繁琐的步骤。 比如安装服务器 nginx,打开终端执行以下命令即可安装:

    brew install nginx

    Homebrew 官方文档 https://brew.sh/

    Homebrew 怎么安装

    方法一:brew官网的安装脚本

    执行命令:

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

    这时很可能会出现一个问题: 要么下载极其龟速,要么直接出现如下提示

    curl: (7) Failed to connect to raw.githubusercontent.com port 443: Connection refused
    

    原因:这是 http://raw.githubusercontent.com 访问不稳定引起的。

    此时需要换一种科学高效的安装方法,即方法二。

    方法二:brew 镜像安装脚本(亲测最快速最有效)

    /usr/bin/ruby -e "$(curl -fsSL https://cdn.jsdelivr.net/gh/ineo6/homebrew-install/install)"

    该脚本用了中科大镜像加速访问,仅修改仓库地址部分,不会产生安全隐患。 关于中科大所提供的 Homebrew 镜像服务 https://lug.ustc.edu.cn/wiki/mirrors/help/brew.git

    注:注意这里的速度,几百kib/s或几m/s才是正常的。若只有几kib/s,通常为无效的安装方法,一般加载了百分之几十后就会报错。

    Homebrew 安装时如何避坑

    1.若出现 Error: Checksum mismatch.

    报错代码如下:

    curl: (56) LibreSSL SSL_read: SSL_ERROR_SYSCALL, errno 54
    Error: Checksum mismatch.
    Expected: b065e5e3783954f3e65d8d3a6377ca51649bfcfa21b356b0dd70490f74c6bd86
    Actual: e8a348fe5d5c2b966bab84052062f0317944122dea5fdfdc84ac6d0bd513c137
    Archive: /Users/joyce/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文件引起的安装中断,只需要到上面对应的路径里,删掉这个文件,重新执行安装命令即可:

    /usr/bin/ruby -e "$(curl -fsSL https://cdn.jsdelivr.net/gh/ineo6/homebrew-install/install)"

    2.若卡在了Cloning into...


    由这里的龟速可断定卡住了,立马用Control + C中断脚本,然后执行以下命令:

    cd "$(brew --repo)/Library/Taps/"
    mkdir homebrew && cd homebrew
    git clone git://mirrors.ustc.edu.cn/homebrew-core.git

    执行后可看到:


    速度立马快得飞起,一下子就能装好。

    注:最后出现 Installation successful! 或者 Checking out files: 100% (5392/5392), done. 说明安装成功。

    Homebrew安装完为何需要配置

    前面已经提到,Homebrew通常用来下载软件的,但它在安装软件时非常慢。为了提升安装速度,需要更改 Homebrew 的安装源,将其替换成国内镜像。

    这里用的是由中科大负责托管维护的 Homebrew 镜像。其中,前两个为必须配置的项目,后两个可按需配置。

    1.必备设置

    • 替换 brew.git:
    git -C "$(brew --repo)" remote set-url origin https://mirrors.ustc.edu.cn/brew.git
    • 替换 homebrew-core.git:
    git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git

    2.按需设置

    • 替换 homebrew-cask.git:
    git -C "$(brew --repo homebrew/cask)" remote set-url origin https://mirrors.ustc.edu.cn/homebrew-cask.git
    • 替换homebrew-bottles:

    首先要先区分你的mac用哪种终端工具,如果是 bash,则执行:

    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 主要由四个部分组成: brew、homebrew-core 、homebrew-cask、homebrew-bottles,它们对应的功能如下:

    组成功能
    Homebrew 源代码仓库
    homebrew-core Homebrew 核心源
    homebrew-cask 提供macos应用和大型二进制文件的安装
    homebrew-bottles 预编译二进制软件包

    Homebrew 基本用法有哪些

    // 查询:
    brew search 软件名
    
    // 安装:
    brew install 软件名
    
    // 卸载:
    brew uninstall 软件名
    
    // 更新 Homebrew:
    brew update 
    
    // 查看 Homebrew 配置信息:
    brew config 

    // 查看 Homebrew 安装软件的具体路径:
    brew list 软件名
        例如: brew list ant
    
       /usr/local/Cellar/ant/1.10.9/bin/ant
       /usr/local/Cellar/ant/1.10.9/bin/antRun
       /usr/local/Cellar/ant/1.10.9/bin/antRun.pl
       /usr/local/Cellar/ant/1.10.9/bin/complete-ant-cmd.pl
       /usr/local/Cellar/ant/1.10.9/bin/runant.pl
       /usr/local/Cellar/ant/1.10.9/bin/runant.py
       /usr/local/Cellar/ant/1.10.9/libexec/bin/ (6 files)
       /usr/local/Cellar/ant/1.10.9/libexec/etc/ (16 files)
       /usr/local/Cellar/ant/1.10.9/libexec/lib/ (55 files)
       /usr/local/Cellar/ant/1.10.9/libexec/manual/ (1572 files)
       /usr/local/Cellar/ant/1.10.9/libexec/ (8 files)
    
     
    
        说明:
    
        1、通过brew install安装应用最先是放在/usr/local/Cellar/目录下。
    
        2、有些应用会自动创建软链接放在/usr/bin或者/usr/sbin,同时也会将整个文件夹放在/usr/local
    
        3、可以使用 brew list 软件名 (比如 brew list ant)确定安装位置

    注:使用官方脚本同样会遇到uninstall地址无法访问问题,可以替换为下面脚本:

    /usr/bin/ruby -e "$(curl -fsSL https://cdn.jsdelivr.net/gh/ineo6/homebrew-install/uninstall)"

    参考文章

    mac下镜像飞速安装Homebrew教程

    科大Homebrew源

    写在最后

    在此之前试过了好几种安装方法,比如下载一个 brew_install.rb的文件,然后执行命令ruby brew_install.rb,一开始行得通,但下载到中途就会报错,而且出错的方法不一,也尝试过好几种相应的解决方法,最终都夭折了,只有这个镜像安装才能成功,而且下载速度非常快。

  • 相关阅读:
    响应式开发
    css3的2D和3D的转换
    前端CSS3笔记
    DOM精简版笔记
    JS进阶
    linux 修改时间同步到BIOS
    linux-设置代理和取消代理
    linux-rpm强制安装跳过依赖包
    [linux] VNC the connection was refused by the computer
    Linux vim 常用命令(不定时update)
  • 原文地址:https://www.cnblogs.com/chenyablog/p/14343707.html
Copyright © 2020-2023  润新知