How to Install Pytorch with CUDA support on Windows 10

PyTorch benefits significantly from using CUDA (NVIDIA's GPU acceleration framework), here are the steps to install PyTorch with CUDA support on Windows.

Prerequisites

Make sure you have an NVIDIA GPU supported by CUDA and have the following requirements.

1. CUDA for GPU support
    • For CUDA 11.8 version, make sure you have Nvidia Driver version 452.39 or higher
    • For CUDA 12.1 version, make sure you have Nvidia Driver version 527.41 or higher

2. Windows 10 or higher (recommended), Windows Server 2008 r2 and greater

3. Python 3.8~3.11

4 Steps to Install Pytorch with CUDA Version

Step 1. Check your NVIDIA driver

Open the NVIDIA Control Panel. Click System Information and check the driver version. It should be greater then 537.58, as this is the current driver version at the time of writing.

If you have an older version, goto https://www.nvidia.com/en-us/geforce/drivers/ and update your driver. There is an automatic and manual driver update possible if you know the videocard type.

check nvidia driver version

Step 2. Open a Command Prompt

Open a Windows terminal or the command prompt (cmd) and type python. The Windows app store will open automatically where you can install it from!

install python on windows
command prompt

Step 3. Install Pytorch with CUDA Version

Goto the pytorch website https://pytorch.org/get-started/locally/ and select the necessary buttons using Pip.

install pytorch with cuda version
install Pytorch 2.1.1 with CUDA 12.1
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121
install Pytorch 2.1.1 with CUDA 11.8
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118

Note: You can also install previous versions of Pytorch. The following is the command reference.

install Pytorch 2.1.0 with CUDA 12.1
pip install torch==2.1.0 torchvision==0.16.0 torchaudio==2.1.0 --index-url https://download.pytorch.org/whl/cu121
install Pytorch 2.1.0 with CUDA 11.8
pip install torch==2.1.0 torchvision==0.16.0 torchaudio==2.1.0 --index-url https://download.pytorch.org/whl/cu118
install Pytorch 2.0.1 with CUDA 11.8
pip install torch==2.0.1 torchvision==0.15.2 torchaudio==2.0.2 --index-url https://download.pytorch.org/whl/cu118
install Pytorch 2.0.1 with CUDA 11.7
pip install torch==2.0.1 torchvision==0.15.2 torchaudio==2.0.2
install Pytorch 2.0.0 with CUDA 11.8
pip install torch==2.0.0 torchvision==0.15.1 torchaudio==2.0.1 --index-url https://download.pytorch.org/whl/cu118
install Pytorch 2.0.0 with CUDA 11.7
pip install torch==2.0.0 torchvision==0.15.1 torchaudio==2.0.1
install Pytorch 1.13.1 with CUDA 11.7
pip install torch==1.13.1+cu117 torchvision==0.14.1+cu117 torchaudio==0.13.1 --extra-index-url https://download.pytorch.org/whl/cu117
install Pytorch 1.13.1 with CUDA 11.6
pip install torch==1.13.1+cu116 torchvision==0.14.1+cu116 torchaudio==0.13.1 --extra-index-url https://download.pytorch.org/whl/cu116
install Pytorch 1.13.0 with CUDA 11.7
pip install torch==1.13.0+cu117 torchvision==0.14.0+cu117 torchaudio==0.13.0 --extra-index-url https://download.pytorch.org/whl/cu117
install Pytorch 1.13.0 with CUDA 11.6
pip install torch==1.13.0+cu116 torchvision==0.14.0+cu116 torchaudio==0.13.0 --extra-index-url https://download.pytorch.org/whl/cu116
install Pytorch 1.12.1 with CUDA 11.6
pip install torch==1.12.1+cu116 torchvision==0.13.1+cu116 torchaudio==0.12.1 --extra-index-url https://download.pytorch.org/whl/cu116
install Pytorch 1.12.1 with CUDA 11.3
pip install torch==1.12.1+cu113 torchvision==0.13.1+cu113 torchaudio==0.12.1 --extra-index-url https://download.pytorch.org/whl/cu113
install Pytorch 1.12.1 with CUDA 10.2
pip install torch==1.12.1+cu102 torchvision==0.13.1+cu102 torchaudio==0.12.1 --extra-index-url https://download.pytorch.org/whl/cu102
install Pytorch 1.12.0 with CUDA 11.6
pip install torch==1.12.0+cu116 torchvision==0.13.0+cu116 torchaudio==0.12.0 --extra-index-url https://download.pytorch.org/whl/cu116
install Pytorch 1.12.0 with CUDA 11.3
pip install torch==1.12.0+cu113 torchvision==0.13.0+cu113 torchaudio==0.12.0 --extra-index-url https://download.pytorch.org/whl/cu113
install Pytorch 1.12.0 with CUDA 10.2
pip install torch==1.12.0+cu102 torchvision==0.13.0+cu102 torchaudio==0.12.0 --extra-index-url https://download.pytorch.org/whl/cu102
install Pytorch 1.11.0 with CUDA 11.3
pip install torch==1.11.0+cu113 torchvision==0.12.0+cu113 torchaudio==0.11.0 --extra-index-url https://download.pytorch.org/whl/cu113
install Pytorch 1.11.0 with CUDA 10.2
pip install torch==1.11.0+cu102 torchvision==0.12.0+cu102 torchaudio==0.11.0 --extra-index-url https://download.pytorch.org/whl/cu102

For more information, please check the Pytorch official page: https://pytorch.org/get-started/previous-versions/.

Step 4. Verify Installation

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.

From the command line, type python, then then enter the following code:

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

The output should be something similar to:

>>> print(torch.rand(2,3))
tensor([[0.7688, 0.5814, 0.9436],
        [0.0245, 0.6007, 0.2279]])
>>>

Additionally, to check if your GPU driver and CUDA is enabled and accessible by PyTorch, run the following commands to return whether or not the CUDA driver is enabled:

C:\Users\Administrator>python
Python 3.11.6 (tags/v3.11.6:8b6ee5b, Oct  2 2023, 14:57:12) [MSC v.1935 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> torch.cuda.is_available()
True
>>>
>>> print(torch.cuda.device_count())
1