Contents

Ollama Environment Variables Tutorial: Configure Host, Model Paths, and Context Length

Ollama's behavior is controlled by environment variables. This section covers the most common settings and how to configure them on each operating system.

Common environment variables

VariablePurposeDefault
OLLAMA_HOSTAddress and port the service listens on127.0.0.1:11434
OLLAMA_MODELSDirectory where models are storedSee below
OLLAMA_CONTEXT_LENGTHDefault context length (tokens)4096
OLLAMA_KEEP_ALIVEHow long a model stays in memory while idle5m
OLLAMA_NUM_PARALLELNumber of requests each model handles at once1
OLLAMA_MAX_LOADED_MODELSMaximum number of models loaded at once3 per GPU
OLLAMA_MAX_QUEUEMaximum number of queued requests; excess requests receive 503512
OLLAMA_ORIGINSDomains allowed to make cross-origin (CORS) requestsLocalhost
OLLAMA_FLASH_ATTENTIONForce Flash Attention on (1) or off (0)Automatic
OLLAMA_KV_CACHE_TYPEContext cache quantization: f16, q8_0, q4_0f16
OLLAMA_NO_CLOUDDisable cloud features (cloud models, web search)Disabled

Run ollama serve --help to see the full list. Defaults may change between versions, so check the version you have installed.

How to configure

Windows

  1. First, quit Ollama from the system tray.
  2. Search the Start menu for "Edit environment variables for your account."
  3. Add or edit a variable, for example, set the name to OLLAMA_HOST and the value to 0.0.0.0:11434.
  4. Select OK, then restart Ollama from the Start menu.

macOS (app version)

launchctl setenv OLLAMA_HOST "0.0.0.0:11434"
launchctl setenv OLLAMA_CONTEXT_LENGTH "8192"

After setting the variables, restart the Ollama app. launchctl setenv stops working after a reboot. To make the settings persistent, run a script at login.

Linux (systemd)

sudo systemctl edit ollama.service

Add the following to the editor that opens:

[Service]
Environment="OLLAMA_HOST=0.0.0.0:11434"
Environment="OLLAMA_CONTEXT_LENGTH=8192"

Save the file, then apply the changes:

sudo systemctl daemon-reload
sudo systemctl restart ollama

When running manually

OLLAMA_HOST=0.0.0.0:11434 ollama serve

Connect from other computers on your local network

By default, Ollama listens only on 127.0.0.1, so only the local computer can connect. To let other computers (or phones or NAS devices) on the same network use it:

  1. Set OLLAMA_HOST to 0.0.0.0:11434, then restart Ollama.

  2. Make sure your firewall allows traffic on port 11434.

  3. Test the connection from another computer:

    curl http://YourComputerIP:11434

Ollama's API does not require a username or password. Never expose port 11434 directly to the internet, or anyone could use your GPU and even delete your models. To access Ollama remotely, use a VPN (such as Tailscale) or put an authenticated reverse proxy in front of it.

Change where models are stored

Models can range from several GB to tens of GB. Their default locations are:

SystemLocation
WindowsC:\Users\YourUserName\.ollama\models
macOS~/.ollama/models
Linux/usr/share/ollama/.ollama/models

To store models on another drive, set OLLAMA_MODELS, for example, to D:\ollama\models on Windows. On Linux, make sure the ollama user has read and write access to the new directory:

sudo mkdir -p /data/ollama/models
sudo chown -R ollama:ollama /data/ollama

You can move the entire models directory to the new location without downloading the models again.

Context length

Context length is the number of tokens a model can process at once, including your input, conversation history, and the model's response. In Chinese, one character is roughly 1–2 tokens.

If you give the model a long document to summarize and find that it only read the latter part, the context is usually too short and the earlier content was truncated.

Choose one of these ways to adjust it:

  • Global: OLLAMA_CONTEXT_LENGTH=16384
  • For one model: PARAMETER num_ctx 16384 in a Modelfile
  • For one request: "options": {"num_ctx": 16384} in the API

Longer contexts use more memory. Run ollama ps to check whether the model still fits entirely in GPU memory.

Keep models loaded

Loading a model takes time each time (large models may take more than ten seconds).

OLLAMA_KEEP_ALIVE=30m     # Unload after 30 minutes of inactivity
OLLAMA_KEEP_ALIVE=-1      # Never unload
OLLAMA_KEEP_ALIVE=0       # Unload immediately after use

The keep_alive parameter in an API request overrides this setting.

Serve multiple users at once

When using Ollama as a shared service for a team:

OLLAMA_NUM_PARALLEL=4
OLLAMA_MAX_LOADED_MODELS=2

Note: The memory used for the context grows with the number of parallel requests (approximately NUM_PARALLEL × CONTEXT_LENGTH), so make sure you have enough GPU memory.

Call Ollama directly from a web page (CORS)

When calling Ollama directly from a browser extension or a web page on another domain, allow that origin:

OLLAMA_ORIGINS="https://my-app.example.com,chrome-extension://*"

Updated October 12, 2026