Easy Python Setup On Apple Silicon For Machine Learning & Utilising ARM Architecture Across Virtual Environments šŸ

Adam McGauran
5 min readOct 31, 2023

--

Photo by Tianyi Ma on Unsplash

This article details how to safely install python on Apple silicon without effecting operating system internal dependenciesā€™ as well as setting up a virtual environment to ensure no cross contamination between your machineā€™s operating system internal dependenciesā€™ and your machine learning projects, all using homebrew & conda forge.

So you just got a Mac that has Appleā€™s new M-series chip and you need to set it up for all your data science and machine learning needs, but where to start? While itā€™s not as easy as it was when Mac used an Intel chip, itā€™s still achievable in a few steps that Iā€™ll outline below.

  1. Install Homebrew & Use Python3
  2. Install Miniforge
  3. Create Virtual Environments
  4. Install Python Libraries
  5. Profit??

Python is already on your Mac!

Mac comes with version 2.7 installed. Yes, that is an old version but if you try to remove it or delete it, your operating system internals will stop working. There are a few dependenciesā€™ that use Python 2.7 and you should leave them alone.

First, verify that Python is on your mac by opening up Terminal and type the below command

python --version

What you should see is something like Python 2.7.18. You can check where python is installed with the below command

where python

It should be located in usr/bin/python, leave that alone, this is what your operating system internal dependenciesā€™ use and instead, we will focus on getting Python3 installed on your system that you can use separately and safely.

Installing Homebrew

First things first, we need Homebrew.

Homebrew is an open source software package management system that is free to use. We are going to employ it to simplify the process of getting Python3 onto your machine. The name ā€œHomebrewā€ stems from that idea that each user will customise their Mac with software depending on the their own taste.

Open up terminal and paste in the below command, This will automatically install Homebrew inside your /usr/local directory

mkdir homebrew && curl -L https://github.com/Homebrew/brew/tarball/master | tar xz ā€” strip 1 -C homebrew

Complete!

Seriously, thatā€™s it. Homebrew has been used to successfully install Python 3 on your Mac and you ready to go. You can now open terminal and type ā€˜python3ā€™ and you will be entered into your python environment.

However, the only problem is every time you want to execute something using Python3, you need to type in ā€œpython3ā€.

Iā€™m not happy with that and you shouldnā€™t be either, this is no way to live life so letā€™s go ahead and get this rectified.

Anaconda to the rescue

Sort of. Ideally, we want to be able to manage different project environments and have separate environments for separate projects and manage all the different packages that will soon be installed in a safe manner. Anyone who has worked on enough projects will tell you how crucial this is ā€” separate work environments means less headaches to put it simply.

Anaconda is a package / environment management solution that is great, however, it is very large and if you donā€™t want the whole thing which installs tons of libraries for you then you will want to follow this tutorial and install Miniforge instead. This is compatible with x64 so if you install these they will work fine under terminal rosetta however they will likely be a little slower, but will still be pretty good. Again, not ideal.

Soā€¦.

Lets use ARM architecture natively

You didnā€™t buy a new mac to use a slower architecture so lets go ahead and set up ARM to run natively.

This is where Conda Forge comes into play. You can download that here. You want to find the OSx arm64 (Apple Silicon) version as can be seen below

Miniforge3 download page

This will get you the sh file. So open up a new terminal, type ā€œsh ā€ and drag your download from your downloads into your terminal as can be seen below. This is a quicker way to get the whole path to the file.

Now execute.

This will bring up the installation steps as well as some prompts you have to agree to, so take your time here.

Using your Environments

Start Conda automatically every time you open terminal?

At the end of the Miniforge installation you will be asked if you want the Conda environment to execute every time you start the terminal, this is personal preference. I have it set to do it but if you donā€™t want to do that you can easily activate your environment every time you want to use it with the below command.

conda activate base

ā€˜baseā€™ is the default environment your given from Conda. To exit this environment use the below command

conda deactivate base

And lastly to create a new environment

conda create name

where ā€˜nameā€™ is the identifier you want to give to the environment. Then to activate run the above command again but this time with the identifier you have given the environment.

Now let us check which version of python is available inside the base environment

python --version

This should show the latest version of python, which at this time is 3.10.12, which also allows us to use a python command instead of python3. Mission accomplished.

When inside your python environment you can now install all your favourite libraries by simply calling ā€œconda installā€¦ā€, you can install different versions of python either within each environment which allows a lot of flexibility between projects on Apple silicon as well as all the latest speed benefits using ARM natively. Happy coding!

--

--