Skip to content

Latest commit

 

History

History
97 lines (67 loc) · 3.25 KB

File metadata and controls

97 lines (67 loc) · 3.25 KB

Installing on macOS

{:.no_toc}

PyTorch can be installed and used on macOS. Depending on your system and GPU capabilities, your experience with PyTorch on a Mac may vary in terms of processing time.

Prerequisites

{: #mac-prerequisites}

macOS Version

PyTorch is supported on macOS 10.15 (Catalina) or above.

Python

{: #mac-python}

It is recommended that you use Python 3.9 - 3.12. You can install Python either through the Anaconda package manager (see below), Homebrew, or the Python website.

Package Manager

{: #mac-package-manager}

To install the PyTorch binaries, you will need to use one of two supported package managers: pip or Anaconda.

Anaconda

To install Anaconda, you can download graphical installer or use the command-line installer. If you use the command-line installer, you can right-click on the installer link, select Copy Link Address, or use the following commands on Mac computer with Apple silicon:

# The version of Anaconda may be different depending on when you are installing`
curl -O https://repo.anaconda.com/miniconda/Miniconda3-latest-MacOSX-arm64.sh
sh Miniconda3-latest-MacOSX-arm64.sh
# and follow the prompts. The defaults are generally good.`

pip

Python 3

If you installed Python via Homebrew or the Python website, pip was installed with it. If you installed Python 3.x, then you will be using the command pip3.

Tip: If you want to use just the command pip, instead of pip3, you can symlink pip to the pip3 binary.

Installation

{: #mac-installation}

Anaconda

{: #mac-anaconda}

To install PyTorch via Anaconda, use the following conda command:

conda install pytorch torchvision -c pytorch

pip

{: #mac-pip}

To install PyTorch via pip, use one of the following two commands, depending on your Python version:

# Python 3.x
pip3 install torch torchvision

Verification

{: #mac-verification}

To ensure that PyTorch was installed correctly, we can verify the installation by running sample PyTorch code. Here we will construct a randomly initialized tensor.

import torch
x = torch.rand(5, 3)
print(x)

The output should be something similar to:

tensor([[0.3380, 0.3845, 0.3217],
        [0.8337, 0.9050, 0.2650],
        [0.2979, 0.7141, 0.9069],
        [0.1449, 0.1132, 0.1375],
        [0.4675, 0.3947, 0.1426]])

Building from source

{: #mac-from-source}

For the majority of PyTorch users, installing from a pre-built binary via a package manager will provide the best experience. However, there are times when you may want to install the bleeding edge PyTorch code, whether for testing or actual development on the PyTorch core. To install the latest PyTorch code, you will need to build PyTorch from source.

Prerequisites

{: #mac-prerequisites-2}

  1. [Optional] Install Anaconda
  2. Follow the steps described here: https://github.com/pytorch/pytorch#from-source

You can verify the installation as described above.