How to Install PyTorch on Debian 12

Updated on 02 July, 2025
Learn to install PyTorch on Debian 12 with pip or Conda, plus setup and verification instructions.
How to Install PyTorch on Debian 12 header image

PyTorch is an open-source tensor computation library developed by Meta AI. It supports GPU and CPU acceleration and is widely used for building deep learning models in applications like computer vision, NLP, and reinforcement learning.

This article shows you how to install PyTorch on Debian 12 using either pip or Anaconda, create an isolated Python environment, and verify the installation with a simple test script.

Prerequisites

Before you begin, you need to:

Create a Python Virtual Environment

To isolate your PyTorch project and avoid system-level conflicts, create a virtual environment using the virtualenv tool.

  1. Verify that Python 3.9 or later is installed.

    console
    $ python3 --version
    

    Output:

    Python 3.11.2
  2. Confirm that pip is available.

    console
    $ pip --version
    

    Output:

    pip 23.0.1 from /usr/lib/python3/dist-packages/pip (python 3.11)
  3. Install virtualenv.

    console
    $ sudo apt install virtualenv -y
    
  4. Create a new virtual environment named pytorch_project_env.

    console
    $ virtualenv pytorch_project_env
    
  5. Activate the virtual environment.

    console
    $ source pytorch_project_env/bin/activate
    

    The shell prompt changes to indicate the active environment.

    (pytorch_project_env) linuxuser@vultr:~$

When to Use pip vs. conda

  • Use pip if you're working in a lightweight virtual environment using virtualenv or prefer to manage packages manually.

  • Use Anaconda if you're already using Conda environments or need access to a broader scientific computing stack with simplified dependency management.

Both methods install the same PyTorch libraries. Choose the one that best fits your development setup.

Install PyTorch with pip

To install the latest CPU-only version of PyTorch, use the official PyTorch wheel repository with pip.

  1. Make sure your virtual environment is active.

  2. Install PyTorch and related packages.

    console
    $ pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu
    
    • torch: Core PyTorch library for tensor computation and deep learning.
    • torchvision: Utilities and pretrained models for computer vision.
    • torchaudio: Audio processing tools for deep learning applications.

Verify Installation

You can verify your PyTorch installation by launching the Python interpreter and running a few basic commands.

  1. Open the Python interpreter.

    console
    $ python
    

    Output:

    Python 3.11.2 (main) [GCC 12.2.0] on linux
    Type "help", "copyright", "credits", or "license" for more information.
    >>>
  2. Import the PyTorch package.

    python
    >>> import torch
    
  3. Print the installed PyTorch version.

    python
    >>> print(torch.__version__)
    

    Output:

    2.7.0+cu126
  4. Generate a 5x3 tensor of random values to confirm basic functionality.

    python
    >>> x = torch.rand(5, 3)
    
    python
    >>> print(x)
    

    The output should be similar to:

    tensor([[0.7363, 0.4839, 0.1370],
        [0.6429, 0.0108, 0.9823],
        [0.8575, 0.9642, 0.5792],
        [0.8951, 0.8762, 0.7949],
        [0.7175, 0.0135, 0.9493]])
  5. To exit the interpreter, type:

    python
    >>> exit()
    

Conclusion

You successfully installed PyTorch on a Debian 12 server using either pip or Anaconda and verified the setup with a test script. With PyTorch, you can now build and train deep learning models for computer vision, natural language processing, and reinforcement learning.

To explore additional libraries and capabilities, visit the official PyTorch documentation.

Comments

No comments yet.