Configure Ollama for GPU Privacy, Context Size & Storage
In this article
Yes, you can configure Ollama to run locally on your GPU, keep all data private, control the context window, and decide where each model is stored.
What You'll Need
- A computer with a compatible NVIDIA or AMD GPU and up‑to‑date drivers.
- Operating system: Linux, macOS, or Windows (64‑bit).
- Administrative or sudo rights to install software.
- Internet connection for the initial model download (once downloaded, no further network traffic is required).
- Enough disk space for the models you intend to use (each model can be several gigabytes).
- Basic familiarity with a terminal or command prompt.
Step‑by‑Step
- Install Ollama. Open a terminal and run the official installer:
curl -fsSL https://ollama.com/install.sh | sh
On Windows, download the .exe from the official site and run the installer. Verify the installation withollama --version. - Confirm GPU availability. Ollama automatically detects CUDA‑enabled NVIDIA GPUs or AMD GPUs with ROCm. Run:
ollama list
If the output showsGPU: enabled, you are ready. If not, update your drivers and ensure thenvidia‑container‑toolkit(for NVIDIA) or the appropriate ROCm packages are installed. - Set privacy‑first environment variables. Create a file called
.envin your home directory (or edit your shell profile) and add:OLLAMA_NO_TELEMETRY=1 OLLAMA_HOST=127.0.0.1:11434
The first line disables anonymous usage reporting. The second forces the server to bind only to the loopback address, preventing external access. - Choose a custom model storage folder. By default Ollama stores models under
~/.ollama/models. To relocate them, set theOLLAMA_MODEL_PATHvariable:export OLLAMA_MODEL_PATH=/mnt/data/ollama_models
Create the directory first and ensure the user running Ollama has read/write permissions. - Adjust the context size. The context window determines how many tokens the model can consider at once. Set
OLLAMA_MAX_CONTEXTto the desired token count (e.g., 8192):export OLLAMA_MAX_CONTEXT=8192
Higher values increase memory usage; keep it within the limits of your GPU VRAM (rough rule: 1 GB VRAM ≈ 2000 tokens). - Restart the Ollama service. After changing environment variables, restart:
ollama stop && ollama start
On Windows use the Services manager or runollama.exe stopthenollama.exe start. - Pull a model to the new storage location. Example with Llama 3.1:
ollama pull llama3.1
The model will be downloaded into the path defined byOLLAMA_MODEL_PATH. Verify withollama list– the model path should reflect your custom directory. - Run a test query. Use the CLI or an API client:
ollama run llama3.1 "Explain quantum entanglement in two sentences."
If the response appears quickly and no external IP addresses are shown in network monitors, your privacy configuration is working. - Persist the environment variables. Add the export lines to your shell profile (
~/.bashrc,~/.zshrc, or Windows System Environment Variables) so they survive reboots. - Optional: Enable GPU memory pre‑allocation. For large context windows, pre‑allocate VRAM to avoid fragmentation:
export OLLAMA_GPU_ALLOC=1
Restart Ollama again after setting this flag.
Common Mistakes
- Setting
OLLAMA_MAX_CONTEXThigher than the GPU can handle, which leads to out‑of‑memory crashes. - Forgetting to bind the server to
127.0.0.1; leaving the default0.0.0.0can expose the API to the local network. - Placing
OLLAMA_MODEL_PATHon a filesystem that does not support sparse files; this can waste disk space and slow loading. - Running the installer as a non‑admin user without sudo; the service will fail to start.
- Disabling telemetry but not clearing existing logs; old logs may still contain usage data.
Tips
- Use a dedicated SSD for
OLLAMA_MODEL_PATHto reduce load latency. - Monitor VRAM with
nvidia‑smi(orrocm-smi) while testing different context sizes. - Keep a small “baseline” model locally (e.g., tiny‑llama) for quick sanity checks before pulling larger models.
- If you run multiple Ollama instances, assign each a unique
OLLAMA_HOSTport to avoid clashes. - Regularly prune unused models with
ollama pruneto reclaim disk space.
Troubleshooting
Problem: Ollama fails to start after setting environment variables.
- Run
ollama statusto view the error log. - Check that the variables are exported in the same shell session where you run
ollama start. - Ensure the custom model directory exists and is writable.
- If the log mentions
CUDA error: unknown error, reinstall or update the NVIDIA driver.
Problem: Out‑of‑memory error when using a large context size.
- Reduce
OLLAMA_MAX_CONTEXTin increments of 512 tokens. - Verify VRAM usage with
nvidia‑smi. If usage is near 90 %, lower the context size. - Consider swapping to a smaller model that requires less VRAM per token.
Problem: Model files are still being downloaded to the default folder.
- Confirm that
OLLAMA_MODEL_PATHpoints to an absolute path (no tilde expansion). - Restart the service after changing the variable; a running daemon will ignore later changes.
- Run
ollama list --verboseto see the actual storage location for each model.
Problem: API is reachable from another device on the LAN.
- Double‑check
OLLAMA_HOST. It must be exactly127.0.0.1:11434(orlocalhost:11434). - If you use a reverse proxy, ensure it binds only to localhost.
- Run
netstat -tulnp | grep 11434(Linux) to verify the listening address.
Frequently Asked Questions
Can I run Ollama on a CPU‑only machine?
Yes, but performance will be much slower and the context size may need to be reduced to stay within RAM limits.
Is any data ever sent to Ollama’s cloud servers?
No. When you bind the service to 127.0.0.1 and disable telemetry, all inference runs stay on your hardware.
How do I know which context size a model supports?
Model documentation usually lists the maximum token window. If unsure, start with 2048 tokens and increase until you hit a memory error.
Do I need to set OLLAMA_MAX_CONTEXT for every model?
The variable is global; it applies to all loaded models. Changing it after a model is loaded requires a restart to take effect.
Can I store models on an external USB drive?
Yes, as long as the drive is formatted with a filesystem that supports large files and you give the Ollama process read/write permissions.
What happens to existing models after I change OLLAMA_MODEL_PATH?
They remain in the original folder. You can move them manually to the new directory and run ollama list --refresh to have Ollama recognize the new location.