Installing PyTorch with GPU Support and CUDA on Ubuntu: A Guide

Posted by

How to Install PyTorch with GPU Support and CUDA on Ubuntu

How to Install PyTorch with GPU Support and CUDA on Ubuntu

If you want to take advantage of the power of your GPU for faster computations when using PyTorch, you’ll need to install PyTorch with GPU support and CUDA on Ubuntu. Here’s a step-by-step guide to help you get set up:

Step 1: Update Your System

Before you begin, it’s always a good idea to update your system to ensure you have the latest packages. You can do this by running the following commands in your terminal:

sudo apt update
sudo apt upgrade

Step 2: Install CUDA

CUDA is a parallel computing platform and application programming interface model created by NVIDIA. PyTorch relies on CUDA for GPU acceleration, so you’ll need to install it on your system. You can download the CUDA toolkit from the NVIDIA website or install it via the package manager. For example, you can install CUDA 10.2 with the following commands:

wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/cuda-ubuntu1804.pin
sudo mv cuda-ubuntu1804.pin /etc/apt/preferences.d/cuda-repository-pin-600
sudo apt-key adv --fetch-keys http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/7fa2af80.pub
sudo add-apt-repository "deb http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/ /"
sudo apt-get update
sudo apt-get -y install cuda-10-2

Step 3: Install PyTorch

Once you have CUDA installed on your system, you can install PyTorch with GPU support using pip. Make sure you have pip installed on your system by running:

sudo apt install python3-pip

Then, you can install PyTorch with GPU support by running the following command:

pip install torch torchvision torchaudio

Step 4: Verify Your Installation

To verify that PyTorch is using your GPU for computations, you can run the following Python code:

import torch
print(torch.cuda.is_available())

If the output is True, then PyTorch is successfully using your GPU for computations.

That’s it! You’ve now successfully installed PyTorch with GPU support and CUDA on Ubuntu. You can now take advantage of your GPU for faster computations when working with PyTorch.

0 0 votes
Article Rating
4 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@timurcoldant3253
1 month ago

Hi! If i have nvidia driver 535.. i can install only 12.3 and 12.4 Cuda but for pytorch there is no option using it, or not? Can u help me pls.

@sethuraman-cd2qr
1 month ago

this not works for me. i'm using aws ubuntu 22.04 with aarch64. please provide solution to install pytorch cuda enable

@user-qr6kg4rf9d
1 month ago

is this all it takes to use the gpu with pytorch ?

@FunkBass87
1 month ago

Thank you for sharing! Would you consider sharing a .gist or breakdown of the command line codes used?