How to set Ollama environment variables
Ollama installed, system administrator or root access, and familiarity with shell environment configuration.
What this does
Environment variables control Ollama's runtime behavior, including model storage paths, GPU usage, logging verbosity, and API binding. This guide configures common variables persistently so they survive service restarts and reboots.
Steps
Identify the relevant environment variable. Common variables include
OLLAMA_HOST,OLLAMA_MODELS,OLLAMA_NUM_PARALLEL,OLLAMA_MAX_LOADED_MODELS, andOLLAMA_LOG_LEVEL.Create a systemd drop-in override for persistent service configuration on Linux. This ensures variables persist across reboots and service restarts.
sudo systemctl edit ollamaExpected output: An editor opens with an empty override file.
Add the desired environment variables inside the override block. This example sets a custom model directory, parallel request limit, and log level.
[Service] Environment="OLLAMA_MODELS=/mnt/storage/ollama-models" Environment="OLLAMA_NUM_PARALLEL=2" Environment="OLLAMA_LOG_LEVEL=info"Save and exit the editor.
Reload the systemd configuration and restart the service.
sudo systemctl daemon-reload sudo systemctl restart ollamaExpected output: No error output; service restarts with the new variables applied.
On macOS, use a launchd environment configuration file instead.
sudo launchctl config system OLLAMA_MODELS /Volumes/External/ollama-models sudo launchctl kickstart -k ai.ollama.ollamaExpected output: Configuration applied; service restarts.
Verification
systemctl show ollama --property Environment
# Expected: Environment="OLLAMA_MODELS=/mnt/storage/ollama-models" "OLLAMA_NUM_PARALLEL=2" "OLLAMA_LOG_LEVEL=info"
Common failures
- Variables not applied after restart — Override file syntax error. Run
systemctl cat ollamato inspect actual loaded configuration. - Model storage path change breaks existing models — Changing
OLLAMA_MODELSdoes not migrate files. Move files manually and update the variable to point to the new location. - Too many parallel requests causes GPU OOM — Reduce
OLLAMA_NUM_PARALLELorOLLAMA_MAX_LOADED_MODELSto lower memory pressure. daemon-reloadfails on WSL2 — WSL2 does not support full systemd. Usesudo service ollama restartinstead on WSL2.- Wrong environment on Windows — On Windows, set variables via System Properties > Environment Variables or PowerShell
[Environment]::SetEnvironmentVariable(...).