Installing PyTorch on Ubuntu 24.04: A Step-by-Step Guide
Installing PyTorch on Ubuntu 24.04 lets you use a powerful tool for building AI and machine learning models.
PyTorch is a free software library that helps developers create and train artificial intelligence, especially deep learning, much faster. It’s known for making complex model building more straightforward.
This guide specifically covers installing PyTorch version 2.3 on Ubuntu 24.04. Version 2.3 can use NVIDIA’s CUDA toolkit, which greatly speeds up calculations by using your graphics card for demanding jobs.
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 your system and install Python and pip with the following command: `sudo apt update && sudo apt upgradesudo apt install python3 python3-pip`. This process prepares your computer to download and install PyTorch.
With Python and pip installed, creating a Python virtual environment is a good next step.
Creating a Python virtual environment for PyTorch is a good practice after installing Python and pip. This isolated environment keeps PyTorch and its dependencies separate, preventing conflicts with other Python projects on your system.
Create a Python virtual environment for PyTorch
Creating a Python virtual environment for PyTorch on Ubuntu 24.04 keeps your projects organized and prevents library conflicts. To start, install the necessary tools by running: sudo apt install virtualenv. Then, you can create your own isolated space for PyTorch and its dependencies.
To create a Python virtual environment, use the following command to install Python environment packages.
sudo apt install virtualenv
Next, create a virtual environment named myenv.
virtualenv myenv
Next, activate the virtual environment with the following command.
source myenv/bin/activate
Once the environment is activated, use this command 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 with the following command.
deactivate
Install PyTorch using Anaconda
Installing PyTorch on Ubuntu 24.04 using Anaconda is an effective way to manage Python projects and libraries. Download the Miniconda installer for your system with the following command: curl -O https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh. Anaconda helps keep your PyTorch setup separate and manageable.
Download the installer with this command.
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. You can 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, use 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
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:
You’ve now learned how to install PyTorch on Ubuntu 24.04 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.
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!