Using conda on a M1 Mac

Run multiple conda distributions to get the best of both worlds.

Nils Flaschel
Towards Data Science

--

Photo by pine watt on Unsplash

If you recently bought or got a new M1 Mac from work and you are using Python to develop or work on data science projects you probably already wasted some hours trying to get some packages to run. I still struggle a lot with Python, Docker, and conda on my Mac, but I found a way to get many packages to run inside conda environments.

Since the M1 is an ARM-based system, many Python packages will not install properly because they are built to run on AMD64 (x86). Like many other, I use conda to set up environments for my projects — preferably with Anaconda or Miniconda.

When I wanted to install Tensorflow for the first time on my Mac, I stumbled across Miniforge (https://github.com/conda-forge/miniforge) which is comparable to Miniconda, but with conda-forge as the default channel and a focus on supporting various CPU architectures. Tensorflow works like expected when installed with Miniforge.

But as soon as I need to install certain packages that I use for work — like SimpleITK (which is now also available as M1 Python wheel!) — Miniforge does not manage to install it. It is a bit of a gamble. At some point, I realized that I can install and use both, Miniforge and Miniconda on the same system.

EDIT: As Lucas-Raphael Müller pointed out to me, you do not need to install both, Miniconda and Miniforge. You can choose whether to use packages compiled for Intel chips or Apple Silicon like stated here: https://github.com/Haydnspass/miniforge#rosetta-on-mac-with-apple-silicon-hardware.

After installing Miniforge the initialization command will be set in your .bashrc/.zshrc:

# >>> conda initialize >>>
# !! Contents within this block are managed by ‘conda init’ !!
__conda_setup=”$(‘/Users/xyz/miniforge3/bin/conda’ ‘shell.zsh’ ‘hook’ 2> /dev/null)”
if [ $? -eq 0 ]; then
eval “$__conda_setup”
else
if [ -f “/Users/xyz/miniforge3/etc/profile.d/conda.sh” ]; then
. “/Users/xyz/miniforge3/etc/profile.d/conda.sh”
else
export PATH=”/Users/xyz/miniforge3/bin:$PATH”
fi
fi
unset __conda_setup
# <<< conda initialize <<<

This will initialize conda with Miniforge. You just need to copy your .bashrc/.zshrc file and change miniforge3 to miniconda3 and chose which one you want to use by default. Changing is as simple as running source .bashrc with the desired conda initialization.

For example, I was working on a project in which I needed SimpleITK for preprocessing images and Tensorflow to train a model. I was not able to get both working in the same Miniforge environment on the M1. So I split preprocessing and training into two environments, one utilizing Miniconda to run SimpleITK and one Miniforge environment to run Tensorflow.

The good part about it is that you can see both, the Miniconda and Miniforge environments at the same time by running conda env list . The only difference is that you will not see the names of the environments built with the other installer, only the path. With Miniconda initialized you will need to run conda activate with the full path to the Miniforge environment.

This is still easy to manage in a bash script to run scripts using multiple environments built with multiple conda distributions.

I hope this is just a temporary workaround until more and more packages will work on the M1, but I am sure this will take some time.

Hope this helps some people struggling with Python packages using conda on an M1.

--

--