How to enable GPU support with TensorFlow (macOS)

If you are using one of the laptops on loan of the CCI, or have a Macbook of your own with an M1/M2/M3 chip, here is what you can do to make full use of this chip with Tensorflow.



If you are using one of the CCI laptops, it should already have Anaconda and Python installed. If you are using your personal computer, please follow the steps to install Anaconda and Python from previous posts on the wiki.


  1. Go to your terminal and create a new virtual environment with

conda create -n ENV_NAME python=PYTHON_VERSION -y

For this line of code, instead of ENV_NAME you can add any other name. Just make sure there is no other environment with the same name by typing conda env list in your terminal. That should show you a list with the names of all the other environments that computer has. For the Python version, you can choose any version later than 3.8. For more info about Python versions, see the Python language wiki page.

  1. Go into your environment by typing

conda ENV_NAME activate

  1. Check the version of Tensorflow you need before installing it.

    • For Tensorflow 2.13 or LATER: python -m pip install tensorflow
    • For Tensorflow 2.12 or EARLIER python -m pip install tensorflow-macos
  2. Now we need to install the tensorflow-metal plug-in, running the line:

python -m pip install tensorflow-metal

For more info about the metal plig-in for mac, please read: Apple-Metal

  1. After the installation is complete, you can copy and run the next python lines.
import tensorflow as tf

print(f"Tensor Flow Version: {tf.__version__}")
print(f"Python {sys.version}")

gpu = len(tf.config.list_physical_devices('GPU'))>0
print("GPU is", "available" if gpu else "NOT AVAILABLE")

The output should be the tensorflow and python version you installed, and if everything is correct, the final message will be that the GPU is available

Revision #2
Created 22 March 2024 15:51:05 by Mayra Cristina Berrones Reyes
Updated 29 April 2024 22:22:38 by Mayra Cristina Berrones Reyes