Installing PyTorch with CUDA via PowerShell

  • Check your NVIDIA drivers and CUDA toolkit

  • Uninstall the CPU version of PyTorch (assuming that your PyTorch is currently based on CPU version)

  • Install PyTorch with CUDA 11.6 using PowerShell

Step 1: Check NVIDIA Drivers & CUDA Toolkit

  1. Open PowerShell:

    • Press Win + X and select Windows PowerShell.
  2. Run NVIDIA SMI Command:

     nvidia-smi
    

    Example Output:

     PS C:\Windows\System32> nvidia-smi
     Sun Mar  9 12:29:36 2025
     +-----------------------------------------------------------------------------+
     | NVIDIA-SMI 511.65       Driver Version: 511.65       CUDA Version: 11.6     |
     |-------------------------------+----------------------+----------------------+
     | GPU  Name            TCC/WDDM | Bus-Id        Disp.A | Volatile Uncorr. ECC |
     | Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
     |                               |                      |               MIG M. |
     |===============================+======================+======================|
     |   0  NVIDIA GeForce ... WDDM  | 00000000:01:00.0 Off |                  N/A |
     | N/A   57C    P0    10W /  N/A |      0MiB /  4096MiB |      0%      Default |
     |                               |                      |                  N/A |
     +-------------------------------+----------------------+----------------------+
    
     +-----------------------------------------------------------------------------+
     | Processes:                                                                  |
     |  GPU   GI   CI        PID   Type   Process name                  GPU Memory |
     |        ID   ID                                                   Usage      |
     |=============================================================================|
     |  No running processes found                                                 |
     +-----------------------------------------------------------------------------+
    

Step 2: Uninstall the CPU Version

pip uninstall torch -y

Step 3: Install PyTorch with CUDA 11.6

pip install torch --index-url https://download.pytorch.org/whl/cu116

Step 4: Test

If you plan to run via virtual environment, activate the environment first.

And then run the code one after another:

python -c "import torch; print(torch.__version__)"
python -c "import torch; print(torch.cuda.is_available())"
python -c "import torch; print(torch.version.cuda)"
python -c "import torch; print(torch.cuda.get_device_name(0))"

Example:

(myenv) PS C:\ZML> python -c "import torch; print(torch.__version__)"
1.13.1+cu116
(myenv) PS C:\ZML> python -c "import torch; print(torch.cuda.is_available())"
True
(myenv) PS C:\ZML>
(myenv) PS C:\ZML> python -c "import torch; print(torch.version.cuda)"
11.6
(myenv) PS C:\ZML> python -c "import torch; print(torch.cuda.get_device_name(0))"
NVIDIA GeForce RTX 3050 Ti Laptop GPU

You can also run the code in Visual Code (but you need to install ipykernel first)

NOTE:

When installing or upgrading PyTorch with CUDA, there are a few libraries you might want to consider uninstalling and reinstalling to avoid compatibility issues. Here are some libraries to check:

1. NumPy

  • Ensure you have a compatible version of NumPy.

  • You can uninstall and reinstall it: bashCopy

      pip uninstall numpy -y
      pip install numpy
    

2. Pandas

  • Similar to NumPy, check for compatibility and reinstall if necessary:

      pip uninstall pandas -y
      pip install pandas
    

3. Torchvision

  • If you use Torchvision, make sure it matches your PyTorch version:

      pip uninstall torchvision -y
      pip install torchvision --extra-index-url https://download.pytorch.org/whl/cu116
    

4. Torchaudio

  • If you use Torchaudio, ensure it is compatible:

      pip uninstall torchaudio -y
      pip install torchaudio --extra-index-url https://download.pytorch.org/whl/cu116
    
  • Libraries like torchtext, torchmetrics, or any other libraries that rely on PyTorch should also be checked for compatibility.

For recent versions of PyTorch (1.10 and later), NumPy versions 1.19.2 or higher are usually compatible.

If your NumPy version is different, uninstall it first:

pip uninstall numpy -y

Reinstall

pip install numpy==1.24.4

For recent versions of PyTorch (1.10 and later), Pandas versions 1.1.0 or higher are typically compatible.

If your Pandas version is different, uninstall it first:

pip uninstall pandas -y

:pip uninstall numpy -y

Reinstall

pip install pandas==1.5.3

.