zoukankan      html  css  js  c++  java
  • mac下镜像飞速安装Homebrew教程

    Homebrew是一款包管理工具,目前支持macOSlinux系统。主要有四个部分组成: brewhomebrew-core 、homebrew-caskhomebrew-bottles

    本文主要介绍Homebrew安装方式以及如何加速访问,顺便普及一些必要的知识。

    1. 脚本说明

    Homebrew默认安装脚本:

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

    如果你等待一段时间之后遇到下面提示,就说明无法访问官方脚本地址:

    curl: (7) Failed to connect to raw.githubusercontent.com port 443: Operation timed out

    请使用下面的脚本:

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

    上面脚本中使用了中科大镜像来加速访问。

    安装使用到的脚本源码在此 homebrew-install ,仅修改了仓库地址部分,不会产生安全隐患,另外求 star 。


    官方脚本无法使用的原因是访问很不稳定,我上面提供的方案里是采用了jsdelivr CDN加速访问。
    另外也可以采用写入hosts的方式,可以一定程度解决GitHub资源无法访问的问题,我也写了一篇操作文章,有需要可以阅读下。
    ineo6:解决GitHub网页githubusercontent地址无法访问问题​zhuanlan.zhihu.com图标

    2. 安装说明

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

    如果命令执行中卡在下面信息:

    ==> Tapping homebrew/core
    Cloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-core'...

    Control + C中断脚本执行如下命令:

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

    cask 同样也有安装失败或者卡住的问题,解决方法也是一样:

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

    成功执行之后继续执行前文的安装命令:

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

    最后看到==> Installation successful!就说明安装成功了。

    最最后执行:

    brew update

    3. 如何卸载Homebrew

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

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

    4. 设置镜像方法

    brewhomebrew/core是必备项目,homebrew/caskhomebrew/bottles按需设置。

    通过 brew config 命令查看配置信息。

    4.1 中科大源

    git -C "$(brew --repo)" remote set-url origin https://mirrors.ustc.edu.cn/brew.git
    
    git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git
    
    git -C "$(brew --repo homebrew/cask)" remote set-url origin https://mirrors.ustc.edu.cn/homebrew-cask.git
    
    brew update
    
    # 长期替换homebrew-bottles
    echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.bash_profile
    source ~/.bash_profile

    注意bottles可以临时设置,在终端执行下面命令:

    export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles

    4.2 清华大学源

    git -C "$(brew --repo)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git
    
    git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git
    
    git -C "$(brew --repo homebrew/cask)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask.git
    
    brew update
    
    # 长期替换homebrew-bottles
    echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles' >> ~/.bash_profile
    source ~/.bash_profile

    4.3 恢复默认源

    git -C "$(brew --repo)" remote set-url origin https://github.com/Homebrew/brew.git
    
    git -C "$(brew --repo homebrew/core)" remote set-url origin https://github.com/Homebrew/homebrew-core.git
    
    git -C "$(brew --repo homebrew/cask)" remote set-url origin https://github.com/Homebrew/homebrew-cask.git
    
    brew update

    homebrew-bottles配置只能手动删除,将 ~/.bash_profile 文件中的 HOMEBREW_BOTTLE_DOMAIN=https://mirrors.xxx.com内容删除,并执行 source ~/.bash_profile

    5. 其他

    5.1 cask

    目前cask是从GitHub上读取软件源,而GitHub Api对匿名访问有限制,如果使用比较频繁的话,可以申请Api Token,然后在环境变量中配置到HOMEBREW_GITHUB_API_TOKEN

    .bash_profile中追加:

    export HOMEBREW_GITHUB_API_TOKEN=yourtoken

    注意:因为cask是基于GitHub下载软件,所以目前是无法加速的。

    6. 总结

    在前面的过程中我们把brewhomebrew-core的地址都指向到中科大镜像。

    原理是通过修改install脚本,在里面预设镜像地址来做到的。

    #!/usr/bin/ruby
    # This script installs to /usr/local only. To install elsewhere (which is
    # unsupported) you can untar https://github.com/Homebrew/brew/tarball/master
    # anywhere you like.
    HOMEBREW_PREFIX = "/usr/local".freeze
    HOMEBREW_REPOSITORY = "/usr/local/Homebrew".freeze
    HOMEBREW_CACHE = "#{ENV["HOME"]}/Library/Caches/Homebrew".freeze
    # 这里替换了BREW_REPO
    BREW_REPO = "https://mirrors.ustc.edu.cn/brew.git".freeze

    最后不完美的地方是我们只能预设brew镜像,没找到比较好的办法预设homebrew-corehomebrew-caskgit地址。

    参考文章

    如果对您有帮助

    请给我一个赞⭐️,让更多人能发现文章,这也是对我的认可和鼓励。


    广告插播

    GitMaster可以树形展示git项目代码。

    • 代码树支持GitHubGitLabGitee
    • ️ 支持私有部署页面,一键标记
    • ️ 文件提交历史可视化 git history
    • GitHub通知提醒功能
    • ⬇️ 支持`GitHub`文件、目录下载

    本文首发博客: mac下镜像飞速安装Homebrew教程

     (二维码自动识别)

  • 相关阅读:
    odoo 成长之路
    程序人生之项目汇报(吐槽篇)
    docker-compose介绍及使用
    XML-RPC入门
    编辑器之神-vim
    Linux基本命令 三、系统管理
    基于socketserver模块实现并发的套接字(tcp、udp)
    第6章-3.使用函数统计指定数字的个数 (20分)
    第6章-2.使用函数求素数和 (20分)
    第6章-1.使用函数求特殊a串数列和 (10分)
  • 原文地址:https://www.cnblogs.com/john-xiong/p/13562322.html
Copyright © 2011-2022 走看看