PyTorch is a popular open-source machine learning library that enables developers and researchers to build and deploy deep learning models efficiently. Installing PyTorch on Ubuntu 24.04 allows you to leverage GPU acceleration and utilize its extensive ecosystem for AI projects. Whether you’re setting up a new environment or upgrading an existing one, following this guide ensures a smooth installation process. Before starting, ensure your system is up to date and you have access to a terminal with administrative privileges.
What You’ll Need
- Ubuntu 24.04 installed and running
- Internet connection for downloading packages
- Basic familiarity with terminal commands
- Optional: NVIDIA GPU with the latest drivers for GPU acceleration
- Anaconda or Miniconda (recommended for managing environments)
Step-by-Step
- Update Your System
- Install Necessary Dependencies
- Choose Your Installation Method
- Install Anaconda/Miniconda (Optional but Recommended)
- Create a New Conda Environment
- Install PyTorch via Conda
- Install PyTorch via pip (Optional)
- Verify the Installation
Open your terminal and run the following commands to ensure your system packages are current:
sudo apt update
sudo apt upgrade -y
Install essential packages needed for Python and CUDA support if applicable:
sudo apt install python3-pip python3-dev build-essential -y
You can install PyTorch either via pip or Conda. Conda is recommended for managing dependencies and environments, especially for GPU support.
If you prefer Conda, download Miniconda from the official website:
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
bash Miniconda3-latest-Linux-x86_64.sh
Follow the prompts to complete installation, then restart your terminal or run source ~/.bashrc.
It’s best to isolate your PyTorch installation:
conda create -n pytorch_env python=3.10 -y
conda activate pytorch_env
Visit the official PyTorch website’s installation selector (https://pytorch.org/get-started/locally/) to get the latest command tailored for your setup. For most cases with GPU support, run:
conda install pytorch torchvision torchaudio pytorch-cuda=11.8 -c pytorch -c nvidia
If you are using CPU only, install with:
conda install pytorch torchvision torchaudio cpuonly -c pytorch
If you prefer pip, ensure pip is up to date:
pip install --upgrade pip
Then install PyTorch with:
pip install torch torchvision torchaudio
Open a Python shell and run the following commands to check if PyTorch is installed correctly:
import torch
print(torch.__version__)
print(torch.cuda.is_available())
If the CUDA availability returns True and the version prints correctly, your GPU setup is ready. Otherwise, it’s a CPU-only installation.
Common Mistakes
- Not updating the system before installation, leading to dependency issues.
- Using incompatible CUDA versions with your GPU drivers, causing runtime errors.
- Installing PyTorch outside a dedicated environment, risking dependency conflicts.
- Forgetting to activate the environment before installing or verifying PyTorch.
- Skipping the verification step, which might hide installation problems.
Tips
- Always check the latest recommended command for your setup on the official PyTorch site to ensure compatibility.
- If you encounter CUDA errors, verify your GPU driver is correctly installed and compatible with your CUDA version.
- Use Conda environments to easily manage different project dependencies and avoid conflicts.
- Regularly update your NVIDIA drivers and CUDA toolkit if you plan to work with GPU acceleration.
- For troubleshooting, clear cache or reinstall if issues persist.
Troubleshooting
If the installation does not go as planned, follow these steps:
- Check for Compatibility
- Verify CUDA Installation
- Reinstall PyTorch
- Check Environment Activation
- Consult Logs and Errors
Ensure your GPU drivers and CUDA toolkit are compatible with the version of PyTorch you installed. Visit the NVIDIA driver download page and CUDA toolkit documentation for guidance.
Run nvidia-smi in the terminal to check if your GPU is recognized and the driver is functioning properly. If not, reinstall or update your GPU drivers.
Sometimes, a clean reinstall helps. Remove existing PyTorch installations and reinstall following the latest instructions.
If using Conda, ensure you have activated the correct environment before running your scripts.
Read the error messages carefully; they often give clues about missing dependencies or incompatible versions.
If issues persist, seek help on forums like Stack Overflow or the PyTorch discussion boards, providing details about your system configuration and error logs.
Frequently Asked Questions
1. Can I install PyTorch without a GPU?
Yes, you can install the CPU-only version of PyTorch. Use the command: pip install torch torchvision torchaudio or the Conda equivalent with cpuonly.
2. How do I verify if PyTorch is installed correctly?
Open a Python terminal and run import torch; print(torch.__version__). To check GPU support, run print(torch.cuda.is_available()). If it returns True, your GPU setup is working.
3. Can I install PyTorch with GPU support on Ubuntu 24.04?
Yes, provided your GPU drivers and CUDA toolkit are compatible. Follow the instructions for installing CUDA and the appropriate PyTorch version with CUDA support.
4. What should I do if I get CUDA errors after installation?
Check your NVIDIA driver version with nvidia-smi. Ensure it matches the CUDA version required by PyTorch. Reinstall or update drivers if necessary.
5. Is it better to install PyTorch via Conda or pip?
Both methods work well. Conda manages dependencies more effectively, especially for GPU support. Use pip if you prefer a lightweight setup or do not need environment management.
6. How often should I update my PyTorch installation?
Stay updated with the latest stable releases by periodically running the install commands. Check the official website for new versions and compatibility notes.
By following this comprehensive guide, you can successfully install PyTorch on Ubuntu 24.04, whether for CPU or GPU acceleration. Always verify your setup and keep your drivers and dependencies current for optimal performance and compatibility.