HOW-TO · SET

How to set Ollama environment variables

intermediate10 minBy Fredoline Eruo
Target environment
Ubuntu 24.04 · Ollama 0.4.xWindows 11 · Ollama 0.4.xmacOS 15 · Ollama 0.4.x
PREREQUISITES

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

  1. Identify the relevant environment variable. Common variables include OLLAMA_HOST, OLLAMA_MODELS, OLLAMA_NUM_PARALLEL, OLLAMA_MAX_LOADED_MODELS, and OLLAMA_LOG_LEVEL.

  2. Create a systemd drop-in override for persistent service configuration on Linux. This ensures variables persist across reboots and service restarts.

    sudo systemctl edit ollama
    

    Expected output: An editor opens with an empty override file.

  3. 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.

  4. Reload the systemd configuration and restart the service.

    sudo systemctl daemon-reload
    sudo systemctl restart ollama
    

    Expected output: No error output; service restarts with the new variables applied.

  5. 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.ollama
    

    Expected 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 ollama to inspect actual loaded configuration.
  • Model storage path change breaks existing models — Changing OLLAMA_MODELS does 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_PARALLEL or OLLAMA_MAX_LOADED_MODELS to lower memory pressure.
  • daemon-reload fails on WSL2 — WSL2 does not support full systemd. Use sudo service ollama restart instead on WSL2.
  • Wrong environment on Windows — On Windows, set variables via System Properties > Environment Variables or PowerShell [Environment]::SetEnvironmentVariable(...).

Related guides

RELATED GUIDES