2025 Data: What Blockchain Adoption Metrics Tell Us About the Future of Digital Payment Infrastructure
2025 blockchain metrics on financial transactions, fraud prevention, and digital wallet adoption — see what production...
Running a language model locally costs you nothing per token and leaks zero data to third parties. That’s the entire argument. If your project involves sensitive documents, tight budgets, or you simply want inference that works offline, local LLMs aren’t a curiosity anymore — they’re a practical choice. This walkthrough covers everything you need for Python local LLM development with Ollama — from installation to a working RAG pipeline, on CPU or GPU, with code you can run today.
Ollama is a single binary that packages llama.cpp with a model registry and a REST API. That’s it. No Docker containers to configure, no Python environment headaches before you even load a model. You install it, pull a model like you’d pull a Docker image, and a local HTTP server starts serving inference requests on localhost:11434.
The API surface mirrors OpenAI’s — same /v1/chat/completions endpoint shape, same message format. That decision alone makes it worth considering for Python local LLM development with Ollama, because it means your existing OpenAI-based code needs maybe two lines changed.
Here’s how Ollama compares to the other popular options:
| Tool | Setup Complexity | GPU Required? | OpenAI-Compatible API | Best For |
|---|---|---|---|---|
| Ollama | Low | No | Yes | Developer machines, prototyping |
| vLLM | High | Yes (multi-GPU) | Yes | High-concurrency production |
| llama.cpp (raw) | Medium | No | No | Fine-grained control |
Python LLM inference optimization with vLLM makes sense when you need to serve dozens of concurrent requests on beefy hardware. For everything else — solo development, privacy-sensitive workflows, offline environments — Ollama is the better starting point.
One command:
curl -fsSL https://ollama.com/install.sh | sh
After that, verify it’s running:
ollama --version
systemctl status ollama # Linux only
On macOS, Ollama installs as a menu bar app and auto-starts a background service. You don’t need to manage it manually.
The gotcha here is GPU passthrough. It requires WSL2 version 2.x or later — check with wsl --version. If you’re on an older WSL2, update it before installing Ollama, or you’ll get CPU-only inference even on a machine with a decent NVIDIA card. Worth checking before you go any further.
CPU-only works out of the box. On a modern 8-core machine with a 7B parameter model, expect roughly 5–10 tokens per second. Usable for development, slow for anything that needs to feel responsive.
GPU acceleration kicks in automatically when Ollama detects CUDA (NVIDIA) or Metal (Apple Silicon). For NVIDIA, you need CUDA 11.8 or later installed separately. Apple Silicon machines get Metal support automatically — and because of unified memory, a MacBook Pro with 32GB RAM can comfortably run a 13B model.
A practical reference for model sizes:
| Model Size | Recommended VRAM | Suggested Quantization | Approx. CPU tok/s |
|---|---|---|---|
| 7B | 6–8 GB | Q4_K_M | 5–10 |
| 13B | 10–12 GB | Q4_K_M | 2–4 |
| 70B | 40+ GB | Q4_K_M | <1 |
Q4_K_M is the sweet spot almost every time. It cuts model size roughly in half compared to full precision with minimal quality loss.
ollama pull llama3.2 # download a model
ollama list # see what's local
ollama rm llama3.2 # free up disk space
Model tags encode both size and quantization: llama3.2:8b-instruct-q4_K_M tells you it’s the 8B instruct variant at 4-bit K-means quantization. If you run ollama pull llama3.2 without a tag, you get the default — usually a reasonable 4-bit quantized version.
The right starting model depends on what you’re building. For general-purpose chat and RAG, Llama 3.2 or Mistral 7B are solid defaults. Code generation is better served by CodeLlama or Deepseek-Coder. And if you’re on constrained hardware — 8GB RAM or less — Phi-3.5-mini (3.8B) punches well above its weight.
Before you commit a model to your code, test it interactively:
ollama pull llama3.2
ollama run llama3.2
>>> What's the difference between a list and a tuple in Python?
You’ll have a response in seconds. If it feels slow or the quality isn’t there for your use case, pull a different model and try again. It costs you nothing but a few minutes and some disk space.
pip install ollama
import ollama
response = ollama.chat(
model='llama3.2',
messages=[{'role': 'user', 'content': 'Explain async in Python.'}]
)
print(response['message']['content'])
For streaming — which you want if you’re building anything user-facing:
for chunk in ollama.chat(model='llama3.2', messages=[...], stream=True):
print(chunk['message']['content'], end='', flush=True)
Here’s what most guides don’t make obvious enough: if you’ve already built something with the openai Python package, you can point it at Ollama with two lines. No new SDK to learn, no refactoring your request logic.
from openai import OpenAI
client = OpenAI(
base_url="http://localhost:11434/v1",
api_key="ollama" # required by the SDK, ignored by Ollama
)
response = client.chat.completions.create(
model="llama3.2",
messages=[{"role": "user", "content": "Summarize this document."}]
)
Every other call — streaming, tool use, system prompts — works the same way.
A Python RAG application using LlamaIndex and Ollama is surprisingly compact. The key insight is that you can run embeddings locally too — Ollama serves nomic-embed-text as an embedding model, so nothing touches an external API. The entire pipeline stays on your machine.
pip install llama-index llama-index-llms-ollama llama-index-embeddings-ollama
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, Settings
from llama_index.llms.ollama import Ollama
from llama_index.embeddings.ollama import OllamaEmbedding
# Configure both LLM and embeddings to use Ollama
Settings.llm = Ollama(model="llama3.2", request_timeout=120.0)
Settings.embed_model = OllamaEmbedding(model_name="nomic-embed-text")
# Load local documents (PDFs, txt, md — LlamaIndex handles them all)
documents = SimpleDirectoryReader("./docs").load_data()
# Index and query
index = VectorStoreIndex.from_documents(documents)
query_engine = index.as_query_engine()
response = query_engine.query("What are the key findings in this report?")
print(response)
Every part of this pipeline — chunking, embedding, retrieval, generation — runs on your machine. I’ve seen teams spend months building cloud-based RAG systems, then realize during a security audit that they’d been sending proprietary documents to third-party APIs. Local inference eliminates that problem entirely.
| Variable | What It Does | Typical Value |
|---|---|---|
OLLAMA_HOST | Bind address | 0.0.0.0:11434 to expose beyond localhost |
OLLAMA_NUM_PARALLEL | Concurrent requests | 4 for a development server |
OLLAMA_MAX_LOADED_MODELS | Models kept hot in memory | 2 to balance RAM vs. cold-start time |
OLLAMA_KEEP_ALIVE | How long to keep model loaded | -1 for always-on |
Set OLLAMA_KEEP_ALIVE=-1 when you’re in active development. The default behavior unloads models after 5 minutes of inactivity, which means your next request waits 10–20 seconds to reload. Annoying during iteration.
When your Python app lives in a Docker container, the localhost address won’t reach the Ollama service running on your host. Use host.docker.internal instead:
# In your Docker container
client = OpenAI(base_url="http://host.docker.internal:11434/v1", api_key="ollama")
And on the host side, expose Ollama on all interfaces with OLLAMA_HOST=0.0.0.0:11434.
Yes, but with limits. Set OLLAMA_NUM_PARALLEL to control how many requests run concurrently. Each parallel request requires additional VRAM or RAM proportional to the context window size. On a 16GB GPU with a 7B model, you can realistically handle 2–4 parallel requests before memory becomes the bottleneck. For serious multi-user production workloads, Python LLM inference optimization with vLLM is worth the setup overhead — it’s built specifically for high-throughput serving.
No. Ollama runs on CPU out of the box and requires no CUDA drivers or special configuration. CPU inference is slower — roughly 5–10 tokens per second on a 7B model — but entirely functional for development, testing, and low-traffic applications. If you have an NVIDIA GPU with CUDA 11.8+ or an Apple Silicon Mac, Ollama detects and uses it automatically.
Pass the model name as a variable rather than a hardcoded string. Because Ollama’s API mirrors OpenAI’s, the only thing that changes between models is the model parameter in your chat call. You can swap llama3.2 for mistral or codellama in one place. For LlamaIndex-based applications, update Settings.llm and rebuild your index if the embedding model changes.
It depends on your traffic volume. A Python RAG application using LlamaIndex backed by Ollama works well for internal tools, single-user applications, or low-traffic services. For anything that needs to scale horizontally or serve dozens of concurrent users, you’d want to pair it with a dedicated inference backend. But for most RAG applications teams actually build — internal knowledge bases, document Q&A tools, developer assistants — Ollama is more than sufficient.
You now have the full path: install Ollama, pull a model, call it from Python with the native SDK or the OpenAI-compatible endpoint, and wire it into a LlamaIndex RAG pipeline with local embeddings. None of that required GPU drivers on day one or a cloud account.
From here, the two most valuable directions are Modelfile customization — which lets you bake in system prompts and model parameters so your Python code stays clean — and running Ollama behind a lightweight proxy if you need multi-model routing. Python local LLM development with Ollama scales further than most people expect; if you eventually hit throughput limits, that’s when benchmarking against vLLM becomes worth the setup time.
Start with the LlamaIndex RAG example. Run it against a real document you care about. The first time a fully local model answers a specific question about your own files, it clicks.
Tell us about your product, platform, or business software requirement. We will suggest the right team structure, roadmap, and engagement model within 48 hours.