Tuesday 23 February 2021

Setting up homebrew on Apple Mac M1 with ARM architecture

Homebrew https://brew.sh/ lets you install and manage packages and applications from the command line on MacOS.

The new Apple Mac M1 is running Apple Silicon technology which is based on ARM processor (aarch64 architecture) rather than Intel processor (x86_64 architecture). This can cause some issues when wanting to run software compiled for x86_64 when a version is not available yet on aarch64.

To work around this, Apple provide Rosetta which provides an emulation layer to execute x86 opcodes under ARM. You can install Rosetta from terminal with

$ /usr/sbin/softwareupdate --install-rosetta --agree-to-license

To make it simpler to switch between ARM and x86 mode copy set up a new terminal profile which will run under Rosetta emulation.

Run terminal, press Apple + , to open Preferences. Click on your favourite profile, e.g. mine is called "a Homebrew".


Click the three dot icon at the bottom and on the popup menu click Duplicate Profile. 


Enter a name like "Rosetta Shell" and click on the tab Shell then tick Run command and enter 

env /usr/bin/arch -x86_64 /bin/bash --login


Now if you run terminal using your usual profile it will give you a terminal session running in ARM mode.
If you click menu Shell / New Tab then you should see a list of profiles. Click on the Rosetta Shell one you created.


You should have two tabs open. The first is your ARM mode shell. The second is your Rosetta x86 mode shell.

You can click between them to run commands now for either mode.

We'll install Home brew for both ARM and x86.
For ARM mode it will go in the default location /opt/homebrew.
For x86 mode it will go in /usr/local.

In the first tab, type 

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

It will install ARM brew to /opt/homebrew/bin/brew.

In the second tab, type the same command.
It will install x86 brew to /usr/local/bin/brew.

Finally I add some lines to my shell startup profile ~/.bash_profile to detect which mode I am running in and to set the correct path to brew. I also change the prompt so that I get an i when in Intel x86 mode.
Edit ~/.bash_profile and add

if [ "$(sysctl -n sysctl.proc_translated)" = "1" ]; then
    # run under rosetta 2 with
    #   env /usr/bin/arch -x86_64 /bin/bash --login
    #local brew_path="/usr/local/homebrew/bin"
    eval $(/usr/local/bin/brew shellenv)
    export PS1="i \D{%I:%M %p}:\w $ "
else
    #local brew_path="/opt/homebrew/bin"
    eval $(/opt/homebrew/bin/brew shellenv)
    export PS1=" \D{%I:%M %p}:\w $ "
fi

Now when I logout of terminal and back in I see either the ARM prompt

or the x86 prompt with an i to remind me of the mode

Note that I am using bash for my Apple shell. The system default is now zsh so you will need to adapt this if you use that.


Not all packages are available yet under ARM brew.
However, the full range is available under x86 brew running under the Rosetta x86 emulation.

E.g. $ brew install ruby



No comments: