苹果电脑macOS安装homebrew教程



前言

       Homebrew 是一个流行的包管理器,专为 macOS(以及 Linux)操作系统设计。它允许用户通过命令行界面轻松安装、更新和管理软件包。Homebrew 的主要优势在于其易用性、灵活性和社区支持。无需复杂的配置过程,比如使用homebrew安装adb和jdk、python,你不再需要去配置环境变量,Homebrew 会自动处理这些环境变量的设置。 确保 adb 命令可以在你的终端(Terminal)或命令行界面中直接使用;现在我们一起来看看homebrew的安装配置过程吧。

 

一、准备工作

在开始安装之前,请确保你的MacOS系统已经更新到最新版本,并且已经连接到互联网。此外,由于Homebrew依赖于Xcode的命令行工具,因此你需要先安装这些工具。

xcode-select --install

如下图,在终端执行以上命令后,如果系统提示你already installed 已经安装了Xcode命令行工具,那么你可以跳过这一步。

 

二、安装方式

现在,你可以开始下载和安装Homebrew了。你可以直接访问homebrew的官网,去下载:https://brew.sh/zh-cn/,当然我们也提供了更多的下载方式:

1. 官方安装脚本,你可以在终端中,直接输入以下命令,这个命令会从Homebrew的官方仓库下载安装脚本,并自动执行安装过程。安装脚本会引导你完成安装过程,只需按照屏幕上的提示进行即可。

当然,

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

这是 Homebrew 的官方一键安装命令。当你运行这个命令时,它会执行以下操作:

    1. 使用 curl 命令从 GitHub 上下载 Homebrew 的安装脚本。
    2. 使用 /bin/bash 执行下载的安装脚本。
    3. 安装脚本会检查你的系统环境,并确定最佳的安装位置(通常是 /usr/local)。
    4. 脚本会创建必要的目录结构,并将 Homebrew 的核心文件复制到这些目录中。
    5. 脚本会自动将 /usr/local/bin(或其他安装位置中的 bin 目录)添加到你的 PATH 环境变量中(如果尚未添加)无需再额外配置环境变量。

2. 国内镜像安装(推荐:建议国内用户使用国内镜像安装和配置镜像源,可以显著提升下载速度,官方的下载速度会比较慢一点。)

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

3. Git 克隆安装

sudo git clone https://github.com/Homebrew/brew /opt/homebrew

 

三、配置国内镜像源

1. 中科大镜像

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

 

2. 清华大学镜像

# 替换 brew.git
git -C "$(brew --repo)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git

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

 


四、后续使用

1、我们安装完成以后,可以使用命令验证安装情况, brew --version 命令可以查看 Homebrew 版本,


2、我们还可以在终端中输入 brew doctor 命令来测试 Homebrew 是否能够正确执行:
如果一切正常,brew doctor会显示 “Your system is ready to brew.”。如果有问题,它会提供一些解决建议,比如下图,由于作者用的是清华大学镜像源,这里就会提醒使用的标准源,这也是正常的。

 

3、查找资源的命令 brew search <keyword> ,homebrew的资源非常强大,比如我们开发小伙伴用到的python和java相关工具都可以使用homebrew来一键安装。

 除了命令行工具等开发者工具之外,下载QQ这样的图形软件,也是可以的喔,

 

4、如果你想要的资源,如上图搜索到了,那么就可以通过 brew install <formula> 来一键安装了。

 另外,如要卸载软件包,命令: brew uninstall <formula> ,

如果要更新某一软件包,命令: brew upgrade <formula> 

这个命令可以列出,你已经安装的软件:命令: brew list --versions 

 

5、更新 Homebrew

Homebrew的版本迭代更新是很快的,我们市场需要更新,所以更新命令比较常用,需要知道,

brew update

 

6、如果想要恢复官方源,命令如下:

# 重置 brew.git
git -C "$(brew --repo)" remote set-url origin https://github.com/Homebrew/brew.git

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

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

 



结语

 

亲爱的朋友:
      希望本文中描述的问题以及解决方案,可以帮助到您。当然,我们深知,问题和挑战总是层出不穷,新的情况也在不断涌现。如果读者朋友您有更好的方案,或者在实际应用中发现了文中的不足之处,请不吝分享您的宝贵建议。诚挚地邀请每一位读者加入我们的行列,共同完善这份教程。
    感谢您的阅读与支持!

Dear frends,

     We hope that the questions and solutions presented in this article can be of assistance to you. Of course, we are fully aware that problems and challenges are always emerging in an endless stream, and new situations are constantly arising. If you, our readers, have better solutions or have discovered any deficiencies in this article through practical application, please do not hesitate to share your valuable suggestions with us. We sincerely invite every reader to join us in continuously improving this tutorial.

Thank you for your reading and support!
See you,Parting is for better meeting!

 

 
posted @ 2025-02-12 14:46  xiaodi888  阅读(7986)  评论(0)    收藏  举报