Installing PyTorch on Ubuntu 24.04: A Step-by-Step Guide
You install PyTorch on Ubuntu 24.04 using command-line tools like pip or conda to manage packages.
PyTorch is a powerful open-source machine learning framework essential for many AI and deep learning applications. It features dynamic computation graphs, allowing you to build and iterate on models more easily.
For demanding tasks, PyTorch leverages NVIDIA’s CUDA toolkit to harness GPU acceleration, significantly speeding up your training times. This guide focuses on installing PyTorch version 2.3 on your Ubuntu 24.04 system.
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
Installing PyTorch on Ubuntu 24.04 using pip is a quick way to add this powerful tool to your system. Pip acts like a simple app store for Python, making it easy to install libraries like PyTorch.
To get started, you’ll need to install Python and the pip module. Run the command below to do that.
sudo apt update && sudo apt upgrade
sudo apt install python3 python3-pip
Once Python and pip are installed, it’s a good practice to create a Python virtual environment for PyTorch.
Create a Python virtual environment for PyTorch
Creating a Python virtual environment for PyTorch is smart because it keeps your project’s libraries separate from other Python stuff on your computer. This stops different programs from messing with each other.
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
You can install PyTorch on Ubuntu 24.04 with Anaconda, which is a popular tool for managing Python environments. Its smaller version, Miniconda, works well too.
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. 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, 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:
So, you’ve 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!