
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:
- Have access to a Debian 12 server as a non-root user with sudo privileges.
Create a Python Virtual Environment
To isolate your PyTorch project and avoid system-level conflicts, create a virtual environment using the virtualenv
tool.
Verify that Python 3.9 or later is installed.
console$ python3 --version
Output:
Python 3.11.2
Confirm that
pip
is available.console$ pip --version
Output:
pip 23.0.1 from /usr/lib/python3/dist-packages/pip (python 3.11)
Install
virtualenv
.console$ sudo apt install virtualenv -y
Create a new virtual environment named
pytorch_project_env
.console$ virtualenv pytorch_project_env
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 usingvirtualenv
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.
To install the latest CPU-only version of PyTorch, use the official PyTorch wheel repository with pip
.
Make sure your virtual environment is active.
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.
Anaconda is a Python distribution that includes Conda, a package and environment manager. You can use Conda to install PyTorch and its associated libraries from the official PyTorch channel.
If you haven't installed Anaconda yet, follow the Anaconda installation guide for Debian 12 before proceeding.
Run the following command to install PyTorch with CPU-only support.
console$ conda install pytorch torchvision torchaudio cpuonly -c pytorch
When prompted, confirm the installation by typing
y
.The output should include lines similar to:
Downloading and Extracting Packages: Preparing transaction: done Verifying transaction: done Executing transaction: done
This method installs torch
, torchvision
, and torchaudio
with compatible versions for your Conda environment.
Verify Installation
You can verify your PyTorch installation by launching the Python interpreter and running a few basic commands.
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. >>>
Import the PyTorch package.
python>>> import torch
Print the installed PyTorch version.
python>>> print(torch.__version__)
Output:
2.7.0+cu126
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]])
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.
No comments yet.