Sub topic 16
Configuring the server: host, keep-alive, and storage location
This chapter assumes you can run ollama pull and ollama run, and that you have called the REST API with curl or Python. It also assumes you can set environment variables in your operating system. You will change three server settings: which address the server listens on, how long a model stays in memory, and where model files are stored.
How Ollama reads its settings
The Ollama server is a background process, and it reads its settings from environment variables when it starts. Changing a variable in your current shell does nothing unless that shell starts the server. If the server runs as a service, the variable must be set in the service's configuration, and the service must be restarted.
| Variable | Default | Controls |
|---|---|---|
| `OLLAMA_HOST` | `127.0.0.1:11434` | Address and port the server listens on |
| `OLLAMA_KEEP_ALIVE` | `5m` | How long an idle model stays loaded |
| `OLLAMA_MODELS` | Per-user `.ollama/models` folder | Where model files are stored |
Setting variables for your platform
How you set a variable depends on how the server is started.
Linux with systemd
The Linux installer registers a service named ollama. Create an override file with systemctl edit:
sudo systemctl edit ollama.serviceIn the editor, add a [Service] section with one Environment line per variable:
[Service]
Environment="OLLAMA_HOST=0.0.0.0:11434"
Environment="OLLAMA_KEEP_ALIVE=30m"Then reload and restart:
sudo systemctl daemon-reload
sudo systemctl restart ollama
systemctl show ollama --property=EnvironmentThe last command prints the environment systemd gives the service, so you can confirm your values were picked up.
macOS
The menu bar app starts the server. Set the variable with launchctl, then quit and reopen the app:
launchctl setenv OLLAMA_KEEP_ALIVE "30m"Windows
Quit Ollama from the tray icon. Open the environment variable settings for your user account, add or edit the variable, and start Ollama again.
Running the server by hand
If you start the server yourself with ollama serve, set the variables on the same command line:
OLLAMA_HOST=0.0.0.0:11434 OLLAMA_KEEP_ALIVE=1h ollama serveHost: choosing who can connect
By default the server binds to 127.0.0.1, the loopback address. Only programs on the same machine can reach it. Setting OLLAMA_HOST=0.0.0.0:11434 makes it listen on every network interface, so other machines can connect. You can also bind to one specific interface address, such as 192.168.1.20:11434, or change the port.
OLLAMA_HOST also tells the CLI where to find the server. The CLI is a client of the same HTTP API, so you can point it at a remote machine:
OLLAMA_HOST=192.168.1.20:11434 ollama listIf you change the port on the server, set the same value in every shell or script that uses the CLI. Otherwise the CLI keeps looking at port 11434 and reports that it cannot connect.
Browser-based tools that call the API from another origin are blocked unless that origin is allowed. OLLAMA_ORIGINS takes a comma-separated list of allowed origins, for example OLLAMA_ORIGINS=http://localhost:3000.
Keep-alive: balancing latency against memory
Loading a model from disk into RAM or GPU memory can take seconds to minutes for large models. To avoid paying that cost on every request, Ollama keeps a model loaded after it answers. It unloads the model after OLLAMA_KEEP_ALIVE of idle time, which is five minutes by default. You can check what is loaded with ollama ps. Its UNTIL column shows when each model will be unloaded.
Accepted values are:
- A duration string such as
10mor24h. - A number of seconds, such as
3600. 0, which unloads the model as soon as the response finishes.- Any negative number, such as
-1, which keeps the model loaded indefinitely.
A longer keep-alive gives faster replies after idle periods but holds memory that other programs cannot use. A shorter one frees memory sooner but makes the next request slower.
Overriding keep-alive per request
The keep_alive field in an API request overrides the server default for that model. A request with a model name and no prompt loads the model without generating anything:
curl http://localhost:11434/api/generate -d '{
"model": "llama3.2",
"keep_alive": -1
}'Sending "keep_alive": 0 the same way unloads it. The same field works in /api/chat:
import requests
resp = requests.post(
"http://localhost:11434/api/chat",
json={
"model": "llama3.2",
"messages": [{"role": "user", "content": "Say hello."}],
"stream": False,
"keep_alive": "30m",
},
timeout=120,
)
resp.raise_for_status()
print(resp.json()["message"]["content"])The last request to touch a model sets its timer. If one script sends keep_alive: 0 and another expects the model to stay warm, they will interfere with each other.
You can also unload a model from the CLI:
ollama stop llama3.2Storage location
Model files are large, often several gigabytes each, so the default location may not suit your disk layout. The default depends on how Ollama was installed:
| Platform | Default model directory |
|---|---|
| macOS | `~/.ollama/models` |
| Linux (systemd service) | `/usr/share/ollama/.ollama/models` |
| Windows | `C:\Users\<name>\.ollama\models` |
Set OLLAMA_MODELS to a directory on a larger or faster drive to change it. On Linux, the service runs as the ollama user, so that user must be able to read and write the new directory:
sudo mkdir -p /data/ollama-models
sudo chown -R ollama:ollama /data/ollama-models
sudo systemctl edit ollama.serviceAdd this to the override:
[Service]
Environment="OLLAMA_MODELS=/data/ollama-models"Then run sudo systemctl daemon-reload and sudo systemctl restart ollama.
Changing the variable does not move existing models. The server looks in the new directory, finds nothing, and ollama list comes back empty. Either pull the models again or copy the old directory's contents to the new one while the server is stopped.
Common errors
ollama listis empty after a restart. The server is using a differentOLLAMA_MODELSthan before, or you are talking to a different server. Check where each one stores its files.connection refusedfrom another machine. The server is still bound to127.0.0.1, or a firewall blocks the port.permission deniedwhile pulling after changing the storage path. On Linux, theollamauser does not own the new directory.- Variable set but no effect. You exported it in a shell, but the server runs as a service that does not inherit your shell's environment.
- Memory stays full. A model with
keep_alive: -1was loaded and never unloaded. Runollama ps, thenollama stop <model>.
Exercise
You run export OLLAMA_MODELS=/data/models in your terminal on a Linux machine where Ollama runs as a systemd service, then run ollama pull llama3.2. The model still lands in the old location. Why, and what should you do?
Show answer
The pull is performed by the server, not by the CLI, and the systemd service does not inherit your shell's environment. Set OLLAMA_MODELS in a service override with sudo systemctl edit ollama.service, using an Environment="OLLAMA_MODELS=/data/models" line under [Service]. Make sure the ollama user owns /data/models, then run sudo systemctl daemon-reload and sudo systemctl restart ollama.
Exercise
A script calls the /v1 OpenAI-compatible endpoint, and the first request each morning is slow because the model has been unloaded overnight. Name two ways to keep the model loaded.
Show answer
Set OLLAMA_KEEP_ALIVE to a long duration (for example 24h) or -1 in the server's environment and restart the server. Or preload the model with a native API request, such as a /api/generate call containing only "model" and "keep_alive": -1, before the script runs. The /v1 endpoint cannot set keep-alive per request.