12. Ollama as Systemd Service

Chapter 12 of 20 · 20 min

Running Ollama as a systemd service ensures it starts on boot, restarts after failures, and runs under a dedicated user account. This is the recommended approach for server deployments.

Default Service File

On Linux, the install script creates /etc/systemd/system/ollama.service:

[Unit]
Description=Ollama Service
After=network-online.target
Wants=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

The service runs as the ollama user, limiting damage if the process is compromised.

Managing the Service

# Start the service
sudo systemctl start ollama

# Stop the service
sudo systemctl stop ollama

# Restart (reload after config changes)
sudo systemctl restart ollama

# Check status
sudo systemctl status ollama

# Enable on boot
sudo systemctl enable ollama

Customizing with Environment Variables

Create an override file to customize the service:

sudo systemctl edit ollama.service

Add custom environment variables:

[Service]
Environment="OLLAMA_HOST=0.0.0.0:11434"
Environment="OLLAMA_MODELS=/data/ollama/models"
Environment="OLLAMA_NUM_PARALLEL=4"

After editing, reload and restart:

sudo systemctl daemon-reload
sudo systemctl restart ollama

Changing the Model Directory

By default, models are stored in ~/.ollama/models. To use a different location:

sudo systemctl edit ollama.service
[Service]
Environment="OLLAMA_MODELS=/mnt/storage/ollama-models"

Create the directory and set permissions:

sudo mkdir -p /mnt/storage/ollama-models
sudo chown ollama:ollama /mnt/storage/ollama-models
sudo systemctl restart ollama

Viewing Logs

# Recent logs
sudo journalctl -u ollama -n 50

# Follow logs in real-time
sudo journalctl -u ollama -f

# Logs since last boot
sudo journalctl -u ollama -b

Logs show startup messages, request logs, and errors. If the service fails to start, logs usually indicate the cause.

Failure Modes

Common systemd issues:

  • Service starts but fails health check - Check journalctl -u ollama for errors. Often caused by missing GPU drivers.
  • Port already in use - Another process (like a manual ollama serve) is using port 11434. Find and stop it with lsof -i :11434.
  • Permission denied on model directory - The ollama user cannot read the models directory. Check ownership with ls -la.
EXERCISE

Check the current systemd service configuration with systemctl cat ollama, then create an override that changes OLLAMA_HOST to bind on all interfaces.