PyTorch Installation and Hardware Considerations on Ubuntu 24.04

 

  1. Introduction: This article provides a comprehensive guide to installing PyTorch on Ubuntu 24.04, including hardware detection, the differences between Intel CPU and GPU support, and detailed steps for both conda-based installation and building from source. It includes summaries, optional cases, and contextual discussions for complete understanding.
  2. Checking Hardware Compatibility: Before installing PyTorch, it is essential to determine your system’s hardware, especially GPU availability.
    Check for NVIDIA GPU
    $ lspci | grep -i nvidia
    $ nvidia-smi

    If nvidia-smi shows GPU info, NVIDIA drivers, and the GPU are present. If not, install drivers:
    $ sudo ubuntu-drivers autoinstall
    $ sudo reboot

    If NO NVIDIA GPU Is Detected, check the general display hardware:
    $ lspci
    $ lshw -C display
    $ lscpu

    If only Intel or AMD integrated graphics appear, proceed with CPU-only installation.
  3. Why Hardware Type Matters: PyTorch uses different backends depending on the hardware:
    • NVIDIA GPU: CUDA-enabled acceleration.
    • AMD GPU: ROCm-enabled acceleration (limited support).
    • Intel GPU: Currently unsupported for PyTorch GPU acceleration.
    • Intel/AMD CPU: CPU-only PyTorch with MKL/OpenBLAS.
    Knowing your hardware ensures you install the correct backend for performance and compatibility.
  4. Intel CPU vs Intel GPU:
    • Intel CPU: General-purpose processor.
    • Intel GPU: Integrated graphics, not usable for PyTorch acceleration.
    PyTorch does not currently support acceleration using Intel integrated GPUs. However, it works well with Intel CPUs, using optimized libraries such as MKL.
    Prerequisites for Intel CPU:
    No special prerequisites are required. Conda builds include:
    • Intel MKL
    • OpenBLAS
  5. Installing PyTorch via Conda
    Step 1: Install Miniconda
    $ wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
    $ bash Miniconda3-latest-Linux-x86_64.sh
    $ source ~/.bashrc

    Step 2: Create Environment and Install PyTorch CPU-only install:
    $ conda create -n pytorch-env python=3.10 -y
    $ conda activate pytorch-env
    $ conda install pytorch torchvision torchaudio cpuonly -c pytorch

    GPU install (NVIDIA CUDA 12.1):
    $ conda create -n pytorch-env python=3.10 -y
    $ conda activate pytorch-env
    $ conda install pytorch torchvision torchaudio pytorch-cuda=12.1 -c pytorch -c nvidia

    Step 3: Test Installation:
    $ python
    >> import torch
    >> print(“PyTorch version:”, torch.__version__)
    >> print(“CUDA available:”, torch.cuda.is_available())
    >> if torch.cuda.is_available():
    …….print(“GPU Name:”, torch.cuda.get_device_name(0))
  6. If Everything goes as expected. Congratulations! Now you can use PyTorch in this Anaconda environment.

Resources: