How to Build PyTorch from Source Using uv on Ubuntu 24.04 (February 2026)
Prerequisites
- x86_64
- uv
- Ubuntu 24.04 LTS
- CUDA toolkit + cuDNN (if you want to build with CUDA support)
Install dependencies
sudo apt update
sudo apt install -y \
build-essential \
git \
ccache \
curl \
pkg-config \
libjpeg-dev \
libpng-dev \
libopenblas-dev \
libomp-dev
Create venv, clone source from GitHub
You can use any python interpreter by changing --python 3.12.0 part.
mkdir -p ~/src/pytorch-build
cd ~/src/pytorch-build
uv venv --python 3.12.0 .venv
source .venv/bin/activate
git clone https://github.com/pytorch/pytorch --recursive
Install python dependencies
cd pytorch
uv pip install setuptools wheel ninja cmake
uv pip install --group dev
uv pip install mkl-static mkl-include # if you need Intel MKL
Let CMake search packages in the virtual environment
This ensures CMake finds Python and dependencies inside the virtual environment.
export CMAKE_PREFIX_PATH="${VIRTUAL_ENV}${CMAKE_PREFIX_PATH:+:$CMAKE_PREFIX_PATH}"
Enable ccache
export USE_CCACHE=1
Set max parallel jobs to avoid system overload
CUDA kernel build takes too much memory so I recommend to set 16 parallel jobs if system memory is 64GB.
jobs=$(( $(nproc) - 1 ))
export MAX_JOBS="$jobs"
export CMAKE_BUILD_PARALLEL_LEVEL="$jobs"
(Optional) Disable CUDA support
CUDA support is enabled by default but to disable it, set USE_CUDA=0.
export USE_CUDA=0
Build and install editable
uv pip install --no-build-isolation --verbose --editable .
Verify that successfully installed
python -c "import torch; print(torch.__version__); print(torch.cuda.is_available())"
Output will be something like this:
2.12.0a0+git125edef
True