Installing PyTorch on Debian 12 with pip or Anaconda
PyTorch is an open-source tensor computation framework created by Meta AI. It offers both CPU and GPU acceleration and is a popular choice for developing deep learning models in areas such as computer vision, natural language processing, and reinforcement learning.
This guide explains how to install PyTorch on Debian 12 using either pip or Anaconda, set up an isolated Python environment, and confirm the installation with a basic test script.
Prerequisites
Before starting, ensure you meet the following requirements:
- Access to a Debian 12 server as a non-root user with sudo privileges.
Create a Python Virtual Environment
To keep your PyTorch project isolated and prevent conflicts with system-level packages, create a virtual environment using the virtualenv
tool.
First, confirm that Python 3.9 or newer is installed:
$ python3 --version
Example output:
Python 3.11.2
Check that pip is installed:
$ pip --version
Example output:
pip 23.0.1 from /usr/lib/python3/dist-packages/pip (python 3.11)
Install virtualenv:
$ sudo apt install virtualenv -y
Create a new virtual environment named pytorch_project_env
:
$ virtualenv pytorch_project_env
Activate the virtual environment:
$ source pytorch_project_env/bin/activate
The shell prompt will change to reflect the active environment:
(pytorch_project_env) linuxuser@centron:~$
Choosing Between pip and conda
Use pip if you prefer a lightweight environment created with virtualenv or want to manually manage packages.
Use Anaconda if you already work with Conda environments or require a more comprehensive scientific computing stack with simplified dependency handling.
Both methods install the same PyTorch packages. Pick the option that aligns with your workflow.
Installing PyTorch with pip
To install the latest CPU-only version of PyTorch, use the official PyTorch wheel repository. Ensure your virtual environment is active before running the command:
$ pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu
- torch: Core library for tensor computation and deep learning.
- torchvision: Tools and models for computer vision.
- torchaudio: Audio processing utilities for deep learning.
Installing PyTorch with Anaconda
Anaconda is a Python distribution with Conda for managing packages and environments. Use Conda to install PyTorch from the official PyTorch channel.
If Anaconda is not installed, follow its Debian 12 installation instructions before continuing.
Install the CPU-only version of PyTorch:
$ conda install pytorch torchvision torchaudio cpuonly -c pytorch
When asked, confirm by typing y
. The output will include messages such as:
Downloading and Extracting Packages:
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
Verifying the Installation
To confirm PyTorch is installed, start the Python interpreter:
$ python
Example output:
Python 3.11.2 (main) [GCC 12.2.0] on linux
Type "help", "copyright", "credits", or "license" for more information.
>>>
Import PyTorch:
>>> import torch
Print the PyTorch version:
>>> print(torch.__version__)
Example output:
2.7.0+cu126
Create a random 5×3 tensor to test functionality:
>>> x = torch.rand(5, 3)
>>> print(x)
Example output:
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]])
Exit the interpreter:
>>> exit()
Conclusion
You have installed PyTorch on Debian 12 using either pip or Anaconda and validated the installation with a test script. You can now begin creating and training deep learning models for tasks like computer vision, natural language processing, and reinforcement learning.
For further details and resources, check the official PyTorch documentation.