Ollama and Open WebUI Optimization Guide

May 16, 2026 ยท View on GitHub

Configure Ollama and Open WebUI for optimal performance based on your server hardware and use case.

Table of Contents

Overview

Ollama performance depends on three factors:

  1. Hardware resources: CPU cores, RAM, and GPU availability
  2. Configuration tuning: Environment variables that control memory and threading
  3. Model selection: Choosing models appropriate for your hardware

The default configuration works for most setups, but tuning these settings can improve response times and stability.

Environment Variables

Ollama Environment Variables Reference

VariableDefaultDescription
OLLAMA_HOST127.0.0.1:11434Network interface and port to bind
OLLAMA_NUM_PARALLELAutoMaximum concurrent requests
OLLAMA_MAX_LOADED_MODELSAutoModels kept in memory simultaneously
OLLAMA_KEEP_ALIVE5mTime to keep model loaded after last request
OLLAMA_NUM_THREADSAutoCPU threads for inference
OLLAMA_FLASH_ATTENTION0Enable Flash Attention optimization

Detailed Variable Explanations

OLLAMA_HOST

Controls which network interface Ollama listens on.

OLLAMA_HOST=0.0.0.0:11434    # Listen on all interfaces
OLLAMA_HOST=127.0.0.1:11434  # Listen only on localhost (default)

When to change: Set to 0.0.0.0:11434 when you need direct network access to Ollama from other containers or external services without going through the reverse proxy.

OLLAMA_NUM_PARALLEL

Maximum number of requests Ollama processes simultaneously.

OLLAMA_NUM_PARALLEL=2    # Process 2 requests at once
OLLAMA_NUM_PARALLEL=4    # Process 4 requests at once

How to calculate:

  • Each parallel request uses additional memory
  • Formula: available_ram / (model_size * 1.5)
  • Example: 24GB RAM with 8GB model = 24 / (8 * 1.5) = 2 parallel requests

When to increase: Multiple users accessing Open WebUI simultaneously.

When to decrease: Memory errors or slow responses under load.

OLLAMA_MAX_LOADED_MODELS

Number of models kept in memory at once.

OLLAMA_MAX_LOADED_MODELS=1    # One model at a time
OLLAMA_MAX_LOADED_MODELS=2    # Two models loaded simultaneously

How to calculate:

  • Formula: available_ram / average_model_size
  • Example: 24GB RAM with 8GB models = 3 models maximum (but leave headroom)

When to increase: Frequently switching between multiple models.

When to decrease: Running large models or experiencing out-of-memory errors.

OLLAMA_KEEP_ALIVE

Duration to keep a model loaded in memory after the last request.

OLLAMA_KEEP_ALIVE=5m     # Unload after 5 minutes idle (default)
OLLAMA_KEEP_ALIVE=24h    # Keep loaded for 24 hours
OLLAMA_KEEP_ALIVE=0      # Unload immediately after each request
OLLAMA_KEEP_ALIVE=-1     # Never unload (keep forever)

When to increase:

  • Frequent usage patterns with gaps between requests
  • Fast response times are critical
  • Server has sufficient RAM

When to decrease:

  • Memory is limited
  • Multiple models need to be available
  • Infrequent usage patterns

OLLAMA_NUM_THREADS

CPU threads allocated for model inference.

OLLAMA_NUM_THREADS=8     # Use 8 CPU threads
OLLAMA_NUM_THREADS=4     # Use 4 CPU threads

How to calculate:

  • Leave 1-2 cores for system and other services
  • Formula: total_cores - 2
  • Example: 8 core server = 6-7 threads for Ollama

When to increase: Running on CPU-only server with many cores.

When to decrease: Other services need CPU resources.

OLLAMA_FLASH_ATTENTION

Enables Flash Attention for faster inference on supported hardware.

OLLAMA_FLASH_ATTENTION=1    # Enable Flash Attention
OLLAMA_FLASH_ATTENTION=0    # Disable (default)

When to enable:

  • Server has a supported GPU (NVIDIA with Compute Capability 7.0+)
  • Running models that support Flash Attention
  • Processing long context windows

When to disable:

  • CPU-only inference
  • Older GPU hardware
  • Experiencing instability

Port Binding Configuration

The ports section in docker-compose.yml controls network access to Ollama.

Option 1: Localhost Only (Default)

ports:
  - "127.0.0.1:11434:11434"

Behavior: Ollama accessible only from the server itself.

Use cases:

  • Claude Code running on the same server via SSH
  • Open WebUI accessing Ollama through Docker network
  • Maximum security (no external access)

Access method: SSH tunnel from your local machine.

# On your local machine
ssh -L 11434:127.0.0.1:11434 dev@server -p 5522 -N

# Then access via http://localhost:11434

Option 2: Tailscale Only

ports:
  - "${TS_IP}:11434:11434"

Prerequisites: Set TS_IP in the .env file.

# In ~/docker/ollama-openwebui/.env
TS_IP=100.x.x.x

Behavior: Ollama accessible only from Tailscale network.

Use cases:

  • Remote IDE integration (VS Code, Zed, Continue)
  • Direct API access from Tailscale-connected devices
  • Multi-user access from trusted devices

Access method: Direct connection via Tailscale IP.

# From any Tailscale-connected device
curl http://100.x.x.x:11434/api/tags

Option 3: Both Localhost and Tailscale

ports:
  - "127.0.0.1:11434:11434"
  - "${TS_IP}:11434:11434"

Use cases:

  • Claude Code on server (localhost) plus remote IDE access (Tailscale)
  • Flexibility for different access patterns

Security Comparison

ConfigurationNetwork ExposureSecurity Level
Localhost onlyNoneHighest
Tailscale onlyTailscale meshHigh
BothTailscale meshHigh
0.0.0.0 (not recommended)All interfacesLow

Server Profiles

Small Server (4GB RAM, 2 vCPU)

Suitable for: Light testing, small models only.

environment:
  - OLLAMA_HOST=0.0.0.0:11434
  - OLLAMA_NUM_PARALLEL=1
  - OLLAMA_MAX_LOADED_MODELS=1
  - OLLAMA_KEEP_ALIVE=5m
  - OLLAMA_NUM_THREADS=1
mem_limit: 3g
memswap_limit: 4g
cpus: 1.5

Recommended models: llama3.2:1b, qwen2.5:0.5b, phi3:mini

Medium Server (8GB RAM, 4 vCPU)

Suitable for: Individual development, moderate usage.

environment:
  - OLLAMA_HOST=0.0.0.0:11434
  - OLLAMA_NUM_PARALLEL=1
  - OLLAMA_MAX_LOADED_MODELS=1
  - OLLAMA_KEEP_ALIVE=1h
  - OLLAMA_NUM_THREADS=3
mem_limit: 6g
memswap_limit: 8g
cpus: 3

Recommended models: llama3.2:3b, codellama:7b, mistral:7b

Large Server (16GB RAM, 8 vCPU)

Suitable for: Regular development, multiple users.

environment:
  - OLLAMA_HOST=0.0.0.0:11434
  - OLLAMA_NUM_PARALLEL=2
  - OLLAMA_MAX_LOADED_MODELS=1
  - OLLAMA_KEEP_ALIVE=4h
  - OLLAMA_NUM_THREADS=6
mem_limit: 12g
memswap_limit: 16g
cpus: 6

Recommended models: llama3.1:8b, codellama:13b, deepseek-coder:6.7b

KVM8 Profile (32GB RAM, 8 vCPU)

Suitable for: Heavy usage, larger models, multiple concurrent users.

environment:
  - OLLAMA_HOST=0.0.0.0:11434
  - OLLAMA_NUM_PARALLEL=2
  - OLLAMA_MAX_LOADED_MODELS=1
  - OLLAMA_KEEP_ALIVE=24h
  - OLLAMA_NUM_THREADS=8
  - OLLAMA_FLASH_ATTENTION=1
mem_limit: 24g
memswap_limit: 28g
cpus: 7

Recommended models: llama3.1:8b, mixtral:8x7b, deepseek-coder:33b Working with: hf.co/mradermacher/Huihui-Qwen3-Coder-30B-A3B-Instruct-abliterated-i1-GGUF:Q4_K_M

GPU Server (Any RAM with NVIDIA GPU)

Suitable for: Fast inference, large models.

environment:
  - OLLAMA_HOST=0.0.0.0:11434
  - OLLAMA_NUM_PARALLEL=4
  - OLLAMA_MAX_LOADED_MODELS=2
  - OLLAMA_KEEP_ALIVE=24h
  - OLLAMA_FLASH_ATTENTION=1
deploy:
  resources:
    reservations:
      devices:
        - driver: nvidia
          count: all
          capabilities: [gpu]

Note: GPU memory limits model size. A 24GB GPU can run most 70B quantized models.

Resource Calculation

Step 1: Determine Available Resources

# Check total RAM
free -h

# Check CPU cores
nproc

# Check for GPU
nvidia-smi  # If available

Step 2: Calculate Ollama Allocation

Reserve resources for system and other containers:

ComponentRAM ReservationCPU Reservation
System2GB1 core
Open WebUI2GB1 core
Traefik256MB0.25 core
Other containersVariesVaries

Formula for Ollama limits:

ollama_ram = total_ram - 4GB (system + webui)
ollama_cpus = total_cores - 2

Step 3: Calculate Thread Count

OLLAMA_NUM_THREADS = ollama_cpus - 1

Leave one CPU for Ollama's internal operations.

Step 4: Calculate Parallel Requests

OLLAMA_NUM_PARALLEL = floor(ollama_ram / (model_size * 1.5))

Each parallel request needs approximately 1.5x the model size in memory.

Example Calculation

Server: 32GB RAM, 8 vCPU, using 8GB model

  1. Ollama RAM: 32GB - 4GB = 28GB (set mem_limit: 24g with buffer)
  2. Ollama CPUs: 8 - 2 = 6 (set cpus: 7 allowing burst)
  3. Thread count: 6 - 1 = 5 (set OLLAMA_NUM_THREADS=6-8)
  4. Parallel requests: 24 / (8 * 1.5) = 2 (set OLLAMA_NUM_PARALLEL=2)

Applying Configuration Changes

Method 1: Edit docker-compose.yml Directly

  1. Open the configuration file:

    nano ~/docker/ollama-openwebui/docker-compose.yml
    
  2. Uncomment and modify the environment section:

    services:
      ollama:
        image: ollama/ollama:latest
        container_name: ollama
        restart: unless-stopped
        environment:
          - OLLAMA_HOST=0.0.0.0:11434
          - OLLAMA_NUM_PARALLEL=2
          - OLLAMA_MAX_LOADED_MODELS=1
          - OLLAMA_KEEP_ALIVE=24h
          - OLLAMA_NUM_THREADS=8
          - OLLAMA_FLASH_ATTENTION=1
    
  3. Modify resource limits if needed:

        mem_limit: 24g
        memswap_limit: 28g
        cpus: 7
    
  4. Apply changes:

    cd ~/docker/ollama-openwebui
    docker compose down
    docker compose up -d
    

Method 2: Use Environment File

  1. Add variables to the .env file:

    nano ~/docker/ollama-openwebui/.env
    
  2. Add Ollama settings:

    # Ollama Configuration
    OLLAMA_NUM_PARALLEL=2
    OLLAMA_MAX_LOADED_MODELS=1
    OLLAMA_KEEP_ALIVE=24h
    OLLAMA_NUM_THREADS=8
    
  3. Reference in docker-compose.yml:

    environment:
      - OLLAMA_HOST=0.0.0.0:11434
      - OLLAMA_NUM_PARALLEL=${OLLAMA_NUM_PARALLEL:-1}
      - OLLAMA_MAX_LOADED_MODELS=${OLLAMA_MAX_LOADED_MODELS:-1}
      - OLLAMA_KEEP_ALIVE=${OLLAMA_KEEP_ALIVE:-5m}
      - OLLAMA_NUM_THREADS=${OLLAMA_NUM_THREADS:-4}
    
  4. Apply changes:

    cd ~/docker/ollama-openwebui
    docker compose down
    docker compose up -d
    

Method 3: Change Port Binding

  1. For Tailscale access, first get your Tailscale IP:

    tailscale ip -4
    
  2. Add to .env file:

    TS_IP=100.x.x.x
    
  3. Update ports in docker-compose.yml:

    ports:
      - "${TS_IP}:11434:11434"
    
  4. Apply changes:

    cd ~/docker/ollama-openwebui
    docker compose down
    docker compose up -d
    

Verify Changes Applied

# Check container is running with new settings
docker inspect ollama | grep -A 20 "Env"

# Check port bindings
docker port ollama

# Test Ollama is responding
curl http://localhost:11434/api/tags

Performance Monitoring

Check Current Resource Usage

# Real-time container stats
docker stats ollama

# One-time snapshot
docker stats ollama --no-stream

Monitor Memory Usage

# Check if model is loaded
docker exec ollama ollama ps

# Check model memory usage
docker exec ollama ollama ps --format json | jq

Check Response Times

# Time a simple request
time curl -s http://localhost:11434/api/generate -d '{
  "model": "llama3.2",
  "prompt": "Hello",
  "stream": false
}' | jq -r '.response'

Review Logs for Issues

# Check for errors
docker logs ollama 2>&1 | grep -i error

# Check for memory warnings
docker logs ollama 2>&1 | grep -i memory

# Full log review
docker logs ollama --tail 100

Signs of Misconfiguration

SymptomPossible CauseSolution
Very slow first responseModel loading from diskIncrease OLLAMA_KEEP_ALIVE
Out of memory errorsModel too largeUse smaller model or reduce NUM_PARALLEL
High CPU, slow responsesToo many threadsReduce OLLAMA_NUM_THREADS
Requests timing outToo many parallel requestsReduce OLLAMA_NUM_PARALLEL
Model constantly reloadingLow KEEP_ALIVE valueIncrease OLLAMA_KEEP_ALIVE

Model Selection

Model Memory Requirements

ModelParametersMemory RequiredRecommended Server
llama3.2:1b1B~2GB4GB RAM
llama3.2:3b3B~4GB8GB RAM
llama3.1:8b8B~8GB16GB RAM
codellama:7b7B~6GB16GB RAM
codellama:13b13B~10GB24GB RAM
codellama:34b34B~20GB32GB RAM
mixtral:8x7b47B~26GB32GB RAM
llama3.1:70b70B~40GB64GB RAM or GPU

Choosing Quantization

Models come in different quantization levels that trade quality for memory:

QuantizationMemoryQualityUse Case
Q4_0LowestLowerVery limited RAM
Q4_K_MLowGoodLimited RAM
Q5_K_MMediumBetterBalanced
Q6_KHigherHighQuality focus
Q8_0HighHighestMaximum quality
FP16HighestBestGPU with VRAM
# Pull specific quantization
docker exec ollama ollama pull llama3.1:8b-q4_K_M
docker exec ollama ollama pull llama3.1:8b-q8_0

Code Completion:

  • Light: codellama:7b
  • Heavy: codellama:34b or deepseek-coder:33b

General Chat:

  • Light: llama3.2:3b
  • Heavy: llama3.1:8b or llama3.1:70b

Document Analysis:

  • Light: mistral:7b
  • Heavy: mixtral:8x7b

Last updated: 2026-05-16 (v1.0.0)