How to Install PyTorch with CUDA on Ubuntu

PyTorch is an open-source machine learning framework that enables developers to build and train neural networks for AI applications. When combined with NVIDIA CUDA, PyTorch can leverage GPU acceleration to perform computations up to and even over 100 times faster than CPU-only processing. In this tutorial, you will learn how to install PyTorch with CUDA support on Ubuntu Linux, enabling you to harness the full power of your NVIDIA GPU for machine learning and deep learning tasks.

Whether you’re working on computer vision, natural language processing, or other AI applications, PyTorch with CUDA provides the foundation for efficient model training and inference. This guide covers the complete installation process, from setting up a Python virtual environment to verifying GPU detection and testing actual performance gains.

In this tutorial you will learn:

  • How to prepare your system for PyTorch installation
  • How to create a Python virtual environment
  • How to install PyTorch with CUDA support
  • How to verify GPU detection in PyTorch
  • How to benchmark GPU vs CPU performance
How to Install PyTorch with CUDA on Ubuntu
How to Install PyTorch with CUDA on Ubuntu

Software Requirements and Conventions Used

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Ubuntu Linux (LTS versions recommended)
Software PyTorch, CUDA Toolkit (see CUDA installation guide), Python 3.8 or later
Other NVIDIA GPU with CUDA support, privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

How to install PyTorch with CUDA on Ubuntu step by step instructions

DID YOU KNOW?
PyTorch was initially developed by Meta AI (formerly Facebook AI Research) and released in 2016. It quickly became one of the most popular deep learning frameworks due to its intuitive Python-first approach and dynamic computation graphs. Unlike static frameworks, PyTorch allows you to modify your neural network architecture on-the-fly during runtime, making it particularly favored in research environments. Today, PyTorch powers many cutting-edge AI applications, from OpenAI’s GPT models to Tesla’s self-driving technology.



Verify System Prerequisites

  1. First, verify that CUDA is properly installed on your system:
    $ nvcc --version
    nvcc: NVIDIA (R) Cuda compiler driver
    Copyright (c) 2005-2025 NVIDIA Corporation
    Built on Wed_Aug_20_01:58:59_PM_PDT_2025
    Cuda compilation tools, release 13.0, V13.0.88
    Build cuda_13.0.r13.0/compiler.36424714_0
    
  2. Check your Python version. PyTorch requires Python 3.8 or later:
    $ python3 --version
    Python 3.12.3
    
  3. Install Python pip and virtual environment tools if not already available:
    $ sudo apt update
    $ sudo apt install python3-pip python3-venv
    

Create Python Virtual Environment

  1. Create a dedicated directory for your PyTorch installation:
    $ mkdir ~/pytorch-test
    $ cd ~/pytorch-test
    
  2. Create and activate a Python virtual environment:
    $ python3 -m venv venv
    $ source venv/bin/activate
    

    After activation, your command prompt should change to show (venv) at the beginning, indicating you’re now working inside the virtual environment.

WHY USE VIRTUAL ENVIRONMENTS?
Python virtual environments isolate project dependencies, preventing conflicts between different projects. This is especially important for machine learning work where specific package versions may be required. You can deactivate the environment anytime by running deactivate.



Install PyTorch with CUDA Support

  1. With the virtual environment activated, install PyTorch with CUDA support. For CUDA 12.x and 13.x installations, use the following command:
    $ pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu124
    

    The installation will download PyTorch along with its dependencies, including the necessary CUDA libraries. This may take several minutes depending on your internet connection.

    NOTE
    PyTorch uses CUDA 12.4 wheels (cu124) which are compatible with CUDA 13.x installations due to CUDA’s forward compatibility. If you have an older CUDA version, visit the official PyTorch website to find the appropriate installation command for your CUDA version.

Verify PyTorch and GPU Detection

  1. Create a simple Python script to verify the installation. Create a new file:
    $ nano test_gpu.py
    
  2. Add the following code to the file:
    import torch
    
    print(f"PyTorch version: {torch.__version__}")
    print(f"CUDA available: {torch.cuda.is_available()}")
    print(f"CUDA version: {torch.version.cuda}")
    print(f"GPU count: {torch.cuda.device_count()}")
    if torch.cuda.is_available():
        print(f"GPU name: {torch.cuda.get_device_name(0)}")
    

    Save the file by pressing Ctrl+O, then Enter, and exit with Ctrl+X.

  3. Run the verification script:
    $ python test_gpu.py
    PyTorch version: 2.6.0+cu124
    CUDA available: True
    CUDA version: 12.4
    GPU count: 1
    GPU name: NVIDIA GeForce RTX 3080
    

    If you see CUDA available: True and your GPU name, PyTorch has successfully detected your NVIDIA GPU.

    Terminal window showing PyTorch GPU verification script output confirming CUDA availability and NVIDIA GeForce RTX 3080 detection on Ubuntu
    Verifying PyTorch installation and GPU detection – RTX 3080 successfully recognized

Test GPU Performance

To demonstrate the performance benefits of GPU acceleration, let’s run a simple benchmark comparing CPU and GPU computation speeds.

  1. Create a benchmark script:
    $ nano benchmark.py
    
  2. Add the following code:
    import torch
    import time
    
    # Create random tensor
    size = 10000
    x = torch.randn(size, size)
    
    # CPU benchmark
    start = time.time()
    result_cpu = torch.matmul(x, x)
    cpu_time = time.time() - start
    
    # GPU benchmark
    x_gpu = x.cuda()
    torch.cuda.synchronize()
    start = time.time()
    result_gpu = torch.matmul(x_gpu, x_gpu)
    torch.cuda.synchronize()
    gpu_time = time.time() - start
    
    print(f"CPU time: {cpu_time:.4f} seconds")
    print(f"GPU time: {gpu_time:.4f} seconds")
    print(f"Speedup: {cpu_time/gpu_time:.2f}x faster on GPU")
    
  3. Run the benchmark eg.:
    $ python benchmark.py
    CPU time: 18.1828 seconds
    GPU time: 0.1849 seconds
    Speedup: 98.32x faster on GPU
    

    This demonstrates the dramatic performance improvement when using GPU acceleration for computationally intensive operations. Your results may vary depending on your specific GPU model.

    Terminal showing PyTorch GPU benchmark results with 105-114x speedup over CPU, alongside NVIDIA Settings window displaying RTX 3080 information on Ubuntu
    PyTorch GPU benchmark showing over 100x performance improvement on RTX 3080 compared to CPU

Troubleshooting

CUDA Not Detected by PyTorch

If PyTorch shows CUDA available: False, verify that:

  • CUDA Toolkit is properly installed using nvcc --version
  • NVIDIA drivers are correctly installed using nvidia-smi
  • You installed PyTorch with the correct CUDA version support

ImportError or Module Not Found

Ensure you’re working inside the activated virtual environment. Your prompt should show (venv). If not, activate it again:

$ source ~/pytorch-test/venv/bin/activate

Out of Memory Errors

If you encounter GPU out of memory errors during the benchmark, reduce the matrix size in the benchmark script from 10000 to a smaller value like 5000.



Closing Thoughts

In this tutorial, we successfully installed PyTorch with CUDA support on Ubuntu Linux and verified GPU acceleration is working correctly. The dramatic performance improvement demonstrated in our benchmark (up to 100x faster) shows why GPU acceleration is essential for machine learning and deep learning tasks. With PyTorch and CUDA properly configured, you’re now ready to build and train neural networks, process large datasets, and develop AI applications that leverage the full power of your NVIDIA GPU.



Comments and Discussions
Linux Forum