Back

Powerful Package Manager for Mac - Homebrew

I absolutely do not allow any Mac users to not know how to use Homebrew

Homebrew is a package manager for MacOS. If you have used Linux before, you may be familiar with package managers. However, you don’t need to understand what package management is to use Homebrew. The purpose of this blog is to help you understand and use Homebrew in an easy-to-understand way.

Homebrew Official Website

Introduction

Now, if I want to download the IINA video player on my Macbook, the download process should be like this (I hardly use the app store because most of the time the software I want is not available there, and the software version is often not up to date):

  1. Open a browser and search for the software name “IINA”
  2. Find the official website of the software among the many links, and be sure to distinguish non-official software sites - “https://iina.io/"
  3. Find the download link on the official website and download the dmg/pkg file - “https://dl-portal.iina.io/IINA.v1.1.2.dmg"
  4. Run the downloaded dmg/pkg file to install

Ignoring the repetitiveness of these steps, many people around me cannot even distinguish the official website of the software from the many ads and pirate websites provided by search engines. So, if you use Homebrew, the process of installing IINA is as follows:

  1. Open the command line terminal
  2. Enter brew install --cask iina

With one command line, many operations are left out. To some extent, Homebrew can be called a more powerful App Store on Mac.

In fact, Homebrew is much more powerful than this. Here are some examples of its use:

  1. Install Python (along with Jupyter Notebook) on the computer: brew install python, brew install jupyter
  2. Use the open-source software ntfs-3g to unlock NTFS read and write functions on the Macbook: brew install ntfs-3g
  3. Install an HP printer driver: brew install homebrew/cask-drivers/apple-hewlett-packard-printer-drivers
  4. Many common software are also available: brew install --cask qq, brew install --cask zoom, brew install --cask vscode

Installation

Homebrew is blocked by the GFW, but there are mirror sites in China, so the installation methods in China and abroad will be different.

With Proxy/Installing Overseas (Official Website Installation Method)

First, install the Command Line Tools (CLT) for Xcode on your computer.

xcode-select --install

You may be prompted to enter a password. When entering the password in the command line, there will be no response on the screen. Press Enter after entering the password.

After a successful installation, enter the following code:

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

This code downloads the installation script install.sh from Homebrew’s Github repo and runs it (the installation process may also prompt for a password input).

After installation, run

brew doctor

This command will check the status of Homebrew for users to see if Homebrew has been installed successfully.

Installing in China

In China, you can use the Tsinghua mirror site (Tsinghua University’s Homebrew domestic mirror site) for installation and use: Tsinghua University Homebrew Mirror Site

First, install the Command Line Tools (CLT) for Xcode on your computer.

xcode-select --install

You may be prompted to enter a password. When entering the password in the command line, there will be no response on the screen. Press Enter after entering the password.

To allow the installation script to use Tsinghua’s servers, you need to set a few server variables.

export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git"
export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git"
export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles"

After setting, clone the Homebrew repo from Tsinghua’s server and run the installation script inside it.

git clone --depth=1 https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/install.git brew-install
/bin/bash brew-install/install.sh

After running, you can delete the installation files.

rm -rf brew-install

Note: If your Mac has an M1 chip architecture, you need to run the following code (Not sure if you have an M1? Run uname -m in the command line. If it returns arm64, it’s M1; if it returns x86_64, it’s Intel)

test -r ~/.bash_profile && echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.bash_profile
test -r ~/.zprofile && echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zprofile

eval "$(/opt/homebrew/bin/brew shellenv)" means to run brew shellenv in a separate shell, which will run a script shellenv.sh to set environment variables in the system.
The first line: if bash is installed on the computer, run the eval command every time bash is opened
The second line: if zsh is installed on the computer, run the eval command every time zsh is opened

After running, restart the shell, or source the file that was just changed:

test -r ~/.bash_profile && source ~/.bash_profile
test -r ~/.zprofile && source ~/.zprofile

With this, the installation is complete, and you can use brew doctor to check if the installation was successful.

Switching to a Domestic Source

By default, Homebrew uses foreign servers as software sources, which results in slow access speed. Therefore, you can switch to a domestic mirror site.

Currently, domestic sources include Tencent Source, Tsinghua Source, USTC Source.

Below is an example explanation of the process of switching to the Tsinghua source. Click on the relevant link for the setting methods of each source.

Switching Index Mirrors

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
git -C "$(brew --repo homebrew/cask-fonts)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask-fonts.git
git -C "$(brew --repo homebrew/cask-drivers)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask-drivers.git
git -C "$(brew --repo homebrew/cask-versions)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask-versions.git
git -C "$(brew --repo homebrew/command-not-found)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-command-not-found.git

Apply the changes after switching

brew update-reset

Switching Binary Bottle Mirrors

Tencent Source, Tsinghua Source, USTC Source

test -r ~/.bash_profile && echo 'export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles"' >> ~/.bash_profile
test -r ~/.bash_profile && secho 'export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles"' >> ~/.zprofile
export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles"

The first line: add environment variables to bash to change the homebrew-bottles source
The second line: add environment variables to zsh to change the homebrew-bottles source
The third line: use the homebrew-bottles source in the current shell

Reverting to the Official Homebrew Source

Auxiliary Software

Cakebrew

launchrocket

Uninstallation

References & Resource Websites

Homebrew Official Website

In-depth Application of Homebrew

Homebrew Usage Guide for Programmers - Sspai

Homebrew Setup Guide

Tencent Software Source (Tencent Source)

Tsinghua University Open Source Mirror Site (Tsinghua Source)

USTC Open Source Mirror Site (USTC Source)

Licensed under CC BY-NC-SA 4.0
Last updated on Sep 20, 2024 12:24 UTC
Built with Hugo
Theme Stack designed by Jimmy