How to install Homebrew on Mac

1. Introduction

Homebrew is a package management tool that currently supports macOS and Linux systems. It mainly consists of four parts:brew、homebrew-core 、homebrew-cask、homebrew-bottles

name illustrate
brew Homebrew source code repository
homebrew-core Homebrew core source
homebrew-cask Provides installation of macOS apps and large binaries
homebrew-bottles Precompiled binary packages

2. Official script download, installation and configuration of environment variables

First we can enterofficial website address to get the download link, as follows:

Insert image description here

If you encounter an errorcommand not found: brew after the installation is completed, it is usually because the environment variable is invalid. Here you need to use the previously introduced echo $SHELL to confirm. Terminal type, as follows

Insert image description here

Previously, the default shell for each new user was/bin/bash, so here we need the configuration file to be ~/.bash_profile, from macOS Catalina (10.15.x) version Initially, Mac uses /bin/zsh as the default Shell, and its corresponding configuration file is ~/.zshrc

In addition, we also need to determine the model of our computer's CPU. You can check it here throughuname -m, as follows

Insert image description here

The M1 chip ARM version of Homebrew will eventually be installed in the /opt/homebrew path, while the previous Intel chip Macs will be installed in the /usr/local/Homebrew path.

So to sum up, when we configure Homebrew environment variables, we need to make corresponding configurations based on different situations, as follows:

M1 chip

echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zprofile
eval "$(/opt/homebrew/bin/brew shellenv)"
  • bash
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.bash_profile
eval "$(/opt/homebrew/bin/brew shellenv)"

Intel chip

  • zsh
echo 'eval "$(/usr/local/Homebrew/bin/brew shellenv)"' >> ~/.zprofile
eval "$(/usr/local/Homebrew/bin/brew shellenv)"
  • bash
echo 'eval "$(/usr/local/Homebrew/bin/brew shellenv)"' >> ~/.bash_profile
eval "$(/usr/local/Homebrew/bin/brew shellenv)"

3. GitHub acceleration

When using official scripts to download and install, you often encounter the following error message:

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

The reason why the official script cannot be used is thatraw.githubusercontent.com access is very unstable. Here we can use the method of writinghosts to solve the problem of GitHub to a certain extent. Resource inaccessibility problem.

3.1、Mac

Here we can open the/etc/hosts file and add relevant configurations to it. For specific operations, please refer to:

Finally we need to refresh the cache, as follows

sudo killall -HUP mDNSResponder

3.2、Windows

In addition, if it is a Windows environment, we will append the content in the above article to the C:/windows/system32/drivers/etc/hosts file and refresh the cache.

ipconfig /flushdns

PS: If you feel that it is too troublesome to regularly check the content in the above hosts file, it is recommended to use SwitchHosts to automatically update. The operation is very simple and supports cross-platform. Add a hosts rule and enable it, and then you can automatically synchronize with the latest hosts at set intervals.

4. Mirror script installation

If the installation cannot be successful through the official method, it is recommended to use the mirror script of Homebrew for installation. Here are two recommended ones. The first is the following script:

/bin/bash -c "$(curl -fsSL https://cdn.jsdelivr.net/gh/ineo6/homebrew-install/install.sh)"

This script uses the University of Science and Technology of China image to accelerate access and only modifies the warehouse address, which will not cause security risks.

About the Homebrew mirroring service provided by the University of Science and Technology of China

About the Homebrew mirroring service provided by Tsinghua University

If an error occursError: Checksum mismatch. , the error code is as follows

curl: (56) LibreSSL SSL_read: SSL_ERROR_SYSCALL, errno 54
Error: Checksum mismatch.
Expected: b065e5e3783954f3e65d8d3a6377ca51649bfcfa21b356b0dd70490f74c6bd86
Actual: e8a348fe5d5c2b966bab84052062f0317944122dea5fdfdc84ac6d0bd513c137
Archive: /Users/rocky/Library/Caches/Homebrew/portable-ruby-2.6.3_2.yosemite.bottle.tar.gz
To retry an incomplete download, remove the file above.
Error: Failed to install Homebrew Portable Ruby (and your system version is too old)!
Failed during: /usr/local/bin/brew update --force

The installation is interrupted due to the file in the Homebrew directory. You only need to go to the corresponding path above, delete the file, and restart Just execute the installation command. portable-ruby-2.6.3_2.yosemite.bottle.tar.gz

If the execution of the above command is stuck at the following information

==> Tapping homebrew/core
Cloning into ‘/opt/homebrew/Library/Taps/homebrew/homebrew-core’…

PleaseControl+Cinterrupt the script and execute the following command:

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

Installationcask There are also problems with installation failure or stuck, and the solution is the same:

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

After successful execution, continue to execute the installation command. When you finally see the command, the installation is successful.

==> Installation successful!

Finally, use the following command to update

brew update

If you encounter a problem that cannot be solved, uninstall Homebrew and then reinstall it. The official uninstall script is as follows:

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

Of course, using the official script will also encounter the problem of inaccessible uninstall address, so you can use the following script:

/bin/bash -c "$(curl -fsSL https://cdn.jsdelivr.net/gh/ineo6/homebrew-install/uninstall.sh)"

Deletion here can not only be uninstalled and deleted through scripts, but also deleted in the folder, as follows

# 目录替换为真实的brew位置
sudo rm -rf 目录

In addition, in the terminal of a Mac computer, you can also executeopen /opt/homebrew/ to open the folder interface for deletion.

PS: Homebrew installation path in each environment:

  • Intel installation directory:/usr/local/Homebrew
  • m1 arm installation directory:/opt/homebrew
  • linux installation directory:/home/linuxbrew

The second method is even simpler. We only need to copy the following script content to the terminal and execute it.

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

Then just make a selection according to the prompts that appear in the terminal. If you need to uninstall later, it is more convenient, as follows:

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

5. Change the mirror source

As mentioned above, Homebrew is usually used to download and install software, but it is very slow when downloading software. In order to improve the download speed, you need to change the Homebrew installation source and replace it with a domestic image.

5.1. University of Science and Technology of China

Here we use the Homebrew image hosted and maintained by the University of Science and Technology of China as an example. Among them, brew、homebrew-core is a required item, and homebrew-cask、homebrew-bottles is set as needed. Relevant configuration information can be viewed through the brew config command.

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

brew update

In addition, if you need to set upbottles mirror, you also need to configure environment variables. The judgment of the configuration file has been introduced above, so I won’t go into details here. Choose the following according to different situations. One of the configurations is enough.

# zsh
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles/bottles' >> ~/.zprofile
source ~/.zprofile

# bash
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles/bottles' >> ~/.bash_profile
source ~/.bash_profile

In addition to the mirror source of the University of Science and Technology of China, we can also use other mirror sources for configuration.

5.2. Tsinghua University

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

brew update
# zsh
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/bottles' >> ~/.zprofile
source ~/.zprofile

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

5.3, Tencent

# brew
git -C "$(brew --repo)" remote set-url origin https://mirrors.cloud.tencent.com/homebrew/brew.git
# homebrew-core
git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.cloud.tencent.com/homebrew/homebrew-core.git
# homebrew-cask
git -C "$(brew --repo homebrew/cask)" remote set-url origin https://mirrors.cloud.tencent.com/homebrew/homebrew-cask.git

brew update
# zsh
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.cloud.tencent.com/homebrew-bottles/bottles' >> ~/.zprofile
source ~/.zprofile

# bash
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.cloud.tencent.com/homebrew-bottles/bottles' >> ~/.bash_profile
source ~/.bash_profile

5.4. Beijing Foreign Languages

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

brew update
# zsh
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.bfsu.edu.cn/homebrew-bottles/bottles' >> ~/.zprofile
source ~/.zprofile

# bash
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.bfsu.edu.cn/homebrew-bottles/bottles' >> ~/.bash_profile
source ~/.bash_profile

5.5, Ali

# brew
git -C "$(brew --repo)" remote set-url origin https://mirrors.aliyun.com/homebrew/brew.git
# homebrew-core
git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.aliyun.com/homebrew/homebrew-core.git
# homebrew-cask
# 缺少

brew update
# zsh
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.aliyun.com/homebrew/homebrew-bottles' >> ~/.zprofile
source ~/.zprofile

# bash
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.aliyun.com/homebrew/homebrew-bottles' >> ~/.bash_profile
source ~/.bash_profile

6. Restore default source

If we want to restore the default mirror source of Homebrew, it is very simple. brew、homebrew-core、homebrew-cask can be restored by executing the following script command

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

brew update

Among themhomebrew-bottles configuration can only be deleted manually. Delete the content in the ~/.zprofile file and execute a>That’s itHOMEBREW_BOTTLE_DOMAIN=https://mirrors.xxx.comsource ~/.zprofile

7. Common Homebrew commands

  • View version
brew -v
  • Update Homebrew
brew update
  • View installation list
brew list
  • Search, install, update, and uninstall software, taking git as an example
# 搜索软件
brew search git
# 安装软件
brew install git
# 更新软件
brew upgrade git
# 卸载软件
brew uninstall git
  • View software that needs updating
brew outdated
  • Clean up old versions
# 查看可清理的旧版本包,不执行实际操作
brew cleanup -n
# 清理所有包的旧版本
brew cleanup
# 清理指定包的旧版本
brew cleanup git

Guess you like

Origin blog.csdn.net/weixin_45277161/article/details/134719230