Sub topic 16
Configuring the server: host, keep-alive, and storage location
This chapter assumes you can run ollama serve, use the CLI, and call the REST API from the earlier chapters. You should also know how to set environment variables in your shell. The Ollama server reads its settings from environment variables when it starts. Changing a variable has no effect until you restart the server.
The settings that matter most
| Variable | Default | What it controls |
|---|---|---|
| `OLLAMA_HOST` | `127.0.0.1:11434` | Address and port the server listens on |
| `OLLAMA_KEEP_ALIVE` | `5m` | How long a model stays in memory after its last request |
| `OLLAMA_MODELS` | Per-user default folder | Where downloaded models are stored |
Host: who can reach the server
By default the server listens only on the loopback address, so only programs on your own machine can connect. To accept connections from other machines, bind to all interfaces.
OLLAMA_HOST=0.0.0.0:11434 ollama serveThe CLI also reads OLLAMA_HOST to decide which server to talk to. This lets you point your local client at a remote machine.
OLLAMA_HOST=192.168.1.50:11434 ollama listKeep-alive: how long a model stays loaded
Loading a model into memory takes seconds. Ollama keeps it loaded after a request so the next one is fast, then unloads it after the keep-alive period. You can check what is loaded with ollama ps.
OLLAMA_KEEP_ALIVE=30m ollama serveThe value accepts durations such as 30m or 1h. A negative number such as -1 keeps models loaded indefinitely, and 0 unloads a model right after each response. You can also override the server setting for one request with the keep_alive field.
curl http://localhost:11434/api/generate -d '{
"model": "llama3.2",
"prompt": "Hello",
"stream": false,
"keep_alive": "10m"
}'A longer keep-alive trades memory for speed. A shorter one frees RAM or GPU memory sooner for other programs.
Storage location
Models are large, often several gigabytes each. If your default drive is small, move the store with OLLAMA_MODELS.
mkdir -p /data/ollama-models
OLLAMA_MODELS=/data/ollama-models ollama serveThe account that runs the server needs read and write access to this folder. Models already downloaded stay in the old location. Copy them over or pull them again.
Setting variables for a background service
The variables above only apply to the process you start them in. If Ollama runs as a service, set them in the service's configuration instead.
- Linux with systemd: run
systemctl edit ollama.service, addEnvironment="OLLAMA_HOST=0.0.0.0"under a[Service]heading, then runsystemctl restart ollama. - macOS app: run
launchctl setenv OLLAMA_HOST "0.0.0.0", then quit and reopen the app. - Windows: add the variable in the user environment variables settings, then quit and reopen Ollama from the tray.
Common mistakes
- Setting a variable in one terminal, then expecting the background service to see it.
- Forgetting to restart the server after a change.
- Pointing
OLLAMA_MODELSat a folder the service account cannot write to, which makes pulls fail.
Exercise
You ran OLLAMA_KEEP_ALIVE=1h ollama serve in a terminal, but Ollama also runs as a systemd service on the same machine, and ollama ps shows models unloading after five minutes. Why, and what should you do?
Show answer
The variable applied only to the server process started in that terminal. The CLI is talking to the systemd service, which still uses the default of 5m. Add Environment="OLLAMA_KEEP_ALIVE=1h" with systemctl edit ollama.service and restart the service with systemctl restart ollama.