M1 Macs: Having two Terminals for x86 and ARM architecture

M1 Macs: Having two Terminals for x86 and ARM architecture

I've recently upgraded to an M1 Mac because Black. Magic. Fuckery.
Nonetheless, I required some command line applications which are not yet available for ARM architectures.

First, make sure you have Rosetta 2 installed: usr/sbin/softwareupdate --install-rosetta --agree-to-license.

Individual command line tools can be emulated by adding arch -x86_64 to the beginning of the command. However, I wanted a complete emulated shell where everything gets emulated and executed in x86.
For that, I required a Homebrew installation with the corresponding x86 apps. Consequently, I wanted to have two environments which are somewhat seperated – having their own binaries and libraries.

1) Create a copy of the terminal of your choice (I'm using iTerm). I renamed it to "iTerm-x86".
2) Right click the app and select "Get Info". Select "Open using Rosetta".

3) Start the new Terminal application. The Terminal is now running in the emulated x86 environment and requires a new Homebrew installation.
4) Run the script from brew.sh. It will install Homebrew for the corresponding architectures. Homebrew will use different directories for its binaries (/usr/localfor x86, /opt/homebrew for ARM).
5) Therefore, you want your shell to load the correct $PATH based on your Terminal app.
Add the following snippet to your .zshrc or .zshenv in your home directory.

export PATH="$PATH:/usr/bin:/bin:/usr/sbin:/sbin

if [[ "$(arch)" = "arm64" ]]; then
    echo "Detected ARM ..."
    export PATH="/opt/homebrew/bin:$PATH"
elif [[ "$(arch)" = "i386" ]]; then
    echo "Detected x86 ..."
    export PATH="/usr/local/Homebrew/bin:$PATH"
else
    echo "Unknown architecture."
fi

eval $(brew shellenv)
Add this to your .zshrc or .zshenv.

The  snippet checks the architecture (ARM or the emulated x86?), adds the path for the brew binary and runs eval $(brew shellenv) which then sets the correct Homebrew paths.