Installing PyTorch GPU on Mac M1/M2 Using Conda

Posted by


In this tutorial, we will walk you through the process of installing PyTorch with GPU support on your Mac M1 or M2 using the Conda package manager. PyTorch is an open-source machine learning library that is widely used for building deep learning models. By installing PyTorch with GPU support, you can take advantage of the computational power of your Mac’s GPU for faster model training and inference.

Before we begin, please make sure that you have installed the latest version of Conda on your Mac. If you haven’t already installed Conda, you can do so by following the official installation instructions on the Conda website.

Step 1: Create a new Conda environment
The first step is to create a new Conda environment for PyTorch installation. Open a terminal window and run the following command to create a new Conda environment named ‘pytorch-gpu’:

conda create -n pytorch-gpu python=3.8

Activate the new Conda environment by running the following command:

conda activate pytorch-gpu

Step 2: Install PyTorch with GPU support
Next, we will install PyTorch with GPU support in the newly created Conda environment. To do this, run the following command:

conda install pytorch torchvision torchaudio cudatoolkit=11.1 -c pytorch

This command will install PyTorch, torchvision, and torchaudio with GPU support using CUDA toolkit version 11.1. This version is compatible with the latest Mac M1 and M2 GPUs.

Step 3: Test PyTorch installation
After the installation is complete, you can test if PyTorch is working correctly with GPU support. Open a Python shell in the Conda environment by running the following command:

python

In the Python shell, import PyTorch and check if it is using the GPU by running the following code:

import torch
print(torch.cuda.is_available())

If the output of the above code is True, then PyTorch is successfully using the GPU for computation.

Step 4: Install additional packages (optional)
If you plan to use PyTorch for specific tasks such as natural language processing or computer vision, you may need to install additional packages. You can install these packages using Conda or pip, depending on your requirements.

For example, to install the Transformers library for NLP tasks, you can run the following command:

conda install -c huggingface transformers

Step 5: Save the Conda environment
Once you have installed all the necessary packages, you can save the Conda environment to a YAML file for future use. Run the following command to save the environment:

conda env export > environment.yaml

To activate the environment later, you can use the following command:

conda env create -f environment.yaml

Congratulations! You have successfully installed PyTorch with GPU support on your Mac M1 or M2 using the Conda package manager. You can now start building and training deep learning models with PyTorch on your Mac’s GPU. If you encounter any issues during the installation process, please refer to the official PyTorch documentation or seek help from the PyTorch community.

0 0 votes
Article Rating
35 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@Daniel.M.Trapanese
2 months ago

Hi Thanks a lot. This command worked for me: conda install pytorch::pytorch torchvision torchaudio -c pytorch

@quillerj2412
2 months ago

Thank you very much! Finally a video straight to the point and clearly explains everything. So helpful. Really Appreciate it.

@TamannaIUrmi
2 months ago

Very helpful. The most helpful resource I found online on this topic.

@snb4799
2 months ago

Jeff thank you for all ı got it

@akalamian
2 months ago

thx professior, ran the first ai program on my mac!

@allenlam5210
2 months ago

If you get the error "Verifying transaction: WARNING conda.core.path_actions:verify(1055): Unable to create environments file. Path not writable." when you're installing pytorch, running this command,
sudo chown -R $USER ~/.conda
Seems to fix the issue

@ronraisch2073
2 months ago

as usual, life saver for all of us mac users

@afsarabenazir8558
2 months ago

im still unclear, does this mean i can use libraries written for nvidia gpu on my m1 mac?

@mohamedbennasar9369
2 months ago

Thank you so much, that was really helpful

@andreasschau4215
2 months ago

Thanks.
With this it was easy peasy!

@user-xr8tg8wp9w
2 months ago

where is the yml link?

@user-jo3kt2hv9f
2 months ago

Wow Clear instruction. Thanks a lot Jeff

@buuloctran2215
2 months ago

Hello, how to make mac m1 use more gpu when training, my mac only use 15-20% gpu even i used coreml

@unnatisinghal
2 months ago

Where can I access the yml file now. It is not available at the given link.

@user-lg9or9qx6z
2 months ago

You just wasted my time I can't find the yml file and everyone seems to be having the same issue.

@user-pf8hr6vz9q
2 months ago

Repository shared in the description isn't available

@yashasveejha4311
2 months ago

At 1:49 : the clean up removed python but when I hit python3 , its still there

@quicklearn_AI
2 months ago

Can you add egpu RTX 30 or 40 series and it showing GPU available ?

@beibit-ds
2 months ago

Learning directly from Jeff Heaton himself, awesome!

@rajeevgupta4058
2 months ago

For torch.cuda.device_count() it says 0 and torch.cuda.is_available() it says false, the model that I want to use need GPU to train and it checks these commands, I tried running it on tensorflow env too ( the one that you taught how to create). what should I do?