pip install torch "error: externally-managed-environment" (tested)

pip install torch on Debian 12+/Ubuntu 23.04+ stops with error: externally-managed-environment, and --user fails the same way. Install PyTorch into a virtual environment instead, and use the CPU index if you have no NVIDIA GPU; reproduced and fixed below.

1. Reproduce: pip install torch on Debian's Python

run.sh runs in a fresh debian:trixie-slim container (Debian 13; Ubuntu 23.04+ behaves the same), installs the distro python3-pip, tries to install PyTorch system-wide, then installs it into a virtual environment:

#!/bin/sh
# Debian 13 (trixie), distro Python: install PyTorch
apt-get update -qq >/dev/null && apt-get install -y -qq python3-pip python3-venv >/dev/null 2>&1
python3 --version; pip --version | cut -d' ' -f1-2
echo '== 1. pip install torch'
pip install torch 2>&1 | head -4; echo "exit status: $(pip install torch >/dev/null 2>&1; echo $?)"
echo '== 2. sudo-less --user does not help'
pip install --user torch 2>&1 | head -1
echo '== 3. what apt offers'
apt-cache policy python3-torch | head -3
echo '== 4. venv + CPU-only wheel (dry run)'
python3 -m venv /opt/torch
/opt/torch/bin/pip install --dry-run torch --index-url https://download.pytorch.org/whl/cpu 2>&1 | grep '^Would install' | fold -s -w 100
echo '== 5. venv + default PyPI wheel (dry run)'
/opt/torch/bin/pip install --dry-run torch 2>&1 | grep '^Would install' | fold -s -w 100
echo '== 6. real install of the CPU wheel, then import'
/opt/torch/bin/pip install -q torch --index-url https://download.pytorch.org/whl/cpu 2>&1 | tail -1
/opt/torch/bin/python -c "import torch; print(torch.__version__, torch.tensor([1., 2.]).sum().item())"
du -sh /opt/torch | cut -f1

Output:

Python 3.13.5
pip 25.1.1
== 1. pip install torch
error: externally-managed-environment

× This environment is externally managed
╰─> To install Python packages system-wide, try apt install
exit status: 1
== 2. sudo-less --user does not help
error: externally-managed-environment
== 3. what apt offers
python3-torch:
  Installed: (none)
  Candidate: 2.6.0+dfsg-7
== 4. venv + CPU-only wheel (dry run)
Would install Jinja2-3.1.6 MarkupSafe-3.0.3 filelock-3.32.3 fsspec-2026.7.0 mpmath-1.3.0 
networkx-3.6.1 setuptools-78.1.0 sympy-1.14.0 torch-2.14.1+cpu typing_extensions-4.16.0
== 5. venv + default PyPI wheel (dry run)
Would install Jinja2-3.1.6 MarkupSafe-3.0.4 cuda-bindings-13.4.4 cuda-pathfinder-1.8.4 
cuda-toolkit-13.0.3.0 filelock-4.2.4 fsspec-2026.9.0 mpmath-1.3.0 networkx-3.7 
nvidia-cublas-13.1.1.3 nvidia-cuda-cupti-13.0.85 nvidia-cuda-nvrtc-13.0.88 
nvidia-cuda-runtime-13.0.96 nvidia-cudnn-cu13-9.24.0.43 nvidia-cufft-12.0.0.61 
nvidia-cufile-1.15.1.6 nvidia-curand-10.4.0.35 nvidia-cusolver-12.0.4.66 nvidia-cusparse-12.6.3.3 
nvidia-cusparselt-cu13-0.8.1 nvidia-nccl-cu13-2.30.7 nvidia-nvjitlink-13.4.92 
nvidia-nvshmem-cu13-3.4.5 nvidia-nvtx-13.0.85 setuptools-84.2.1 sympy-1.14.0 torch-2.14.1 
triton-3.8.0 typing_extensions-4.16.0
== 6. real install of the CPU wheel, then import
/opt/torch/lib/python3.13/site-packages/torch/_subclasses/functional_tensor.py:368: UserWarning: Failed to initialize NumPy: No module named 'numpy' (Triggered internally at /__w/pytorch/pytorch/torch/csrc/utils/tensor_numpy.cpp:84.)
  cpu = _conversion_method_template(device=torch.device("cpu"))
2.14.1+cpu 3.0
899M

2. What it means

Debian marks its Python as externally managed (PEP 668), so pip refuses to install anything into it. pip install torch and pip install --user torch both stop with exit status 1 before a byte of PyTorch is downloaded. Nothing is wrong with torch; every package fails the same way (see pip "externally-managed-environment").

3. Fix: a venv, and pick the right wheel

Related: pip install torch: No matching distribution found, No module named 'torch', Torch not compiled with CUDA enabled, externally-managed-environment in Docker.

Frequently asked questions

Why does pip install torch say externally-managed-environment?

Debian and Ubuntu mark the system Python as externally managed (PEP 668), so pip refuses to install any package into it, including with --user.

How do I install PyTorch on Debian or Ubuntu?

python3 -m venv ~/torch-env, then ~/torch-env/bin/pip install torch (add --index-url https://download.pytorch.org/whl/cpu for a CPU-only build).

Can I use apt install python3-torch?

Yes, it needs no pip, but Debian 13 packages torch 2.6.0, while PyPI has 2.14.1.

Written with AI assistance. Measured on 2026-10-24 in a debian:trixie-slim container (Python 3.13.5, distro pip 25.1.1; torch 2.14.1+cpu from the PyTorch CPU index). The script ran verbatim and the output was pasted unedited. PlainTables is our extension; PyTorch is free.