
M1 Macbooks aren’t that new anymore. Somehow, installing Python’s deep learning libraries still isn’t a straightforward process. At least with TensorFlow. PyTorch is different.
Today you’ll learn how to install and run PyTorch natively on your M1 machine. It doesn’t make a difference which M1 machine you have (Air, Pro, Mini, or iMac).
Let’s get started.
Step 1 – Install and configure Miniforge
I’ve spent so much time configuring the M1 Mac for Data Science. It never worked without a flaw. Until I found this option. It will take you between 5 and 10 minutes to set up completely, depending on the Internet speed.
To start, you’ll need to install Homebrew. It’s a package manager for Mac, and you can install it by executing the following line from the Terminal:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Keep in mind – if you’re setting up a new M1 Mac, it’s likely you won’t have XCode build tools installed, which are required for Homebrew. The Terminal will inform you if these are missing and will ask if you want to install them.
Once both XCode build tools and Homebrew are installed, you can restart the Terminal and install Miniforge:
brew install miniforge
It’s a couple of hundred MB download, so it will take some time to complete. Once done, initialize the conda for the Z shell (zsh):
conda init zsh
Finally, restart the Terminal. That’s it! Miniforge is now installed and you’re ready to create virtual environments. Let’s do so next.
Step 2 – Create a virtual environment
The following Terminal command will create a new virtual environment named pytorch_env
based on Python 3.8:
conda create --name pytorch_env python=3.8
Once created, activate it with the following command:
conda activate pytorch_env
You should see something like this:

Let’s install the libraries next.
Step 3 – Install PyTorch
You can find a native PyTorch package on Anaconda’s website. You should look for osx-arm64
under Platform – that tells you it’s M1 compatible:

Clicking on the package will provide us with installation instructions, as you can see below:

You can use the following command to install both PyTorch and Torchvision (for datasets):
conda install -c pytorch pytorch torchvision
And that’s it – PyTorch is now installed! Let’s test the damn thing next.
Step 4 – Testing
Let’s keep it simple and don’t write anything from scratch. PyTorch’s GitHub page comes with many examples, one of which is the script for training a handwritten digit classifier (link).
Simply download that script and run it from the Terminal:
python main.py
You’ll see the following output:

Everything works as expected! Let’s open up the Activity Monitor to verify Python is running natively:

If you see "Apple" under "Kind", it means the program is running natively on the M1 chip, and not under the Rosetta emulator.
Check!
Final Thoughts
And there you have it – PyTorch successfully installed on M1 machines. You can’t follow the same steps for TensorFlow, as Python will crash once you start training the model. Let me know if you want me to cover TensorFlow installation too.
You have to admit, installing PyTorch natively was easier than you expected. Sure, it won’t run as fast as on GPUs, but it is enough to get you started with learning and experimenting.
Thanks for reading.
Loved the article? Become a Medium member to continue learning without limits. I’ll receive a portion of your membership fee if you use the following link, with no extra cost to you.
Learn More
- Top 5 Books to Learn Data Science in 2021
- How to Schedule Python Scripts With Cron – The Only Guide You’ll Ever Need
- Dask Delayed – How to Parallelize Your Python Code With Ease
- How to Create PDF Reports With Python – The Essential Guide
- Become a Data Scientist in 2021 Even Without a College Degree
Stay Connected
- Follow me on Medium for more stories like this
- Sign up for my newsletter
- Connect on LinkedIn