Installing PyTorch Geometric on Mac M1 with Accelerated GPU Support

JG Brasier
3 min readAug 27, 2022

--

PyTorch announced in May 2022 with their 1.12 release that developers and researchers can take advantage of Apple silicon GPUs for significantly faster model training. However, until recently, this was confined in a preview (nightly) version which made compatibility with other PyTorch dependent libraries unreliable.

Building a stable research environment for graph machine learning in Python that harnesses the power of the new arm architecture took a lot of time to figure out. I took great inspiration from this github issue and this article.

This is a straightforward guide to install a stable version of PyTorch Geometric on the new M1/Apple Silicon Chips. This works as of Sept 2022.

Step 1: Update to MacOS 12.3+

Check that you have at minimum MacOS 12.3 (Monterey) as GPU acceleration is only supported for versions 12.3 and up. If not go ahead and update to the latest version. I have MacOS 12.5.1 as of writing.

Step 2: Install Miniforge3

Miniforge3 is a community driven minimal installer for conda. It has an emphasis on supporting various CPU architectures (x86_64, ppc64le, and aarch64 including Apple M1).

This is a really important! Anaconda does not natively support arm64 (the M1 architecture) yet. Furthermore it is bulky and does some under-the-hood package installing with the wrong architecture that can be a pain to debug. I highly recommend removing anaconda entirely, to do so check out this thread.

If you do not have homebrew already installed:

$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)

Insalling miniforge3 through homebrew is the way to go:

$ brew install miniforge

Before using the conda command you need to source your .bashrc or .zshrc depending on what terminal type you are using. I use zsh, so:

$ source ~/.zshrc

Initialize conda as the base environment:

$ conda init zsh

Restart the terminal before proceeding

Step 3: Create and activate a new conda environment

Now that we have a light conda installer, let us create and activate a new environment named myenv .

$ conda create --name myenv python=3.8$ conda activate myenv

I have found python 3.8 to be really stable and compatible with al packages. Miniforge now supports python 3.9.

Step 4: Install arm64 compilers

$ conda install -y clang_osx-arm64 clangxx_osx-arm64 gfortran_osx-arm64

These are necessary to build arm64 versions of torch and torch-geometric

Step 5: Install torch

$ MACOSX_DEPLOYMENT_TARGET=12.5 CC=clang CXX=clang++ python -m pip --no-cache-dir install torch torchvision torchaudio

Here you can replace MACOSX_DEPLOYMENT_TARGET=12.X with the correct OS version you have

Step 6: Check which torch version you have

$ python -c "import torch; print(torch.__version__)">>> 1.12.1 (must be 1.12.0+)

It does not matter which version of torch you have as long as it is 1.12.0and up.

Step 7: Install torch-geometric

$ MACOSX_DEPLOYMENT_TARGET=12.5 CC=clang CXX=clang++ python -m pip --no-cache-dir  install  torch-scatter -f https://data.pyg.org/whl/torch-1.12.1+${cpu}.html$ MACOSX_DEPLOYMENT_TARGET=12.5 CC=clang CXX=clang++ python -m pip --no-cache-dir  install  torch-sparse -f https://data.pyg.org/whl/torch-1.12.1+${cpu}.html$ MACOSX_DEPLOYMENT_TARGET=12.5 CC=clang CXX=clang++ python -m pip --no-cache-dir  install  torch-cluster -f https://data.pyg.org/whl/torch-1.12.1+${cpu}.html$ MACOSX_DEPLOYMENT_TARGET=12.5 CC=clang CXX=clang++ python -m pip --no-cache-dir  install  torch-geometric

Make sure to specify the right version of MACOSX_DEPLOYMENT_TARGET and the right torch version in link to the wheels build https://data.pyg.org/whl/torch-1.12.1+${cpu}.html.

Step 8: Sanity check

Now that we have installed the correct packages, check that torch and torch-geometric can detect MPS and run on it.

Run this script to test torch
The 2 print statements at the start should both return True

Run this script to test torch-geometric

--

--