Ollama
Quick reference for Ollama — run large language models locally with CLI commands, REST API, Modelfiles, vision, tool calling, and OpenAI-compatible endpoints.
Installation & Setup
Install Ollama on macOS, Linux, and Windows and start the server.
Install Ollama on your platform using the official installer or package manager.
# macOS / Linux — one-line install
curl -fsSL https://ollama.com/install.sh | sh
# macOS — via Homebrew
brew install ollama
# Windows — download installer from https://ollama.com/download
# Docker
docker run -d -v ollama:/root/.ollama -p 11434:11434 --name ollama ollama/ollamaConfigure Ollama behavior with environment variables.
# Common environment variables
export OLLAMA_HOST=0.0.0.0:11434 # Listen on all interfaces
export OLLAMA_MODELS=/path/to/models # Custom model storage path
export OLLAMA_KEEP_ALIVE=5m # How long to keep models loadedRunning Models
Run and interact with models from the command line.
Start an interactive chat session with a model.
# Run a model (downloads if not present)
ollama run llama3.2
# Run a specific size variant
ollama run llama3.2:1b
ollama run gemma3:27b
# Run with a one-off prompt (no interactive session)
ollama run llama3.2 "Explain Docker in one sentence"Model Management
Pull, list, copy, and remove models.
Download models from the registry and view installed models.
# Pull a model from the registry
ollama pull llama3.2
# List all downloaded models
ollama list
# Show model details (parameters, template, license)
ollama show llama3.2
# Show the Modelfile of a model
ollama show --modelfile llama3.2Create model aliases and delete models from local storage.
# Copy/alias a model to a new name
ollama cp llama3.2 my-model
# Remove a model
ollama rm my-modelModelfile
Create custom models with system prompts, parameters, and adapters using Modelfiles.
Define a Modelfile with a base model, system prompt, and parameters, then build it.
# Modelfile
FROM llama3.2
SYSTEM "You are a helpful coding assistant."
PARAMETER temperature 0.7
PARAMETER num_ctx 4096
# Build the model
ollama create my-coder -f ./ModelfileReference for common model parameters and their effects.
# Key parameters for PARAMETER instruction
PARAMETER temperature 0.7 # Creativity (0.0-2.0, default ~0.8)
PARAMETER num_ctx 4096 # Context window size in tokens
PARAMETER top_p 0.9 # Nucleus sampling threshold
PARAMETER top_k 40 # Top-K sampling
PARAMETER repeat_penalty 1.1 # Penalize repetition (1.0 = off)
PARAMETER seed 42 # Reproducible outputREST API
Interact with models programmatically using the Ollama REST API.
Generate text completions using the /api/generate endpoint.
# Generate a completion
curl http://localhost:11434/api/generate -d '{
"model": "llama3.2",
"prompt": "Why is the sky blue?",
"stream": false
}'Use the /api/chat endpoint for multi-turn conversations.
# Chat with message history
curl http://localhost:11434/api/chat -d '{
"model": "llama3.2",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "What is Docker?"}
],
"stream": false
}'Embeddings
Generate vector embeddings for text using embedding models.
Use the /api/embed endpoint to create text embeddings for RAG and similarity search.
# Generate embeddings for a single text
curl http://localhost:11434/api/embed -d '{
"model": "all-minilm",
"input": "Ollama is great for local AI"
}'Vision & Multimodal
Use vision models to analyze images alongside text prompts.
Send images to vision-capable models via CLI or API.
# CLI — pass an image file to a vision model
ollama run gemma3 ./photo.png "What's in this image?"
# API — send base64-encoded image
curl http://localhost:11434/api/chat -d '{
"model": "gemma3",
"messages": [{
"role": "user",
"content": "Describe this image",
"images": ["<base64-encoded-image>"]
}],
"stream": false
}'Structured Output
Force models to respond with structured JSON or schema-based output.
Use the format parameter to get structured JSON responses.
# JSON mode — free-form JSON output
curl http://localhost:11434/api/chat -d '{
"model": "llama3.2",
"messages": [{"role": "user", "content": "Tell me about Canada"}],
"format": "json",
"stream": false
}'Tool Calling
Let models call external functions and tools to augment their capabilities.
Define tools that models can invoke, then process the tool calls.
# Python — pass functions directly as tools
from ollama import chat
def get_weather(city: str) -> str:
"""Get current weather for a city"""
return f"Sunny, 22°C in {city}"
response = chat(
model='qwen3',
messages=[{'role': 'user', 'content': 'Weather in NYC?'}],
tools=[get_weather],
)OpenAI Compatibility
Use the OpenAI-compatible API to drop in Ollama as a local replacement.
Point the OpenAI SDK at Ollama for local inference with no code changes.
# Python — use OpenAI SDK with Ollama
from openai import OpenAI
client = OpenAI(
base_url='http://localhost:11434/v1/',
api_key='ollama', # required but ignored
)
response = client.chat.completions.create(
model='llama3.2',
messages=[{'role': 'user', 'content': 'Hello!'}],
)
print(response.choices[0].message.content)Official SDKs
Use the official Python and JavaScript libraries for a native Ollama experience.
Install and use the official Ollama Python library.
# Install
pip install ollama
# Basic chat
from ollama import chat
response = chat(model='llama3.2', messages=[
{'role': 'user', 'content': 'Hello!'},
])
print(response.message.content)Install and use the official Ollama JavaScript/TypeScript library.
// Install
// npm install ollama
import ollama from 'ollama'
const response = await ollama.chat({
model: 'llama3.2',
messages: [{ role: 'user', content: 'Hello!' }],
})
console.log(response.message.content)GPU & Performance
Configure GPU acceleration and optimize model performance.
Configure GPU layers, parallel requests, and memory management.
# Check which models are loaded and GPU usage
ollama ps
# Environment variables for GPU/performance
export OLLAMA_NUM_PARALLEL=4 # Concurrent requests
export OLLAMA_MAX_LOADED_MODELS=2 # Models in memory at once
export OLLAMA_FLASH_ATTENTION=1 # Enable flash attention
export OLLAMA_GPU_OVERHEAD=0 # Reserved GPU memory (bytes)