Installing PyTorch on Ubuntu 24.04: A Step-by-Step Guide
PyTorch 2.3 is a free Python library that lets you build and train machine learning and artificial intelligence models directly on your computer. Setting up version 2.3 on Ubuntu 24.04 connects the software to your NVIDIA graphics card using CUDA, which speeds up heavy calculations.
You can run this setup on your local desktop or laptop to train smart computer programs much faster without needing cloud servers. This guide shows you the exact steps to get PyTorch running on your Ubuntu system today.
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`.
Install PyTorch using pip
PyTorch installation on Ubuntu 24.04 uses pip, a tool that manages Python packages and their dependencies. Update local package lists and install Python along with pip by running `sudo apt update && sudo apt install python3 python3-pip`. This process prepares the computer to download and install PyTorch.
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
Project organization and library conflict prevention rely heavily on virtual environments. Installing necessary tools happens by running `sudo apt install virtualenv`. Dedicated spaces for PyTorch and its dependencies emerge from this setup process.
Install the required package management tools by running `sudo apt install virtualenv`.
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.
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
Anaconda offers another reliable method for managing Python projects and libraries on Ubuntu 24.04. Downloading the Miniconda installer happens through `curl -O https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh`. Separate and manageable PyTorch setups result from this approach.
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.
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.
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.
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?
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.
No comments yet — be the first to share your thoughts!