Skip to content
Follow
Ubuntu Linux

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

Richard
Written by
Richard
Jan 13, 2025 Updated Sep 15, 2026 3 min read
Installing PyTorch on Ubuntu 24.04: A Step-by-Step Guide
Installing PyTorch on Ubuntu 24.04: A Step-by-Step Guide

Installing PyTorch 2.3 on Ubuntu 24.04 sets up an open-source Python library used to build and train machine learning and artificial intelligence models directly on your computer. This setup connects the software to your NVIDIA graphics card using CUDA, which speeds up heavy calculations so you can run complex tasks without cloud servers.

Advertisement
⚡ Quick Answer

Install PyTorch on Ubuntu 24.04 using either pip or conda. For pip, create a virtual environment with `virtualenv myenv`, activate it with `source myenv/bin/activate`, then run `pip3 install torch torchvision torchaudio`. For conda, install Miniconda, then run `conda install pytorch torchvision torchaudio cpuonly -c pytorch`.

Advertisement

Install PyTorch using pip

You can install PyTorch using pip on Ubuntu 24.04 by first updating your system packages and getting Python. Run the command sudo apt update && sudo apt install python3 python3-pip in your terminal. This gets your system ready to download PyTorch and the tools it needs to run properly.

Isolated virtual environments keep project dependencies separate and prevent version conflicts across different Python projects.

This isolated environment keeps PyTorch and its dependencies separate, preventing conflicts with other Python projects on the system.

Create a Python virtual environment for PyTorch

You need to create a Python virtual environment for PyTorch to keep your project files separate from other Python programs on your computer. Run sudo apt install virtualenv in your terminal to get the tools you need. This stops different package versions from breaking each other when you install PyTorch.

Install the required package management tools by running `sudo apt install virtualenv`.

Advertisement
sudo apt install virtualenv

Set up a dedicated workspace named myenv by running the following command.

virtualenv myenv

Activate the workspace using this command.

source myenv/bin/activate

Running this command installs PyTorch inside the active environment.

pip3 install torch torchvision torchaudio

Launch the Python shell and import PyTorch after the installation finishes.

Advertisement
python
import torch

Check the installed version by printing it.

print(torch.__version__)

A successful installation outputs the version number.

Deactivate the virtual environment using the command below when work concludes.

deactivate

Install PyTorch using Anaconda

You can install PyTorch using Anaconda on Ubuntu 24.04 as an alternative to pip for managing your Python packages. Download the Miniconda installer by running curl -O https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh in your terminal. This approach keeps your PyTorch setup self-contained and easy to manage.

Advertisement

Visit the Anaconda website to fetch the Linux-x86_64 installer matching system requirements.

Execute this command to fetch the installer script.

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

Run the downloaded script to complete the Anaconda setup.

bash Miniconda3-latest-Linux-x86_64.sh

Accept the license terms and default installation location by typing yes when prompted by the script.

Advertisement

Configure the shell environment for Anaconda execution.

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

Install PyTorch by executing the following command.

conda install pytorch torchvision torchaudio cpuonly -c pytorch

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

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

Change pytorch-cuda=11.7 to match the specific CUDA version installed on the machine.

Advertisement

Open the Python shell and import PyTorch.

python
import torch

Verify the installation by printing the version number.

print(torch.__version__)

Successful execution displays the installed version.

Configuration is now complete.

Advertisement

Conclusion:

PyTorch installation on Ubuntu 24.04 is now complete using either pip or Anaconda. Key takeaways include:

  • 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 these steps establishes a working PyTorch environment ready for artificial intelligence and deep learning projects.

Was this guide helpful?

Was this helpful?
Richard

About the Author

Richard

Tech Writer, IT Professional

Richard, a writer for Geek Rewind, is a tech enthusiast who loves breaking down complex IT topics into simple, easy-to-understand ideas. With years of hands-on experience in system administration and enterprise IT operations, he’s developed a knack for offering practical tips and solutions. Richard aims to make technology more accessible and actionable. He's deeply committed to the Geek Rewind community, always ready to answer questions and engage in discussions.

Advertisement

📚 Related Tutorials

How to Install KDE Desktop on Ubuntu 24.04
Ubuntu Linux How to Install KDE Desktop on Ubuntu 24.04
How to Install Python Pip on Ubuntu Linux
Ubuntu Linux How to Install Python Pip on Ubuntu Linux
Installing Gatsby.js on Ubuntu: A Step-by-Step Guide
Ubuntu Linux Installing Gatsby.js on Ubuntu: A Step-by-Step Guide
How to Install Python on Ubuntu Linux
Ubuntu Linux How to Install Python on Ubuntu Linux

No comments yet — be the first to share your thoughts!

Leave a Comment

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