How to use Homebrew on Apple M1 Mac?

Homebrew is one of the most useful tools for managing software packages on the Mac. Homebrew is a software package management tool under the Mac OS platform, with many useful functions such as installation, uninstallation, update, viewing, and searching. A simple instruction can realize package management without you having to care about various dependencies and file paths, which is very convenient and fast.

But so far, it has not yet completed the adaptation to the new Mac models equipped with Apple silicon. According to the instructions posted by the maintainer on GitHub, Homebrew is actively adapting to the new architecture, but it still faces some major obstacles, such as the lack of a continuous integration framework based on the ARM architecture, and the framework or compiler that many software packages rely on ( go, gcc, qt) are not adapted, etc.

Homebrew is still available on the new Mac, and has released an experimental version that natively supports the ARM architecture.

1. Install Homebrew for ARM

According to the official plan, the ARM version of Homebrew must be installed in the  /opt/homebrew  path instead of the previous /usr/local/Homebrew . Since the official installation script has not been updated, you can manually install it with the following command:

cd /opt # Switch to the /opt directory 
mkdir homebrew # Create the homebrew directory 
curl -L https://github.com/Homebrew/brew/tarball/master | tar xz --strip 1 -C homebrew

(Note: If an error is reported during installation and use, it may be because the current user does not have permission to the  /opt/homebrew  path. For this, you can take over the directory through sudo chown -R $(whoami) /opt/homebrew  .)

Although the ARM version of Homebrew has been installed in the above steps, running the brew command in the terminal at this time cannot directly start the version. This is because by default, the path /opt/homebrew/bin used by Homebrew for the ARM version to install the program is not in the environment variable PATH, so the terminal cannot retrieve the brew program under this path.

To do this, edit the configuration file ~/.zshrc and add the following content:

path=('/opt/homebrew/bin' $path)
export PATH

(Note: It is recommended that users use the default terminal zsh of macOS Big Sur. If you use bash or fish, modify ~/.bashrc or ~/.config/fish/config.fish, the same later.)

Then restart the terminal. In this way, you can start the ARM version of Homebrew by directly executing brew.

2. Install Homebrew for X86

As mentioned above, because many software packages are not currently compatible with the ARM architecture (you can check on Homebrew's Apple silicon issue page) and cannot be installed through the ARM version of Homebrew, we also need to install an X86 version of Homebrew as a backup.

The X86 version of Homebrew cannot be installed in the ARM environment. For this, you need to start an X86 environment terminal. The more widespread method on the Internet is to create a copy of Terminal.app and then make it run in Rosetta compatibility mode.

In fact, if you notice the addition of arch -x86_64 before any command , you can run the command in X86 mode. Therefore, run:

arch -x86_64 $SHELL

You can start an X86 mode terminal, so that all the commands you run after will run in X86 mode.

At this point, run Homebrew's official installation script

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

You can complete the installation of the X86 version of Homebrew.

3. Coexistence of Homebrew for ARM and X86

After the above steps, there are currently two brew programs in the system, namely the X86 version of  /usr/local/bin/brew  and the ARM version of /opt/homebrew/bin/brew . So, when the brew command is executed in the terminal, which one will the system take?

When there is a program with the same name, the terminal will select the version to be executed according to the sequence of the program with the same name in the environment variable PATH. Since ~/.zshrc was configured before, the path of the ARM version of Homebrew was placed at the top of the PATH, so when brew is executed, the ARM version located at  /opt/homebrew/bin/brew  will be run first. If you want to run the X86 version, you need to manually enter the full path  arch -x86_64 /usr/local/bin/brew .

If you find it too troublesome to input such a long command, you can set aliases for the two versions in ~/.zshrc:

abrew='/opt/homebrew/bin/brew' # ARM Homebrew
ibrew='arch -x86_64 /usr/local/bin/brew' # X86 Homebrew

In this way, execute  abrrew install  to call the ARM version of Homebrew to install the software package, and execute  ibrew install   to call the X86 version, which is not easy to be confused.

As for which version of Homebrew should be used to install the package, different considerations are needed:

  • For the command line (CLI) program: You can first try to install with the ARM version of Homebrew to ensure that you get the version compiled for the new architecture to achieve the best running effect. But note:
    	<ul>
    		<li>有的软件包已经兼容新架构、但还没有发布相应的编译版,需要安装的过程中在本地编译,耗时会相对很长;</li>
    		<li>如果软件包还没有兼容新架构,使用 ARM 版 Homebrew 安装会报错,此时可以换用 X86 版 Homebrew 安装。</li>
    	</ul>
    </li>
    <li>对于图形界面(GUI)程序,即通过 Homebrew Cask 安装的 .app 程序:对于这类软件,Homebrew 起的作用只是从官方渠道下载这些软件的安装包,然后安装到 /Applications 路径(及执行安装脚本,如果有)。因此无论其是否针对新架构优化,通过任一版本 Homebrew 都可以安装。考虑到日后维护方便,建议直接用 ARM 版 Homebrew 安装即可。</li>
    

4. Migrate packages from the old Mac (or Homebrew for X86)

If you choose to migrate data from the old Mac or restore Time Machine backups after getting the M1 version of the Mac, then the system may already have the legacy X86 version of Homebrew and the software packages installed with it. In addition, you may also want to migrate the software packages you used to the new Mac without omission. In these cases, you can use the Homebrew Bundle function to assist the migration.

To export a list of packages installed using Homebrew for X86, run:

/usr/local/bin/brew bundle dump

You can get a backup file named Brewfile in the current directory. The file can be opened with a normal text editor, listing all installed software packages, added third-party software sources (tap), GUI programs managed by Homebrew Cask, and Mac App Store programs managed by mas-cli:

tap "homebrew/bundle"
tap "homebrew/cask"
[…]
brew "dash"
brew "ffmpeg"
[…]
cask "bartender"
cask "bettertouchtool"
[…]
mas "Apple Configurator 2", id: 1037126344
mas "Aviary", id: 1522043420
[…]

Make a note of the path to Brewfile. Then, use Homebrew for ARM to import its contents and install:

/opt/homebrew/bin/brew bundle --file /path/to/Brewfile

The migration is completed.

It should be noted that if you are migrating between two versions of Homebrew on the same machine, you do not need to migrate the GUI programs installed through Homebrew Cask and App Store (Homebrew will not allow overwriting installation). At this time, you can manually edit the above Brewfile, delete the records beginning with cask and mas, and then import them through the brew bundle.

If you want the ARM version of Homebrew to take over the installed Homebrew Cask software, just move the  folders under /usr/local/Caskroom  to  /opt/homebrew/Caskroom  :

mv /usr/local/Caskroom/* /opt/homebrew/Caskroom

5. Homebrew can be installed by Rosetta 2 conversion

The method is very simple, just add arch -x86_64 before the installation command in the past.

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

This converted Homebrew may have some compatibility issues on some software, so it is recommended to install another manually compiled version:

$ sudo mkdir -p /opt/homebrew

$ sudo chown -R $(whoami) :staff/opt/homebrew

$ cd / opt

$ curl -L https:/ /github.com/Homebrew/brew/tarball/master | tar xz --strip 1 -C homebrew

Homebrew installed in the first method is in the /usr/local/bin  directory, while in the second method installed in the  /opt/homebrew  directory, the latter will not be automatically added to the PATH environment variable, so it needs to be manually set:

exportPATH= "/opt/homebrew/bin: $PATH"

In this way, you have two Homebrews in your Mac: one is Rosetta converted and the other is native. If the native directory is in the front, the brew command will run this version by default.

What if I want to run Rosetta converted? We can set an ibrew command separately:

aliasibrew= 'arch -x86_64 /usr/local/bin/brew'

Currently, Homebrew does not provide a pre-compiled package for ARM Mac, so it is best to use the  brew install -s  command to install.

Guess you like

Origin blog.csdn.net/a055350/article/details/111040458