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
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.
Create a dedicated directory for your PyTorch installation:
$ mkdir ~/pytorch-test
$ cd ~/pytorch-test
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
With the virtual environment activated, install PyTorch with CUDA support. For CUDA 12.x and 13.x installations, use the following command:
The installation will download PyTorch along with its dependencies, including the necessary CUDA libraries. This may take several minutes depending on your internet connection.
Verify PyTorch and GPU Detection
Create a simple Python script to verify the installation. Create a new file:
$ 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.
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.