Tutorial

How to Install Homebrew on macOS

2024-12-13 #Homebrew#macOS

Sometimes we see others installing software on macOS using the following command, which is very convenient.

1
brew install package

This article will introduce how to install Homebrew on macOS 15 so that this command works on your Mac as well. At the same time, Git will also be installed.

What is Homebrew

Homebrew is a free and open-source software package management system designed to simplify the installation of software on macOS. It has many practical functions such as installing, uninstalling, updating, viewing, and searching. With a simple command, you can manage software packages, making it extremely convenient and fast.

Installing Homebrew

Open the terminal program. To open or quit Terminal on Mac:

  • Launchpad > Other > Terminal
  • Use Launchpad or press Command⌘ + Space to open Spotlight search, then search for Terminal or Terminal.app.

Installation from Official Source

If you cannot access GitHub, please refer to the section on using mirror sources for installation.

Copy the installation command:

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

Execute the installation command in the terminal. During installation, you will need to enter your computer password for authorization:

1
2
==> Checking for `sudo` access (which may request your password)...
Password:

Brew will display the software it will install and the folders it will create. Press Enter to continue and wait for the download and installation to complete.

After it finishes, running brew -v to test might still show the error zsh: command not found: brew.

You need to copy and execute the following three commands as prompted to add Homebrew to your PATH:

1
2
3
echo >> /Users/me/.zprofile
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> /Users/me/.zprofile
eval "$(/opt/homebrew/bin/brew shellenv)"

If oh-my-zsh is used, run the following commands:

1
2
echo 'export PATH="/opt/homebrew/bin:$PATH"' >> ~/.zshrc   
source ~/.zshrc

For bash users:

1
2
echo 'export PATH="/opt/homebrew/bin:$PATH"' >> ~/.bashrc   
source ~/.bashrc

Then you can run brew -v to test the installation, which will successfully display the Homebrew version number.

1
2
>brew -v
Homebrew 4.4.11

Now we can use the brew install command.

Installation from Mirror Sources (Mainland China)

Mainland China users are recommended to use mirror sources for installation.

You can directly use the following installation script:

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

Or check the content below:

Replacing with the Alibaba Mirror Source

1
2
3
4
5
echo 'export HOMEBREW_API_DOMAIN="https://mirrors.aliyun.com/homebrew-bottles/api"' >> ~/.zshrc
echo 'export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.aliyun.com/homebrew/brew.git"' >> ~/.zshrc
echo 'export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.aliyun.com/homebrew/homebrew-core.git"' >> ~/.zshrc
echo 'export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.aliyun.com/homebrew/homebrew-bottles"' >> ~/.zshrc
source ~/.zshrc

Then run the installation command to download the installation script from Alibaba Cloud and install Homebrew:

1
2
3
git clone https://mirrors.aliyun.com/homebrew/install.git brew-install
/bin/bash brew-install/install.sh
rm -rf brew-install

You can also fetch the official installation script from GitHub to install Homebrew:

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

Test:

1
brew -v

Using Homebrew

Installing Software

1
2
3
4
5
6
7
8
9
10
11
# Search for software
brew search mysql

# Install the default version
brew install mysql

# Install a specific version
brew mysql [email protected]

# Install desktop applications (GUI apps)
brew install --cask firefox

Software Management

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# List installed software
brew list

# View software information
brew info mysql

# View software installation path
brew list mysql

# Upgrade all software
brew upgrade

# Upgrade a specific software
brew upgrade wget

# Uninstall software
brew uninstall wget

Service Management

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# List all services
brew services list

# View specific service information
brew services info mysql

# Start service
brew services start mysql

# Restart service
brew services restart mysql

# Stop service
brew services stop mysql

# Remove service
brew services remove mysql

# Remove all unused services
brew services cleanup

Others

1
2
3
4
5
# Check Homebrew status
brew doctor

# Update Homebrew
brew update

Uninstalling Homebrew

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

Reference: Installing & Configuring Homebrew on macOS

Comments
Share

Comments