How to Run Ollama on a VPS: Self-Host Llama 3 in 2026
Running a local LLM on your own VPS gives you a private, cost-predictable AI API. No per-token pricing, no data leaving your infrastructure. The catch: you need enough RAM, and the setup requires a few extra steps compared to a cloud API.
This guide covers the full setup: installing Ollama, pulling a model, keeping it running with systemd, and exposing a secure API through Nginx.
For VPS recommendations and RAM requirements by model size, see Best VPS for Ollama in 2026.
Requirements
| What | Minimum | Recommended |
|---|---|---|
| RAM | 8GB | 16GB |
| Storage | 20GB free | 40GB+ |
| CPU | 2 vCPU | 4+ vCPU |
| OS | Ubuntu 22.04 | Ubuntu 22.04 |
Good options: Hetzner CX32 (€8.30/mo, 8GB RAM — see the full CX32 pricing breakdown) or Contabo Cloud VPS S ($7/mo, 8GB RAM). A 4GB VPS will fail to load most useful models.
Step 1: Install Ollama
curl -fsSL https://ollama.com/install.sh | sh
This installs the ollama binary and creates a systemd service automatically. Verify:
ollama --version
systemctl status ollama
Step 2: Pull a Model
# Llama 3.1 8B — best balance of quality and speed on CPU
ollama pull llama3.1
# Mistral 7B — fast, good for code and structured output
ollama pull mistral
# Phi-3 Mini — runs on 4GB RAM, fast but less capable
ollama pull phi3
Model download sizes:
llama3.1(8B) — ~4.7GBmistral(7B) — ~4.1GBphi3(3.8B) — ~2.3GB
Models are stored in ~/.ollama/models. Make sure your VPS has enough disk space before pulling.
Step 3: Test Locally
# Chat in terminal
ollama run llama3.1
# Or call the REST API directly
curl http://localhost:11434/api/generate -d '{
"model": "llama3.1",
"prompt": "What is the capital of France?",
"stream": false
}'
The API is OpenAI-compatible for /api/chat calls, making it easy to swap with existing integrations.
Step 4: Keep Ollama Running (systemd)
Ollama’s installer creates a systemd service automatically, but verify it’s set to start on reboot:
sudo systemctl enable ollama
sudo systemctl start ollama
sudo systemctl status ollama
If the service isn’t created (older installs), create it manually:
sudo nano /etc/systemd/system/ollama.service
[Unit]
Description=Ollama Service
After=network-online.target
[Service]
ExecStart=/usr/local/bin/ollama serve
User=ollama
Group=ollama
Restart=always
RestartSec=3
Environment="OLLAMA_HOST=127.0.0.1:11434"
[Install]
WantedBy=default.target
sudo systemctl daemon-reload
sudo systemctl enable --now ollama
The OLLAMA_HOST=127.0.0.1 binding is important — it keeps Ollama on localhost only. Nginx handles external access in the next step.
Step 5: Nginx Reverse Proxy with API Key Auth
Never expose port 11434 directly to the internet. Use Nginx as a gatekeeper.
sudo apt install nginx -y
sudo nano /etc/nginx/sites-available/ollama
server {
listen 443 ssl;
server_name your-domain.com;
ssl_certificate /etc/letsencrypt/live/your-domain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/your-domain.com/privkey.pem;
# Simple API key check via header
if ($http_x_api_key != "your-secret-key-here") {
return 401;
}
location / {
proxy_pass http://127.0.0.1:11434;
proxy_set_header Host $host;
proxy_read_timeout 300s; # LLM responses can be slow
proxy_send_timeout 300s;
}
}
sudo ln -s /etc/nginx/sites-available/ollama /etc/nginx/sites-enabled/
sudo certbot --nginx -d your-domain.com
sudo nginx -t && sudo systemctl reload nginx
Now call your API with the key:
curl https://your-domain.com/api/generate \
-H "X-API-Key: your-secret-key-here" \
-d '{"model": "llama3.1", "prompt": "Hello", "stream": false}'
Step 6: Use with n8n or Custom Scripts
Ollama’s API is compatible with the OpenAI SDK. In Node.js:
import OpenAI from 'openai';
const client = new OpenAI({
baseURL: 'https://your-domain.com/v1',
apiKey: 'your-secret-key-here',
});
const response = await client.chat.completions.create({
model: 'llama3.1',
messages: [{ role: 'user', content: 'Explain PM2 cluster mode' }],
});
For n8n self-hosted workflows, use the Ollama node or the HTTP Request node pointing to your Nginx endpoint.
Performance Tips
-
Preload models at startup to avoid cold-start latency:
# Add to /etc/systemd/system/ollama.service [Service] section: ExecStartPost=/bin/sh -c 'sleep 5 && ollama run llama3.1 ""' -
Limit context length for faster responses on CPU:
ollama run llama3.1 --num-ctx 2048 -
Monitor RAM usage — if the VPS starts swapping heavily, the model is too large. Switch to
phi3or upgrade to 16GB RAM.
What’s Next
Self-hosted Ollama pairs well with n8n for AI automation workflows. See our n8n on VPS setup guide for the full n8n + Ollama stack on a single VPS.
For VPS provider comparisons at the 8GB RAM tier (what Ollama needs), see Best VPS for Ollama.