Home How ToSelf-Host an LLM with Ollama on Ubuntu Server: A Practical Guide

Self-Host an LLM with Ollama on Ubuntu Server: A Practical Guide

by Knowledge Base Linux
Self-Host an LLM with Ollama on — Self-Host an LLM with Ollama on Ubuntu Server: A Practical Guide

Running a large language model on your own server sounds like a job for a research lab, but it is now genuinely practical. If you want to self host LLM workloads with Ollama, you can turn a plain Ubuntu server into a private AI endpoint in under twenty minutes: no API keys, no per-token billing, and no data ever leaving your infrastructure. This guide walks through the full setup — installation, model selection, API usage, systemd autostart, and basic hardening. This article covers Self-Host an LLM with Ollama on in detail.

ollama local LLM api endpoint diagram for self hosting

Why Self Host an LLM with Ollama – Self-Host an LLM with Ollama on

Ollama packages open-weight models (Llama 3, Mistral, Qwen, DeepSeek) into a single server process with a clean CLI and an OpenAI-compatible REST API. For a sysadmin, the use cases are immediate:

  • Privacy: logs, code, and tickets stay on your hardware — ideal for internal tools and customer data.
  • Cost: after the server cost, inference is effectively free, no per-token fees.
  • Control: you choose the model, the version, and when it updates.
  • Offline resilience: once models are downloaded, no internet connection is required.

The main cost is hardware: roughly 1 GB of RAM per billion parameters for quantized models. An 8 GB RAM server comfortably runs an 8B model; a 3B model runs fine on 4 GB.

Prerequisites – Self-Host an LLM with Ollama on

  • Ubuntu 22.04 or 24.04 server (bare metal, VPS, or VM).
  • At least 4 GB RAM (8 GB recommended), 20 GB free disk space, SSD strongly recommended.
  • Root or sudo access. If you are connecting remotely, see our guide on how to SSH into a Linux server.
  • Optional: an NVIDIA GPU with CUDA drivers for much faster inference.

Step 1: Install Ollama with the Official Install Script

Ollama’s official installer sets up the binary and registers a systemd service in one command. Always review a remote install script before piping it to a shell on production machines.

# Inspect the script first (good practice on servers)
curl -fsSL https://ollama.com/install.sh -o /tmp/ollama-install.sh
less /tmp/ollama-install.sh

# Then run it
sh /tmp/ollama-install.sh

Verify the binary is installed and check the service status:

ollama --version
systemctl status ollama

The installer creates the ollama systemd service and starts it automatically. The API should already be listening:

curl http://localhost:11434/api/tags
# {"models":[]} — server is running, no models downloaded yet

Step 2: Pull Your First Model

Models are versioned like Docker images. Start with a small, fast model to validate the pipeline, then add larger ones as needed:

# Fast 3B model — good for testing on 4 GB RAM
ollama pull llama3.2:3b

# Stronger general-purpose 8B model (needs ~8 GB RAM)
ollama pull llama3.1:8b

# Code-focused model for scripts and DevOps work
ollama pull qwen2.5-coder:7b

# See what is installed
ollama list

Model files are cached under ~/.ollama/models (for the service user, /usr/share/ollama/.ollama/models). An 8B quantized model is roughly 4.7 GB on disk.

Step 3: Chat from the Command Line

ollama run pulls the model if needed and opens an interactive chat:

ollama run llama3.1:8b
>>> Summarize the last 3 lines of /var/log/auth.log in plain language
>>> /bye

Special commands inside the chat include /set, /show info, and /bye. This is a quick way to sanity-check a model before wiring it into automation.

Step 4: Use the REST API and the OpenAI-Compatible Endpoint

Ollama exposes two APIs on port 11434. The native endpoint is simple:

# One-shot generation (non-streaming)
curl -s http://localhost:11434/api/generate -d '{
  "model": "llama3.1:8b",
  "prompt": "Write a bash one-liner that lists the 5 largest directories under /var",
  "stream": false
}' | jq -r '.response'

# Chat-style request
curl -s http://localhost:11434/api/chat -d '{
  "model": "llama3.1:8b",
  "messages": [{"role": "user", "content": "Explain inode exhaustion in one sentence"}],
  "stream": false
}' | jq -r '.message.content'

Anything written against the OpenAI API works against http://localhost:11434/v1 with any placeholder API key:

curl -s http://localhost:11434/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{
    "model": "llama3.1:8b",
    "messages": [{"role": "user", "content": "Give me a secure nginx SSL snippet"}]
  }' | jq -r '.choices[0].message.content'

Step 5: Make Ollama Start on Boot and Tune It

The installer already enables the service, but confirm and set useful environment variables via a systemd override:

sudo systemctl enable ollama
sudo systemctl is-enabled ollama

To customize, create an override file:

sudo systemctl edit ollama

Add (then save and exit):

[Service]
# Keep model weights loaded in memory for 30 minutes
Environment="OLLAMA_KEEP_ALIVE=30m"
# Load multiple models side by side (RAM permitting)
Environment="OLLAMA_MAX_LOADED_MODELS=2"

Apply:

sudo systemctl daemon-reload
sudo systemctl restart ollama
journalctl -u ollama -f

By default Ollama binds only to 127.0.0.1. Only set OLLAMA_HOST=0.0.0.0 if you truly need remote access — and if you do, protect it behind a firewall and an authenticated reverse proxy (see our UFW firewall setup guide). There is no built-in authentication on the API.

Step 6: Create a Custom Model with a Modelfile

A Modelfile bakes a system prompt and parameters into a reusable model — perfect for a “sysadmin assistant” personality:

cat > /tmp/sysops.Modelfile <<'EOF'
FROM llama3.1:8b
SYSTEM You are a senior Linux sysadmin. Answer concisely, give exact commands in code blocks, and warn about destructive operations.
PARAMETER temperature 0.2
EOF

ollama create sysops -f /tmp/sysops.Modelfile
ollama run sysops

Troubleshooting

"Error: could not connect to ollama server" or curl connection refused

The service is not running. Check and restart it:

systemctl status ollama
sudo systemctl restart ollama
# If the service file is missing, recreate it per the official docs at https://ollama.com
# and run: sudo systemctl daemon-reload && sudo systemctl enable --now ollama

Model downloads are slow or fail with a 403/500

Large models download over a single HTTPS connection to Ollama's registry. Retry the pull, and check that no egress firewall or proxy blocks ollama.com:

ollama pull llama3.1:8b   # resumable — safe to re-run
curl -sI https://ollama.com | head -1

Out-of-memory: the model is killed or the system swaps heavily

You chose a model larger than your RAM. Confirm with dmesg | grep -i "out of memory", then switch to a smaller quantized model (llama3.2:3b instead of an 8B, or qwen2.5:1.5b for very small boxes). Also lower OLLAMA_MAX_LOADED_MODELS to 1 and avoid num_ctx values above 4096 unless you have the RAM.

GPU is ignored (slow CPU-only inference despite NVIDIA card)

Ollama detects NVIDIA GPUs automatically, but only if the driver and CUDA libraries are present. Verify:

nvidia-smi
# If nvidia-smi is missing, install the NVIDIA driver for your Ubuntu release first.
# Then restart Ollama so it re-detects the GPU:
sudo systemctl restart ollama
journalctl -u ollama | grep -i -E "cuda|gpu"

Conclusion

To self host LLM inference with Ollama, you only need one install script, one ollama pull, and a systemd service that keeps the API alive across reboots. You now have a private, cost-free AI endpoint at localhost:11434 that speaks both Ollama's native API and the OpenAI-compatible /v1 interface — ready to power log summarizers, internal chatbots, or CI helpers without sending a single byte to a third party. Start with a small model, verify your RAM budget with real workloads, and only then scale up to larger weights or GPU acceleration.

You may also like

KbLinux

Typically replies within a day

Hello, Welcome to the site. Please click below button for chatting me through Telegram.