UbuntuUbuntu Based

How To Install PyTorch on Ubuntu 24.04 LTS

Install PyTorch on Ubuntu 24.04

PyTorch has become an essential tool for machine learning and artificial intelligence developers. This powerful open-source library offers dynamic computational graphs and efficient tensor computations, making it a popular choice for deep learning projects. In this guide, we’ll walk you through the process of installing PyTorch on Ubuntu 24.04, ensuring you have the latest version up and running on your system.

Prerequisites

Before we dive into the installation process, let’s ensure your system meets the necessary requirements:

  • Ubuntu 24.04 LTS installed and updated
  • Python 3.8 or later
  • Minimum 4GB RAM (8GB or more recommended for larger projects)
  • Administrative access (sudo privileges)

Updating Your System

First, let’s update your system packages to ensure everything is current:

sudo apt update && sudo apt upgrade -y

Installing Python and Pip

Ubuntu 24.04 comes with Python pre-installed, but let’s make sure we have the latest version:

sudo apt install python3 python3-pip

Verify the installation:

python3 --version
pip3 --version

Choosing Your Installation Method

There are two primary methods to install PyTorch on Ubuntu 24.04:

  1. Using Pip (Python’s package manager)
  2. Using Conda (Anaconda’s package manager)

Each method has its advantages, so let’s explore both options to help you decide which is best for your needs.

Method 1: Installing PyTorch Using Pip

Pip is Python’s default package manager and is a lightweight option for installing PyTorch.

Step 1: Set Up a Virtual Environment

It’s best practice to use a virtual environment to avoid conflicts with other Python projects:

sudo apt install python3-venv
python3 -m venv pytorch_env
source pytorch_env/bin/activate

Step 2: Install PyTorch

Now, let’s install PyTorch with CPU support:

pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu

For GPU support (if you have a compatible NVIDIA GPU):

pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118

Step 3: Verify the Installation

To ensure PyTorch is installed correctly, run the following Python commands:

import torch
print(torch.__version__)
print(torch.cuda.is_available())  # Should return True if GPU support is enabled

Method 2: Installing PyTorch Using Conda

Conda is a powerful package manager that can handle complex dependencies and create isolated environments.

Step 1: Install Miniconda

First, download and install Miniconda:

wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
bash Miniconda3-latest-Linux-x86_64.sh

Follow the prompts to complete the installation. Once done, restart your terminal or run:

source ~/.bashrc

Step 2: Create a Conda Environment

Create a new environment for PyTorch:

conda create --name pytorch_env python=3.8
conda activate pytorch_env

Step 3: Install PyTorch

For CPU-only support:

conda install pytorch torchvision torchaudio cpuonly -c pytorch

For GPU support:

conda install pytorch torchvision torchaudio cudatoolkit=11.8 -c pytorch

Step 4: Verify the Installation

Use the same Python commands as in the Pip method to verify your installation.

Installing PyTorch for ROCm Acceleration (AMD GPUs)

If you’re using an AMD GPU, you can install PyTorch with ROCm support:

  1. Install ROCm drivers from AMD’s official website
  2. Run the following command:
pip3 install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/rocm5.4.2

Troubleshooting Common Installation Issues

Missing Dependencies

If you encounter missing dependencies, try installing them manually:

sudo apt-get install libgl1-mesa-glx

CUDA Compatibility Problems

Ensure your NVIDIA driver version is compatible with the CUDA version required by PyTorch. You can check your driver version with:

nvidia-smi

Virtual Environment Activation Issues

If you’re having trouble activating your virtual environment, ensure you’re in the correct directory and try using the full path:

source /path/to/your/pytorch_env/bin/activate

Optimizing PyTorch Performance

To get the most out of PyTorch on your Ubuntu 24.04 system, consider the following tips:

Use GPU Acceleration

If you have a compatible GPU, make sure to install the GPU version of PyTorch for significant performance improvements in training deep learning models.

Optimize Your Code

Learn to use PyTorch’s built-in optimization techniques, such as:

  • Tensor operations on GPU
  • Proper batching of data
  • Using appropriate data types (e.g., float32 vs float64)

Keep PyTorch Updated

Regularly update PyTorch to benefit from performance improvements and new features:

pip3 install --upgrade torch torchvision torchaudio

Or for Conda:

conda update pytorch torchvision torchaudio

Exploring PyTorch Features

Now that you have PyTorch installed, let’s briefly explore some of its key features:

Dynamic Computational Graphs

PyTorch uses a dynamic computational graph, allowing for more flexible model architectures and easier debugging.

Tensor Computations

Tensors are the fundamental data structure in PyTorch. They’re similar to NumPy arrays but can be used on GPUs for faster computations.

Autograd

PyTorch’s automatic differentiation engine, Autograd, makes it easy to compute gradients for optimization in neural networks.

Neural Network Modules

The torch.nn module provides a high-level interface for building neural networks, including pre-built layers and loss functions.

Congratulations! You have successfully installed PyTorch. Thanks for using this tutorial for installing the PyTorch on Ubuntu 24.04 LTS system. For additional help or useful information, we recommend you check the official PyTorch website.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!

r00t

r00t is an experienced Linux enthusiast and technical writer with a passion for open-source software. With years of hands-on experience in various Linux distributions, r00t has developed a deep understanding of the Linux ecosystem and its powerful tools. He holds certifications in SCE and has contributed to several open-source projects. r00t is dedicated to sharing her knowledge and expertise through well-researched and informative articles, helping others navigate the world of Linux with confidence.
Back to top button