PyTorch and TensorFlow are the two dominant machine learning frameworks, used for building, training, and running neural network models. This guide covers installing both (or either) on a CPU-only VPS.
Choosing Between PyTorch and TensorFlow
Both are capable, well-maintained frameworks — PyTorch is generally favored in research and has become the more common choice for many newer open-source models; TensorFlow remains widely used, particularly in some production deployment contexts. If you don't have a specific reason to prefer one, check which framework your specific model/project actually requires.
Step 1 — Set Up a Python Virtual Environment
python3 -m venv ml-env
source ml-env/bin/activate
Using a dedicated virtual environment avoids dependency conflicts with other Python projects on the same server — see How to Install Python on Ubuntu & Debian for the base setup.
Step 2 — Install PyTorch (CPU Version)
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu
The CPU-specific index avoids downloading unnecessary GPU-related components, resulting in a smaller install.
Step 3 — Verify PyTorch Installation
python3 -c "import torch; print(torch.__version__); print(torch.cuda.is_available())"
cuda.is_available() correctly returns False on a CPU-only server — this is expected, not an error.
Step 4 — Install TensorFlow (CPU Version)
pip install tensorflow-cpu
Step 5 — Verify TensorFlow Installation
python3 -c "import tensorflow as tf; print(tf.__version__)"
Running a Simple Test Script (PyTorch)
import torch
x = torch.rand(3, 3)
print(x)
print(x @ x.T) # basic matrix operation
Installing Additional Common Libraries
pip install numpy pandas scikit-learn matplotlib
Managing Memory for Larger Models
CPU-based deep learning can be memory-intensive — monitor usage with htop and consider setting explicit batch size limits in your code to avoid out-of-memory errors on smaller VPS plans.
Using a requirements.txt for Reproducibility
pip freeze > requirements.txt
pip install -r requirements.txt
Ensures consistent package versions if you redeploy this environment on another server later.
Common Errors
"Killed" message with no other error during training — almost always an out-of-memory condition where the OOM killer terminated the process; reduce batch size or model complexity, or check available RAM with free -h.
Installation is extremely slow — some ML package installations involve large downloads; ensure adequate disk space and a stable connection, and be patient with the initial install.
Continue Reading
- VPS Requirements for Running AI and Machine Learning Workloads
- How to Serve a Machine Learning Model with FastAPI
- How to Install Python on Ubuntu & Debian
Browse more articles in AI & Machine Learning on a VPS.