Build PyTorch from source with NVIDIA GPU support

April 27, 2025

Latest the official guides: https://github.com/pytorch/pytorch?tab=readme-ov-file#from-source.

dependencies
git clone [email protected]:pytorch/pytorch.git
cd pytorch
git submodule sync
git submodule update --init --recursive
conda create -n storch python=3.13
conda activate storch
# Install CUDA, including CUDA runtime, compiler and cuDNN

After resolving the software dependencies, build PyTorch 1:

CMAKE_PREFIX_PATH="${CONDA_PREFIX:-'$(dirname $(which conda))/../'}:${CMAKE_PREFIX_PATH}" \
CMAKE_EXPORT_COMPILE_COMMANDS=ON \
CMAKE_BUILD_PARALLEL_LEVEL=32 \
USE_CUDA=1 TORCH_CUDA_ARCH_LIST="8.0" USE_FBGEMM=0 \
python setup.py develop 2>&1 | tee /tmp/torch_build.log

Then validate the installation with:

python3 -c "import torch; print(torch.__file__); print(torch.cuda.is_available())"

Footnotes

  1. FBGEMM fails to compile under GCC 12.2 in Debian 12, see: https://github.com/pytorch/pytorch/issues/77939#issuecomment-1528168416, we sidestep this by disabling FBGEMM integration in PyTorch.