Installing PyTorch on Ubuntu 24.04: A Step-by-Step Guide

This article provides a detailed guide for installing PyTorch on Ubuntu 24.04 using both pip and Anaconda. It explains the significance of PyTorch in machine learning, highlights its compatibility with CUDA for GPU acceleration, and outlines steps for setting up a Python virtual environment or Anaconda for installation.

This article explains how to install PyTorch on Ubuntu 24.04.

PyTorch is an open-source machine learning library widely used for deep learning and artificial intelligence applications. Unlike other frameworks, PyTorch allows for dynamic computation graphs, which means you can change the network architecture during runtime. This is useful for tasks like debugging and debugging complex models.

PyTorch seamlessly integrates with CUDA for GPU acceleration. This feature lets you run your models on NVIDIA GPUs, improving performance significantly during training.

Installing PyTorch on Ubuntu is straightforward, especially with package managers like pip or conda, which can handle dependencies and installation processes effectively.

Install PyTorch using pip

The quickest way to install PyTorch on Ubuntu is to use pip. Pip is a package manager for Python that lets you install and manage extra libraries that are not part of the Python standard library.

To install pip, you must install Python and pip module. Run the command below to do that.

sudo apt update && sudo apt upgrade
sudo apt install python3 python3-pip

After installing Python and pip modules, create a Python virtual environment to install PyTorch.

Create a Python virtual environment for PyTorch

Using a Python virtual environment keeps your project dependencies separate from other Python installed on your system.

To create a Python virtual environment, run the command below to install Python environment packages.

sudo apt install virtualenv

Next, create a virtual environment named myenv.

virtualenv myenv

Next, activate the virtual environment by running the command below.

source myenv/bin/activate

Once the environment is activated, run the command below to install PyTorch.

pip3 install torch torchvision torchaudio

Once installed, run the Python shell and import PyTorch.

python
import torch

Then, print the version to check the version installed on your machine.

print(torch.__version__)

This prints the installed PyTorch version if successful.

Once you’re done, you can deactivate the virtual environment by running the command below.

deactivate

Install PyTorch using Anaconda

Another way to install PyTorch is to use Anaconda. Miniconda is a lightweight Anaconda version designed to manage Python environments.

Visit the Anaconda website and download the correct installer for your system (Linux-x86_64).

Use the command below to download it.

curl -O https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh

Once the script is downloaded, run the command below to install Anaconda.

bash Miniconda3-latest-Linux-x86_64.sh

Follow the prompts and accept the terms, installation location, and more. Type ‘yes‘ for all the prompts.

Once installed, configure your environment to execute Anaconda.

source ~/.bashrc
conda config --set auto_activate_base false

After that, run the command below to install PyTorch.

conda install pytorch torchvision torchaudio cpuonly -c pytorch

Add GPU support (requires compatible NVIDIA GPU and CUDA toolkit):

conda install pytorch torchvision torchaudio pytorch-cuda=11.7 -c pytorch -c nvidia

Replace pytorch-cuda=11.7 with the correct version for your CUDA installation.

Once installed, run the Python shell and import PyTorch.

python
import torch

Then, print the version to check the version installed on your machine.

print(torch.__version__)

This prints the installed PyTorch version if successful.

That should do it!

Conclusion:

In summary, installing PyTorch on Ubuntu 24.04 can be accomplished easily using either pip or Anaconda. Here are the key takeaways:

  • Flexible Installation Methods: Choose pip for simplicity or Anaconda for advanced environment management.
  • Virtual Environments: Using a virtual environment helps maintain project dependencies and prevents conflicts.
  • GPU Acceleration: Integrate PyTorch with CUDA for enhanced training performance on NVIDIA GPUs.
  • Version Verification: Always check the installed PyTorch version to ensure your setup is correct.
  • Community and Support: PyTorch has a large community and extensive documentation, making it a great choice for deep learning projects.

Following the steps outlined, you can successfully set up PyTorch and begin your journey into deep learning and artificial intelligence.

Richard Avatar

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *