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
| Variable | Purpose | Default |
|---|---|---|
OLLAMA_HOST | Address and port the service listens on | 127.0.0.1:11434 |
OLLAMA_MODELS | Directory where models are stored | See below |
OLLAMA_CONTEXT_LENGTH | Default context length (tokens) | 4096 |
OLLAMA_KEEP_ALIVE | How long a model stays in memory while idle | 5m |
OLLAMA_NUM_PARALLEL | Number of requests each model handles at once | 1 |
OLLAMA_MAX_LOADED_MODELS | Maximum number of models loaded at once | 3 per GPU |
OLLAMA_MAX_QUEUE | Maximum number of queued requests; excess requests receive 503 | 512 |
OLLAMA_ORIGINS | Domains allowed to make cross-origin (CORS) requests | Localhost |
OLLAMA_FLASH_ATTENTION | Force Flash Attention on (1) or off (0) | Automatic |
OLLAMA_KV_CACHE_TYPE | Context cache quantization: f16, q8_0, q4_0 | f16 |
OLLAMA_NO_CLOUD | Disable 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
- First, quit Ollama from the system tray.
- Search the Start menu for "Edit environment variables for your account."
- Add or edit a variable, for example, set the name to
OLLAMA_HOSTand the value to0.0.0.0:11434. - 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:
-
Set
OLLAMA_HOSTto0.0.0.0:11434, then restart Ollama. -
Make sure your firewall allows traffic on port 11434.
-
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:
| System | Location |
|---|---|
| Windows | C:\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 16384in 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