{:.no_toc}
PyTorch can be installed and used on various Linux distributions. Depending on your system and compute requirements, your experience with PyTorch on Linux may vary in terms of processing time. It is recommended, but not required, that your Linux system has an NVIDIA or AMD GPU in order to harness the full power of PyTorch's CUDA support or ROCm support.
{: #linux-prerequisites}
PyTorch is supported on Linux distributions that use glibc >= v2.17, which include the following:
- Arch Linux, minimum version 2012-07-15
- CentOS, minimum version 7.3-1611
- Debian, minimum version 8.0
- Fedora, minimum version 24
- Mint, minimum version 14
- OpenSUSE, minimum version 42.1
- PCLinuxOS, minimum version 2014.7
- Slackware, minimum version 14.2
- Ubuntu, minimum version 13.04
The install instructions here will generally apply to all supported Linux distributions. An example difference is that your distribution may support
yum
instead ofapt
. The specific examples shown were run on an Ubuntu 18.04 machine.
{: #linux-python}
Python 3.9-3.12 is generally installed by default on any of our supported Linux distributions, which meets our recommendation.
Tip: By default, you will have to use the command
python3
to run Python. If you want to use just the commandpython
, instead ofpython3
, you can symlinkpython
to thepython3
binary.
However, if you want to install another version, there are multiple ways:
- APT
- Python website
If you decide to use APT, you can run the following command to install it:
sudo apt install python
If you use Anaconda to install PyTorch, it will install a sandboxed version of Python that will be used for running PyTorch applications.
{: #linux-package-manager}
To install the PyTorch binaries, you will need to use one of two supported package managers: Anaconda or pip. Anaconda is the recommended package manager as it will provide you all of the PyTorch dependencies in one, sandboxed install, including Python.
To install Anaconda, you will use the command-line installer. Right-click on the 64-bit installer link, select Copy Link Location
, and then use the following commands:
# The version of Anaconda may be different depending on when you are installing`
curl -O https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
sh Miniconda3-latest-Linux-x86_64.sh
# and follow the prompts. The defaults are generally good.`
You may have to open a new terminal or re-source your
~/.bashrc
to get access to theconda
command.
Python 3
While Python 3.x is installed by default on Linux, pip
is not installed by default.
sudo apt install python3-pip
Tip: If you want to use just the command
pip
, instead ofpip3
, you can symlinkpip
to thepip3
binary.
{: #linux-installation}
{: #linux-anaconda}
To install PyTorch via Anaconda, and do not have a CUDA-capable or ROCm-capable system or do not require CUDA/ROCm (i.e. GPU support), in the above selector, choose OS: Linux, Package: Conda, Language: Python and Compute Platform: CPU. Then, run the command that is presented to you.
To install PyTorch via Anaconda, and you do have a CUDA-capable system, in the above selector, choose OS: Linux, Package: Conda and the CUDA version suited to your machine. Often, the latest CUDA version is better. Then, run the command that is presented to you.
PyTorch via Anaconda is not supported on ROCm currently. Please use pip instead.
{: #linux-pip}
To install PyTorch via pip, and do not have a CUDA-capable or ROCm-capable system or do not require CUDA/ROCm (i.e. GPU support), in the above selector, choose OS: Linux, Package: Pip, Language: Python and Compute Platform: CPU. Then, run the command that is presented to you.
To install PyTorch via pip, and do have a CUDA-capable system, in the above selector, choose OS: Linux, Package: Pip, Language: Python and the CUDA version suited to your machine. Often, the latest CUDA version is better. Then, run the command that is presented to you.
To install PyTorch via pip, and do have a ROCm-capable system, in the above selector, choose OS: Linux, Package: Pip, Language: Python and the ROCm version supported. Then, run the command that is presented to you.
{: #linux-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]])
Additionally, to check if your GPU driver and CUDA/ROCm is enabled and accessible by PyTorch, run the following commands to return whether or not the GPU driver is enabled (the ROCm build of PyTorch uses the same semantics at the python API level link, so the below commands should also work for ROCm):
import torch
torch.cuda.is_available()
{: #linux-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.
{: #linux-prerequisites-2}
- Install Anaconda or Pip
- If you need to build PyTorch with GPU support a. for NVIDIA GPUs, install CUDA, if your machine has a CUDA-enabled GPU. b. for AMD GPUs, install ROCm, if your machine has a ROCm-enabled GPU
- Follow the steps described here: https://github.com/pytorch/pytorch#from-source
You can verify the installation as described above.