Updated: 28 September 2026 · Applies to: PyTorch 2.14 (CUDA 13 build) on Ubuntu 24.04 / 26.04 LTS
PyTorch is the most common framework for training and running AI models. Its pip packages contain the CUDA libraries they need, so a GPU server only needs the NVIDIA driver, not the CUDA Toolkit. This guide installs PyTorch in a virtual environment and proves that it can use the GPU.
Before you start
nvidia-smiprints a table (How to install the NVIDIA driver on Ubuntu 24.04 / 26.04 for a GPU server?).- The default PyTorch package for Linux is built for CUDA 13, which needs driver 580 or newer. With an older driver use the CUDA 12.6 package in step 4.
- PyTorch needs Python 3.10 or newer. Ubuntu 24.04 and 26.04 include a suitable version.
Install PyTorch
- Install the Python tools:
sudo apt update sudo apt install -y python3-venv python3-pip
- Create and activate a virtual environment. Ubuntu 24.04 and newer refuse
pip installoutside a virtual environment ("externally-managed-environment"), so this step is required:python3 -m venv ~/torch-env source ~/torch-env/bin/activate
- Install PyTorch (the download is large, several GB):
pip install torch
- Only if your driver is older than 580: install the CUDA 12.6 build instead:
pip install torch --index-url https://download.pytorch.org/whl/cu126
Test the GPU
python3 -c "import torch; print(torch.__version__); print('GPU available:', torch.cuda.is_available()); print('GPU count:', torch.cuda.device_count()); print(torch.cuda.get_device_name(0))"
You should see GPU available: True and your GPU's name. For a real workload test, run a large matrix multiplication on the GPU while you watch nvidia-smi in a second SSH window:
python3 -c "import torch; a=torch.randn(8192,8192,device='cuda'); b=torch.randn(8192,8192,device='cuda'); [a@b for _ in range(50)]; torch.cuda.synchronize(); print('done')"
If GPU available is False
- Run
nvidia-smi. If it fails, fix the driver first: How to fix the "NVIDIA-SMI has failed" error (cannot communicate with the NVIDIA driver)?. - Check the driver version in the
nvidia-smiheader. Below 580, reinstall PyTorch with the CUDA 12.6 command above, or update the driver (How to install the NVIDIA driver on Ubuntu 24.04 / 26.04 for a GPU server?). - Inside Docker, start the container with
--gpus all(How to use your NVIDIA GPU in Docker with the NVIDIA Container Toolkit?). - You installed a "cpu" build: run
pip uninstall -y torchand install again with one of the commands above.
Frequently asked questions
Do I need to install CUDA first?
No. The driver is enough.
How do I use only one of my GPUs?
See How to choose which GPU an application uses on a multi-GPU server?.
Which PyTorch version does this guide use?
PyTorch 2.14, the current release when this article was written.
Official documentation: PyTorch: Get started locally.
Need a GPU server, or a hand with the setup?
- GPU dedicated servers: NVIDIA GPU servers for AI training and inference; our engineers can install the driver, CUDA, PyTorch or a private LLM and hand it over ready to use.
- Private LLM installation: we install Ollama, the GPU driver and a chat interface on your own server.
Prefer a hand with the setup? Our engineers can do it for you: Hire an Expert, or use our on-demand server management.
