Installing PyTorch on Ubuntu 24.04: A Step-by-Step Guide
Installing PyTorch 2.3 on Ubuntu 24.04 gives you an open-source Python library for building and training machine learning and artificial intelligence models directly on your own computer. With an NVIDIA graphics card and CUDA, PyTorch can run its heavy calculations on the GPU instead of the CPU, so you can work on complex tasks without renting cloud servers.
This guide covers two ways to install it : pip inside a virtual environment, and Anaconda (through Miniconda). Pick one. You don’t need both. You also don’t need an NVIDIA GPU to follow the pip method, because PyTorch runs on the CPU as well, only more slowly.
Install PyTorch on Ubuntu 24.04 with pip inside a virtual environment. Run sudo apt update && sudo apt install python3 python3-pip virtualenv, then create the environment with virtualenv myenv and activate it with source myenv/bin/activate. Finally, run pip3 install torch torchvision torchaudio. No NVIDIA GPU is required; PyTorch runs on the CPU too.
Install PyTorch using pip
To install PyTorch with pip, first update your package list and make sure Python and pip are present. Open a terminal and run sudo apt update && sudo apt install python3 python3-pip. Enter your password when asked. Ubuntu 24.04 ships with Python 3 already, so apt may report that some packages are installed and only fetch what is missing.
When the command finishes and you get your prompt back, the system has what it needs to download PyTorch and its dependencies.
A virtual environment is a separate folder that holds its own Python packages for one project. Each project can then use its own library versions without breaking the others.
The system Python is marked as externally managed, so pip usually refuses to install packages into it and shows an “externally-managed-environment” error. Installing inside a virtual environment avoids that error and keeps the system Python clean.
The environment keeps PyTorch and everything it pulls in separate from other Python projects on the machine. If something goes wrong later, you can delete the environment folder and start again without touching the rest of the system.
Create a Python virtual environment for PyTorch
Create a Python virtual environment for PyTorch so its files stay apart from your other Python programs. First, install the tool that builds environments by running sudo apt install virtualenv in your terminal. This stops package versions from different projects from breaking each other when you install PyTorch.
Run the command below to install the package management tools. It needs administrator rights, so enter your password if prompted: sudo apt install virtualenv
sudo apt install virtualenv
Next, create the workspace and name it myenv. Run the following command from the folder where you want the environment to live. The name is only a label, so you can use a different one if you prefer, as long as you use the same name in later commands.
The command creates a new folder called myenv in your current directory. It prints a few lines about what it set up.
virtualenv myenv
Now activate the workspace with the command below. Run it from the same folder you used in the last step.
When activation works, the name of the environment, (myenv), appears at the start of your terminal prompt. From now on, anything you install with pip goes into this environment only. If the prompt does not change, you are probably in a different folder, so go back and try again.
source myenv/bin/activate
Run the command below to install PyTorch inside the active environment. Check that (myenv) is still showing in your prompt first.
The download is large, often several hundred megabytes or more, and can take a few minutes. pip prints progress as it downloads each package. The install is complete when you see a "Successfully installed" line and your prompt returns.
pip3 install torch torchvision torchaudio
Once the installation finishes, launch the Python shell and import PyTorch. The import can take a second or two the first time. If the shell opens with a >>> prompt and the import line returns without any error, PyTorch is installed and found.
A "ModuleNotFoundError" here usually means the environment is not active, so PyTorch was installed somewhere else or not at all.
python3 import torch
Check the installed version by printing it from the same Python shell.
print(torch.__version__)
The exact number depends on the release pip downloaded, but for this guide it should start with 2.3. Seeing a version number confirms PyTorch is installed. It does not by itself prove that the GPU is being used, which also depends on your NVIDIA driver being installed.
When your work is done, exit the Python shell, then deactivate the virtual environment with the command below. The (myenv) label disappears from your prompt, which means you are back in the normal system environment. To use PyTorch again later, run the activate command from the earlier step. Nothing is lost by deactivating.
deactivate
Install PyTorch using Anaconda
Anaconda is an alternative to pip for managing Python packages. It handles the Python version and the libraries together, which many people find easier for machine learning work. This guide uses Miniconda, the small installer from the Anaconda project, which includes the conda tool without the hundreds of extra packages of the full distribution.
Start by downloading the Miniconda installer. Run curl -O https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh in your terminal. Your PyTorch setup then stays self-contained and easy to manage, and you can remove it later by deleting the conda folder.
Visit the Anaconda website to check which Linux-x86_64 installer matches your system requirements. The x86_64 build is for standard 64-bit Intel and AMD processors. If your machine uses a different processor type, such as ARM, pick the matching installer from that page instead.
Run the command below to download the installer script. It saves the file in your current folder.
curl -O https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
Run the downloaded script with the command below to start the Anaconda setup. The terminal shows a welcome message and starts the license text.
bash Miniconda3-latest-Linux-x86_64.sh
Follow the prompts in the script. Press Enter to page through the license, then type yes to accept the license terms. Press Enter to accept the default installation location. If the installer asks whether to initialize conda in your shell, answer yes as well. The installer copies files for a minute or two and ends with a message that the installation is finished.
Now configure the shell so it can find the conda tool. Run the command below. Alternatively, close the terminal and open a new one. Either way, (base) should appear at the start of your prompt, which means conda is active.
source ~/.bashrc conda config --set auto_activate_base false
Install PyTorch by running the command below. Conda lists the packages it plans to install and asks you to confirm. Type y and press Enter to continue. The download is large, so allow a few minutes.
conda install pytorch torchvision torchaudio cpuonly -c pytorch
To add GPU support, run the command below. It requires a compatible NVIDIA GPU, the NVIDIA driver and a matching CUDA setup. Skip this step if you only want to run PyTorch on the CPU.
conda install pytorch torchvision torchaudio pytorch-cuda=11.8 -c pytorch -c nvidia
Change pytorch-cuda=11.7 to match the CUDA version installed on your machine. If conda reports that it cannot find the package, the CUDA version you chose is probably not published for the current PyTorch release. Newer PyTorch releases no longer offer builds for older CUDA versions such as 11.7. In that case, open the Get Started page on the PyTorch website and use the version it lists for your setup.
When the install finishes, open the Python shell and import PyTorch. If the import returns without an error, conda has installed PyTorch correctly.
python3 import torch
Verify the installation by printing the version number from the same Python shell.
print(torch.__version__)
If everything worked, the shell displays the installed version, which should start with 2.3 for this guide. If you see an import error instead, check that the terminal shows (base) in the prompt and that the install command finished without errors.
Configuration is now complete. You can leave the Python shell and start using PyTorch in your own projects.
Conclusion:
PyTorch is now installed on Ubuntu 24.04 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. On Ubuntu 24.04 it also avoids the externally-managed-environment error from pip.
- GPU Acceleration: Integrate PyTorch with CUDA for enhanced training performance on NVIDIA GPUs. Match the CUDA version to your setup and the PyTorch release.
- 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 gives you a working PyTorch environment, ready for artificial intelligence and deep learning projects. Activate your environment (or open a conda terminal) whenever you want to start work.
Have a question about this guide?
Answered by AI from this guide. Good questions may be added to this page — please don't include personal details.
Did this guide fix your problem?
About the Author
Richard
Tech Writer, IT Professional
Richard is a writer at Geek Rewind who turns complex IT tasks into clear, step-by-step guides. He draws on years of hands-on experience in system administration and enterprise IT operations, focusing on Windows, Linux and WordPress: the problems people actually run into, and the fixes that work. His server and WordPress guides come from systems he runs himself. Richard builds and maintains the platform behind Geek Rewind, from its Ubuntu servers and Nginx configuration to its custom WordPress plugins. Many new tutorials start with readers' questions, and he's always glad to answer them in the comments.
No comments yet — be the first to share your thoughts!