How to Install PyTorch on a VPS
PyTorch is a popular deep learning framework known for its flexibility and Pythonic API. It runs well on CPU-based Breezes for inference and smaller training jobs.
Prerequisites
- A Breeze with at least 4 GB RAM
- Python 3.9 or newer
Set Up a Virtual Environment
sudo apt update && sudo apt install python3-venv python3-pip -y
python3 -m venv ~/pytorch-env
source ~/pytorch-env/bin/activate
pip install --upgrade pip
Install PyTorch (CPU Only)
For CPU-only Breezes, install the lightweight build:
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu
Verify Installation
python3 -c "import torch; print(torch.__version__); print('CUDA:', torch.cuda.is_available())"
Install Additional Libraries
pip install numpy transformers datasets accelerate
Run a Quick Test
import torch
x = torch.rand(5, 3)
print(x)
model = torch.nn.Linear(3, 1)
output = model(x)
print(output)
Performance Tips
Set the number of threads to your CPU core count:
torch.set_num_threads(4)
Use torch.no_grad() during inference to reduce memory usage. For production serving, consider TorchServe or exporting models to ONNX format.