How to install Python under Rosetta 2
Running frontend written in TypeScript is easy and can be done under M1. With Python backends it’s a bit more complicated: There are pip dependencies that just won’t work under M1.
The solution is just to run your entire pipeline under Rosetta. (Ideal solution is to get everything on latest versions which support M1, but this is just not realistic with most projects.)
Copy the Terminal app
Copy and paste the Terminal (or in my case iTerm app — reason why I use this is support for split windows) and rename one to Rosetta. Press cmd-I and make sure “Open using Rosetta” is checked.
Add current architecture to your $PROMPT
You can always print the current architecture using uname -m command. Since I’m switching around a lot, I wanna know which one is running:
I edited theme in my oh-my-zsh installation by editing file ~/.oh-my-zsh/themes/robbyrussell.zsh-theme
(theme I'm using).
This is the updated version:
declare -A archs
archs=(
["arm64"]="$bg_bold[yellow]%}%{$fg_bold[black]%}m1%{$reset_color%}"
["x86_64"]="$bg_bold[red]%}%{$fg_bold[black]%}x86%{$reset_color%}"
)
arch=$(uname -m)
label=$archs[$arch]
PROMPT="%{$label %(?:%{$fg_bold[green]%}➜ :%{$fg_bold[red]%}➜ )"
PROMPT+=' %{$fg[cyan]%}%c%{$reset_color%} $(git_prompt_info)'
ZSH_THEME_GIT_PROMPT_PREFIX="%{$fg_bold[blue]%}(%{$fg[red]%}"
ZSH_THEME_GIT_PROMPT_SUFFIX="%{$reset_color%} "
ZSH_THEME_GIT_PROMPT_DIRTY="%{$fg[blue]%}) %{$fg[yellow]%}✗"
ZSH_THEME_GIT_PROMPT_CLEAN="%{$fg[blue]%})"
Using red box to indicate I’m on older system, and yellow to show I’m up to date.
Install separate copy of Homebrew
I’m assuming you already installed Homebrew under M1. Make sure you have by running this command:
m1 ➜ ~ which brew
/opt/homebrew/bin/brew
We’re going to add another installation and put it in /usr/local
.
Open your Rosetta terminal and install Homebrew. (Just follow the instructions on the website.) It will automatically create a binary in /usr/local/bin/brew
.
After you installed it, your brew
command still points to your M1 installation. So let's open .zshrc
and add an alias:
alias brow='/usr/local/bin/brew'
I saw the idea of using brow
on some other blog (can't remember where) and I thought that was pretty neat. Basically, you'll run brew
for M1 commands and you'll run brow
for Rosetta commands.
Install dependencies using `brow`
Now install whatever dependencies you’ll need for Python and its packages. For me this was:
brow install zlib
brow install openssl
brow install pcre
Install Python under Rosetta using pipenv
In your project directory, create a Pipfile:
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true
[requires]
python_version = "3.7"
I use version 3.7 here, because such version doesn’t exist on my system yet, so I know it will be installed using pyenv
.
Whatever you do, make sure Pipenv doesn’t reuse existing version of Python that was installed under M1. Python has to be installed under Rosetta.
Activate Python and install requirements
Run pipenv shell
and then start installing:
pip install -r requirements.txt
Another alternative is that your Pipfile already contains your requirements, in which case they would be installed in the previous step.
If you encounter any problems with missing libraries (such as zlib), relink them using brow link zlib
and make sure to run those export
commands that brew spits at the end, for example:
For compilers to find zlib you may need to set:
export LDFLAGS="-L/usr/local/opt/zlib/lib"
export CPPFLAGS="-I/usr/local/opt/zlib/include"
For pkg-config to find zlib you may need to set:
export PKG_CONFIG_PATH="/usr/local/opt/zlib/lib/pkgconfig"
(For zlib try exporting this too: CFLAGS='-I/usr/local/opt/zlib/include -L/usr/local/opt/zlib/lib'
)
This should work!
Now you have two Homebrew installations and two version of each Homebrew package — one compiled under M1, and one compiled under Rosetta.
There might be some issues when switching between the environments, but I’m assuming you’ll be running your backend under Rosetta, so it shouldn’t be a problem.