Create a Python 3.12 environment for PyTorch
Current stable PyTorch wants Python 3.10 or newer; 3.12 sits comfortably inside the supported range. Create and activate a dedicated env:
conda create -n torch python=3.12 -y
conda activate torch
PyTorch deserves its own environment even when you suspect you won't need one, because CUDA wheels are enormous, the dependency tree is opinionated and someday you'll want to delete the whole thing cleanly. If envs are new territory, the guide on how to create and manage Miniconda environments covers the workflow, and a bare server needs the install Miniconda on Ubuntu steps before any of this.
Install PyTorch with pip inside the conda env
With the env active, the CPU build is one line:
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu
A CUDA build swaps the index URL. The pattern is cu plus the CUDA version without its dot, so CUDA 12.6 becomes:
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu126
Pick the newest cu1xx index that doesn't exceed what nvidia-smi reported. Drivers are backward compatible, meaning a driver showing CUDA 12.8 runs cu126 wheels happily while the reverse pairing dies at runtime. You don't need the CUDA toolkit installed on the system for any of this, since the wheels bundle the runtime libraries they use; the driver is the only system-level requirement. Two smaller notes. The PyTorch get started selector generates the exact line for your OS and compute platform, and I'd trust it over any copy-pasted command in a tutorial, including this one, because the available CUDA indexes shift with each release. And torchaudio can be dropped when you know audio isn't coming; most people cargo-cult it along.
Verify the install and the GPU
python -c "import torch; print(torch.cuda.is_available())"
python -c "import torch; print(torch.cuda.get_device_name(0))"
On a CUDA install the first prints True and the second names your card. On a CPU install, False from the first is correct behavior, not a failure. The case worth investigating is False on a machine that has a GPU, and there the usual culprit is the CPU wheel having snuck in, since a plain pip install torch with no index URL pulls CPU-only on Linux. Check that before blaming the driver.
The conda-forge pytorch package as an alternative
For a conda-native install, conda-forge maintains community builds:
conda install -c conda-forge pytorch
They're solid builds, and the right answer when the rest of your stack already comes from conda-forge and one solver should manage everything. Otherwise I'd stay on pip: it's the route the PyTorch team supports and documents, releases land there first and you pick the CUDA variant explicitly instead of leaving it to solver negotiation. Where conda-forge sits in the larger channel picture belongs to the Miniconda vs Miniforge comparison.
Export the environment for reproducibility
Once the install works, capture it:
conda env export > environment.yml
The export includes a pip: section listing torch and friends, so this one file rebuilds the whole env elsewhere. One caveat specific to PyTorch envs: the --from-history export variant ignores pip-installed packages entirely, so here the full export is the one that works. I also keep the exact install line, index URL included, in the project README, since the yml records which torch you got but not which index it came from, and on a new machine with a different GPU that index is the first thing to change.