A practical reference for running large language models locally with Ollama.
Installation
Quick Install Script
curl -fsSL https://ollama.com/install.sh | shAPT / Manual Binary
# APT (script adds repo automatically)
curl -fsSL https://ollama.com/install.sh | sh
# Manual binary
curl -L https://ollama.com/download/ollama-linux-amd64.tgz | sudo tar -C /usr/local -xz
ollama serve
# Docker
docker run -d --gpus all -v ollama:/root/.ollama -p 11434:11434 ollama/ollama
# Verify
ollama --versionModel Management
ollama list # List downloaded models
ollama pull llama3.2 # Pull default model
ollama pull llama3.2:1b # Specific tag
ollama pull llama3.2:q4_K_M # Quantized variant
ollama pull mixtral:8x7b # Mixtral MoE
ollama push myuser/mymodel:tag # Push to registry
ollama rm llama3.2 # Remove model
ollama cp llama3.2 my-copy # Copy locally
ollama show llama3.2 # Show model details
ollama show --modelfile llama3.2 # Show underlying Modelfile
ollama show --license llama3.2 # License information
ollama show --parameters llama3.2 # ParametersRunning Models
Interactive vs Single Prompt
ollama run llama3.2 # Interactive chat session
ollama run llama3.2 "Explain quantum computing" # Single shot
ollama run llama3.2 --verbose # Show timing stats
echo "What is 2+2?" | ollama run llama3.2 # Pipe input
ollama run llama3.2 --raw "Q:?" # Skip templateCustom Parameters
ollama run llama3.2 --temperature 0.1 # Deterministic output
ollama run llama3.2 --top-p 0.9 # Nucleus sampling
ollama run llama3.2 --seed 42 # Reproducible
ollama run llama3.2 --num-predict 100 # Max output tokens
ollama run llama3.2 --num-ctx 8192 # Extended context
ollama run llama3.2 --format json # Request JSON output
ollama run llama3.2 --keep-alive 5m # Keep in memory 5 minInteractive Commands
/help # Available commands
/set temperature 0.7 # Change parameter
/set num_ctx 4096 # Set context window
/set seed 42 # Set random seed
/show # Current settings
/load modelname # Switch model
/exit # Quit sessionOllama API
List, Generate, Chat
curl http://localhost:11434/api/tags # List models
curl http://localhost:11434/api/generate -d '{ # Generate
"model": "llama3.2", "prompt": "Hello", "stream": false
}'
curl http://localhost:11434/api/chat -d '{ # Chat
"model": "llama3.2",
"messages": [{"role": "user", "content": "Hello"}]
}'
curl http://localhost:11434/api/embeddings -d '{ # Embeddings
"model": "llama3.2", "prompt": "Hello world"
}'Advanced API with Options
curl http://localhost:11434/api/generate -d '{
"model": "llama3.2",
"prompt": "Write a haiku",
"options": {
"temperature": 0.8, "top_k": 40, "top_p": 0.95,
"num_predict": 100, "seed": 42
},
"stream": false
}'Custom Port & CORS
OLLAMA_HOST=0.0.0.0:11435 ollama serve # Custom port
OLLAMA_ORIGINS="*" ollama serve # Allow all origins
OLLAMA_ORIGINS="https://app.example.com" ollama serveConfiguration
Environment Variables
export OLLAMA_HOST=0.0.0.0 # Bind all interfaces
export OLLAMA_MODELS=/mnt/data/models # Custom model dir
export OLLAMA_KEEP_ALIVE=5m # Idle unload timeout
export OLLAMA_KEEP_ALIVE=-1 # Keep always loaded
export OLLAMA_NUM_PARALLEL=4 # Max concurrent reqs
export OLLAMA_MAX_LOADED_MODELS=2 # Models in memory
export OLLAMA_FLASH_ATTENTION=1 # Flash attention
export OLLAMA_DEBUG=1 # Debug loggingCustom Modelfile
FROM llama3.2
PARAMETER temperature 0.7
PARAMETER top_p 0.9
PARAMETER num_ctx 4096
PARAMETER stop "<|eot_id|>"
SYSTEM You are a helpful AI assistant.
TEMPLATE """
{{ if .System }}<|start_header_id|>system<|end_header_id|>
{{ .System }}<|eot_id|>{{ end }}
<|start_header_id|>user<|end_header_id|>
{{ .Prompt }}<|eot_id|>
<|start_header_id|>assistant<|end_header_id|>
"""Build
ollama create my-model -f ./Modelfile
ollama create my-model:cust -f Modelfile.customSystemd Service
[Unit]
Description=Ollama Service
After=network-online.target
[Service]
ExecStart=/usr/local/bin/ollama serve
Environment=OLLAMA_HOST=0.0.0.0
Environment=OLLAMA_KEEP_ALIVE=5m
Environment=OLLAMA_NUM_PARALLEL=4
Environment=CUDA_VISIBLE_DEVICES=0
Restart=always
RestartSec=3
[Install]
WantedBy=default.targetsudo systemctl daemon-reload && sudo systemctl enable --now ollama.service
journalctl -u ollama -f # Follow logsGPU Configuration
# NVIDIA
export CUDA_VISIBLE_DEVICES=0 # Single GPU
export CUDA_VISIBLE_DEVICES=0,1 # Multiple GPUs
# AMD
export ROCR_VISIBLE_DEVICES=0 # AMD GPU select
docker run -d --device=/dev/kfd --device=/dev/dri -v ollama:/root/.ollama ollama/ollama:rocm
# CPU only
OLLAMA_NO_GPU=1 ollama servePerformance Tuning
# Context length
ollama run llama3.2 --num-ctx 32768 # 32K context
# Param in Modelfile: PARAMETER num_ctx 16384
# NUMA (multi-socket)
numactl --cpunodebind=0 --membind=0 ollama serve
# Flash attention
OLLAMA_FLASH_ATTENTION=1 ollama serveSecurity
# UFW
ufw allow from 192.168.1.0/24 to any port 11434
ufw deny 11434
# iptables
iptables -A INPUT -p tcp --dport 11434 -s 127.0.0.1 -j ACCEPT
iptables -A INPUT -p tcp --dport 11434 -j DROP
# Rate limiting
iptables -A INPUT -p tcp --dport 11434 -m limit --limit 10/minute -j ACCEPTModel Quantization
# Quality/size tradeoffs:
ollama pull llama3.2:q2_K # 2-bit, smallest
ollama pull llama3.2:q3_K_M # 3-bit medium
ollama pull llama3.2:q4_K_M # 4-bit, best tradeoff
ollama pull llama3.2:q5_K_M # 5-bit medium
ollama pull llama3.2:q8_0 # 8-bit, highest quality
ollama pull llama3.2:iq4_xs # IQ format, improved 4-bitTroubleshooting
# OOM: use smaller model, shorter context, fewer parallel requests
ollama pull llama3.2:1b # Smaller model
OLLAMA_NUM_PARALLEL=1 ollama serve # Serial processing
# Slow inference: enable flash attention, use quantized model
OLLAMA_FLASH_ATTENTION=1 ollama serve
ollama pull llama3.2:q4_K_M
# CUDA errors: check driver, restart serve
nvidia-smi # Check driver
OLLAMA_DEBUG=1 ollama serve # Debug mode
# Connection refused: verify server running
curl http://localhost:11434/api/tags # Test endpoint
ss -tlnp | grep 11434 # Check bindingIntegration
OpenAI-Compatible Endpoint
from openai import OpenAI
client = OpenAI(base_url="http://localhost:11434/v1", api_key="ollama")
resp = client.chat.completions.create(
model="llama3.2",
messages=[{"role": "user", "content": "Hello"}]
)curl http://localhost:11434/v1/chat/completions -d '{
"model": "llama3.2",
"messages": [{"role": "user", "content": "Hi"}]
}'LangChain
from langchain_ollama import ChatOllama
llm = ChatOllama(model="llama3.2", temperature=0.7)
print(llm.invoke("Tell me a joke").content)Open WebUI
docker run -d -p 3000:8080 \
--add-host=host.docker.internal:host-gateway \
-v open-webui:/app/backend/data \
ghcr.io/open-webui/open-webui:main
# Access http://localhost:3000REST Management
curl http://localhost:11434/api/create -d '{"name":"m","modelfile":"FROM llama3.2\nSYSTEM You are helpful."}'
curl http://localhost:11434/api/copy -d '{"source":"llama3.2","destination":"backup"}'
curl -X DELETE http://localhost:11434/api/delete -d '{"name":"backup"}'