This guide provides three different methods to install PyTorch with GPU acceleration using CUDA and cuDNN. Choose the method that best suits your requirements and system configuration.
- Using Virtual Environment (venv) in Python
- Using Conda Environment in Anaconda
- Using pip
- Using conda
- Using Docker Container
Check the official PyTorch website for the latest installation instructions.
This method sets up PyTorch with CUDA using a Python virtual environment.
-
Install the latest version of Python from the official website.
-
Create a virtual environment:
python -m venv mlenv
-
Activate the virtual environment:
- Windows:
mlenv\Scripts\activate
- Linux/macOS:
source mlenv/bin/activate
- Windows:
-
Install PyTorch with CUDA support:
First, check your GPU and CUDA version using
nvidia-smi. If you don't have CUDA installed, download CUDA Toolkit and cuDNN from the NVIDIA website.Then, install PyTorch with the appropriate CUDA version:
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu126
-
Verify the installation:
python -c "import torch; print(torch.cuda.is_available())"If the output is
True, PyTorch has been successfully installed with GPU support. -
Deactivate the virtual environment:
deactivate
-
Remove the virtual environment (optional):
- Windows:
rmdir /s /q mlenv
- Linux/macOS:
rm -rf mlenv
- Windows:
This method sets up PyTorch with CUDA using a Conda environment.
-
Add Conda to the system PATH:
- Windows:
- User Installation
Add the following directories to your user's environment variables:
C:\Users\YourUsername\anaconda3 C:\Users\YourUsername\anaconda3\Scripts C:\Users\YourUsername\anaconda3\Library\bin - System Installation
Add the following directories to your system's environment variables:
C:\ProgramData\Anaconda3 C:\ProgramData\Anaconda3\Scripts C:\ProgramData\Anaconda3\Library\bin
- User Installation
Add the following directories to your user's environment variables:
- Linux/macOS:
Add the following line to your shell configuration file (e.g.,
~/.bashrc,~/.bash_profile,~/.zshrc):Then, run:export PATH="/path/to/anaconda3/bin:$PATH"
source ~/.bashrc # or ~/.bash_profile, ~/.zshrc
- Windows:
-
Create a Conda environment:
conda create -n mlenv python=3.12 -y conda activate mlenv
-
Install PyTorch with CUDA support:
First, check your GPU and CUDA version using
nvidia-smi. Then, install PyTorch with the appropriate CUDA version:-
using
pip:If you don't have CUDA installed, download CUDA Toolkit and cuDNN from the NVIDIA website.
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu126
-
using
conda:This method ensures CUDA and cuDNN are installed via Conda
conda install cudatoolkit -c anaconda -y conda install pytorch-cuda=12.4 -c pytorch -c nvidia -y conda install pytorch torchvision torchaudio -c pytorch -c nvidia -y
-
-
Verify the installation:
python -c "import torch; print(torch.cuda.is_available())"If the output is
True, PyTorch has been successfully installed with GPU support. -
Deactivate the Conda environment:
conda deactivate mlenv
-
Remove the Conda environment (optional):
conda env remove -n mlenv
This method sets up PyTorch with CUDA using a Docker container.
-
Install Docker Desktop on your system.
-
Enable GPU support in Docker Desktop:
- Open Docker Desktop.
- Go to
Settings>Resources>Advanced. - Enable
Use the GPUandEnable experimental features.
-
Ensure that Docker has access to the GPU:
- Windows:
- Install the latest NVIDIA drivers.
- Install NVIDIA Container Toolkit.
- Windows:
-
Pull the PyTorch Docker image with GPU support:
docker pull pytorch/pytorch:latest
This command downloads the latest PyTorch image.
-
Run the Docker container:
docker run --gpus all -it --rm --shm-size=8g pytorch/pytorch:latest
This command starts a Docker container with GPU support and an interactive terminal.
--gpus all-> Enables GPU support.-it-> Interactive mode.--rm-> Automatically removes the container when it is stopped.--shm-size=8g-> Sets the shared memory size to 8 GB to avoid shared memory issues.
Alternatively, if you want a persistent container with access to local files, run:
docker run --gpus all -it --name pytorch_container --shm-size=8g -v $(pwd):/workspace pytorch/pytorch:latestThis command mounts the current directory to the
/workspacedirectory in the container.--name pytorch_container-> Names the container.-v $(pwd):/workspace-> Mounts the current directory to the/workspacedirectory in the container.
-
Verify the installation:
python -c "import torch; print(torch.cuda.is_available())"If the output is
True, PyTorch has been successfully installed with GPU support. -
Exit the Docker container:
exitThis command exits the Docker container.
-
Remove the Docker container (optional):
docker rmi pytorch/pytorch:latest
This command removes the PyTorch Docker image.
- Restart a stopped container:
docker start -ai pytorch_container
- Remove a stopped container:
docker rm pytorch_container
To verify that PyTorch has been successfully installed with GPU support, run the following Python code:
import torch
print("CUDA available:", torch.cuda.is_available()) # Returns True if CUDA is available
if torch.cuda.is_available():
current_device = torch.cuda.current_device()
print("Current device ID:", current_device)
print("Current device name:", torch.cuda.get_device_name(current_device))
print("Device memory address:", torch.cuda.device(current_device))
print("Total number of GPUs:", torch.cuda.device_count())- PyTorch Installation Guide: https://pytorch.org/get-started/locally/
- Python Downloads: https://www.python.org/downloads/
- NVIDIA CUDA Toolkit Archive: https://developer.nvidia.com/cuda-toolkit-archive
- NVIDIA cuDNN Archive: https://developer.nvidia.com/cudnn-archive
- Anaconda Distribution: https://www.anaconda.com/products/distribution
- Miniconda: https://docs.conda.io/en/latest/miniconda.html
- Docker Desktop: https://www.docker.com/get-started/
- NVIDIA Container Toolkit: https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/latest/install-guide.html
- PyTorch Docker Hub: https://hub.docker.com/r/pytorch/pytorch
- Pytorch GPU Setup Guide (The MCT Blog): https://mct-master.github.io/machine-learning/2023/04/25/olivegr-pytorch-gpu.html