MacOS 程序包管理器 Homebrew 的使用

MacOS 程序包管理器 Homebrew 的使用

Homebrew 是 macOS 上一个流行的包管理器,用于安装和管理软件包。

Homebrew 的安装、更新、卸载和镜像源设置

Homebrew 的安装、更新、卸载

Homebrew 主要有四个部分组成: brew、homebrew-core 、homebrew-bottles、homebrew-cask。

  • brew:Homebrew 源代码仓库
  • homebrew-core:Homebrew 核心软件仓库
  • homebrew-bottles:Homebrew 预编译二进制软件包
  • homebrew-cask :MacOS 客户端应用

安装 Homebrew

安装 Homebrew 可以让更方便地在 macOS 上安装和管理各种软件包。请按照以下步骤进行安装:

  1. 打开终端应用程序。可以在“应用程序”文件夹中找到它,或者使用 Spotlight 搜索。

  2. 在终端中运行以下命令,以安装 Homebrew:

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

    这个命令将自动下载并安装 Homebrew。

  3. 安装完成后,可以测试 Homebrew 是否成功安装,方法是在终端中运行以下命令:

    brew doctor

    如果一切顺利,应该会看到一条消息,提示 Homebrew 已成功安装。

    也可以通过 brew --config 命令查看安装情况:

    HOMEBREW_VERSION: 3.6.17
    ORIGIN: https://mirrors.ustc.edu.cn/brew.git
    HEAD: e76c55e18434c870cca1de7aadf9d5218271fe2d
    Last commit: 6 months ago
    Core tap ORIGIN: https://github.com/Homebrew/homebrew-core
    Core tap HEAD: e9a966dbab826427e6128601a8e158104a677c5c
    Core tap last commit: 6 months ago
    Core tap branch: master
    HOMEBREW_PREFIX: /usr/local
    HOMEBREW_BOTTLE_DOMAIN: https://mirrors.aliyun.com/homebrew/homebrew-bottles
    HOMEBREW_CASK_OPTS: []
    HOMEBREW_MAKE_JOBS: 8
    Homebrew Ruby: 2.6.10 => /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/bin/ruby
    CPU: octa-core 64-bit kabylake
    Clang: 14.0.3 build 1403
    Git: 2.39.0 => /usr/local/bin/git
    Curl: 7.88.1 => /usr/bin/curl
    macOS: 13.4.1-x86_64
    CLT: 14.3.1.0.1.1683849156
    Xcode: N/A
    

现在,可以使用 Homebrew 来安装各种软件包。例如,要安装 Git,只需在终端中运行以下命令:

brew install git

Homebrew 将自动下载、编译和安装 Git,让很快就可以使用它。

更新 Homebrew

要更新 Homebrew,可以在终端中运行以下命令:

brew update

这个命令会更新 Homebrew 的软件包列表,以便可以下载最新版本的软件包。如果使用 Homebrew 安装了一些软件包,那么可能还需要更新这些软件包,以便它们可以与最新版本的 Homebrew 兼容。

要更新已安装的软件包,可以运行以下命令:

brew upgrade

这个命令会自动更新所有已安装的软件包到最新版本。如果只想更新特定软件包,可以将软件包名称作为参数传递给 brew upgrade 命令。

如果使用 Homebrew Cask 安装了一些应用程序,还可以使用以下命令更新它们:

brew upgrade --cask

这个命令会自动更新所有已安装的应用程序到最新版本。如果只想更新特定应用程序,可以将应用程序名称作为参数传递给 brew upgrade --cask 命令。

总的来说,使用 brew updatebrew upgrade 命令可以让保持 Homebrew 和已安装软件包的最新状态。

卸载 Homebrew

如果想卸载 Homebrew,可以在终端中运行以下命令:

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

这个命令会删除 Homebrew 及其相关文件和目录。请注意,这将删除使用 Homebrew 安装的所有软件包和应用程序,因此在运行此命令之前,应该备份数据和配置文件。

如果只是想卸载单个软件包,可以使用以下命令:

brew uninstall <package_name>

<package_name> 替换为要卸载的软件包的名称。

如果使用 Homebrew Cask 安装了一些应用程序,可以使用以下命令卸载它们:

brew uninstall --cask <app_name>

<app_name> 替换为要卸载的应用程序的名称。

总之,使用 brew uninstallbrew uninstall --cask 命令可以让卸载 Homebrew、软件包和应用程序,以便更好地管理系统。

Homebrew 的镜像源设置

Homebrew 更换国内镜像源

如果在使用 Homebrew 时发现下载速度很慢,可以尝试更换 Homebrew 的镜像源,以加速下载速度。

查看本地的镜像源地址

  1. 查看 brew.git 当前源

    cd "$(brew --repo)" && git remote -v
    

    输出默认镜像源(非国内):

    origin	https://github.com/Homebrew/brew.git (fetch)
    origin	https://github.com/Homebrew/brew.git (push)
    
  2. 查看 homebrew-core.git 当前源

    cd "$(brew --repo homebrew/core)" && git remote -v
    

    输出默认镜像源(非国内):

    origin	https://github.com/Homebrew/homebrew-core.git (fetch)
    origin	https://github.com/Homebrew/homebrew-core.git (push)
    

更换 Homebrew 镜像源

以下是更换 Homebrew 镜像源的步骤:

  1. 打开终端应用程序。

  2. 运行以下命令,备份 Homebrew 的默认镜像源:

    cd "$(brew --repo)"
    git remote set-url origin https://github.com/Homebrew/brew.git
    
  3. 运行以下命令,更换 Homebrew 的镜像源为清华大学镜像源(也可以使用其他镜像源):

    git remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git
    
  4. 运行以下命令,更新 Homebrew:

    brew update
    

更换 Homebrew Core 镜像源

  1. 运行以下命令,备份 Homebrew Core 的默认镜像源:

    cd "$(brew --repo)"/Library/Taps/homebrew/homebrew-core
    git remote set-url origin https://github.com/Homebrew/homebrew-core.git
    
  2. 运行以下命令,更换 Homebrew Core 的镜像源为清华大学镜像源(也可以使用其他镜像源):

    git remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git
    
  3. 运行以下命令,更新 Homebrew Core:

    brew update
    

更换 Homebrew Cask 镜像源

如果使用 Homebrew Cask 安装应用程序,还可以为 Homebrew Cask 更换镜像源。以下是更换 Homebrew Cask 镜像源的步骤:

  1. 运行以下命令,备份 Homebrew Cask 的默认镜像源:

    cd "$(brew --repo)"/Library/Taps/homebrew/homebrew-cask
    git remote set-url origin https://github.com/Homebrew/homebrew-cask.git
    
  2. 运行以下命令,更换 Homebrew Cask 的镜像源为清华大学镜像源(也可以使用其他镜像源):

    git remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask.git
    
  3. 运行以下命令,更新 Homebrew Cask:

    brew update
    

更换 Homebrew Bottles 镜像源

  1. 更换 zsh 的 homebrew-bottles 镜像源(macOS 在 10.15 系统开始,默认的 shell 都换成了 zsh):

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

    使修改立即生效:

    source ~/.zshrc
    

    更新镜像源:

    brew update
    
  2. 更换 bash 的 homebrew-bottles 镜像源:

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

    使修改立即生效:

    source ~/.bash_profile
    

    更新镜像源:

    brew update
    

Homebrew 命令的使用

基础命令

以下是一些常用的 Homebrew 命令:

  1. 安装 Homebrew

    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
    
  2. 安装软件包

    brew install [软件包名称]
    
  3. 搜索可用的软件包

    brew search [关键词]
    
  4. 列出已安装的软件包

    brew list
    
  5. 查看已安装软件包的信息

    brew info [软件包名称]
    
  6. 更新 Homebrew 及已安装的软件包

    brew update
    brew upgrade
    
  7. 卸载软件包

    brew uninstall [软件包名称]
    
  8. 清理过期的下载文件和缓存

    brew cleanup
    
  9. 显示 Homebrew 的版本信息

    brew --version
    
  10. 显示帮助信息

    brew help
    

这些是一些常用的 Homebrew 命令,可以根据需要进一步探索 Homebrew 的功能和其他命令。

brew services 命令

brew services 是 Homebrew 的一个子命令,用于管理 macOS 上的后台服务。它允许你在系统启动时自动启动、停止或重新启动已安装的服务。

以下是一些常用的 brew services 命令:

  1. 启动服务(开机自启)

    brew services start [服务名称]
    
  2. 停止服务

    brew services stop [服务名称]
    
  3. 重新启动服务

    brew services restart [服务名称]
    
  4. 查看当前运行的服务

    brew services list
    
  5. 查看服务的状态和一些信息

    brew services info [服务名称]
    

通过这些命令,可以方便地管理 macOS 上的后台服务,包括启动、停止、重新启动以及设置开机自启等操作。

posted @ 2024-04-11 14:43  飞仔FeiZai  阅读(616)  评论(0编辑  收藏  举报