Course contents · Lesson 1 of 16

Run an LLM Locally with Ollama, Step by Step

Setup · Lesson 1 of 16 · 11 min Hands-on

You want to try an idea with an LLM, but a hosted API first asks for an account, an API key and often a card, and your text leaves your machine. This lesson shows you how to run an LLM locally with Ollama in Docker, so you can build the rest of this course on your own laptop, free and offline.

Why run a model on your own machine

An LLM, a large language model, is a program that writes text by predicting the next token, a short piece of a word, again and again; What Is an LLM? explains how. A hosted LLM runs on a company's servers, and you reach it over the internet.

Running one yourself is free, with no bill per request. It is private, because your prompts never leave the laptop. It works offline once the model is downloaded. And there is no API key, the secret password a hosted service issues, to create, store or leak.

You give up two things. Speed: a laptop, especially without a graphics chip (a GPU) doing the maths, writes more slowly than a data-centre server. Size: the biggest models need far more memory than a laptop has, so you run smaller ones that make more mistakes.

What Ollama is

Ollama is an open-source program that downloads open-weight models and runs them. An open-weight model is one whose trained numbers, its weights, are published for anyone to download. Meta's Llama 3.2, used in this course, is one, under Meta's own licence.

Ollama is a server, a program that waits for requests and answers them. It listens on port 11434, a numbered door on the machine, and speaks HTTP with JSON in and out, so curl, Python or any other HTTP client can use it. The ollama command is also a client for it.

The flowchart follows one request. Notice the middle step: the first request loads the model from disk into memory.

Flowchart: your terminal runs curl, which sends POST /api/generate with JSON to localhost port 11434, published by Docker, to the Ollama server in the container. The server finds llama3.2:3b in the named volume and loads it into memory, where it writes one token at a time on the CPU. Tokens stream back to the terminal as JSON, or as one reply with stream false. Nothing leaves the laptop; the first request is slower while the model loads.
The round trip stays on your laptop; only the first request loads the model.

Start Ollama with Docker Compose

Docker gives everyone the same setup on macOS, Windows and Linux; Docker Compose Basics is a refresher. The checklist shows this lesson's four commands and what each should print. The first, docker compose version, confirms Docker is installed.

Checklist of four steps: docker compose version prints v2 or newer; docker compose up -d in 01-ollama-setup starts the ollama container; docker compose exec ollama ollama pull llama3.2:3b shows progress bars then success; curl localhost:11434/api/generate returns JSON with a response field. Then Ollama is ready on port 11434. An error is usually the port, memory or the model name.
Four commands, each with a result to check before the next.

Get the labs with git clone https://github.com/datsabk/course-labs.git and open course-labs/building-llm-apps/01-ollama-setup, also on GitHub. Its docker-compose.yml, without comments, is short:

services:
  ollama:
    image: ollama/ollama:0.40.19
    ports:
      - "127.0.0.1:11434:11434"
    volumes:
      - ollama-models:/root/.ollama

volumes:
  ollama-models: {}

image pins Ollama 0.40.19, so everyone runs the same version. ports publishes the container's port 11434 on your laptop at 127.0.0.1 only, so other machines cannot reach it. volumes mounts a named volume, storage Docker keeps outside the container, at /root/.ollama, where Ollama saves models. Remove the container and the models stay.

Box diagram: your laptop contains Docker, which runs the Ollama container from ollama/ollama:0.40.19 with ollama serve on port 11434. The named volume ollama-models sits outside the container and is mounted at /root/.ollama. Docker publishes the port as 127.0.0.1:11434, which your terminal reaches as localhost:11434. Other machines cannot connect.
The container is disposable; the named volume keeps your 2 GB download.

Start it in the background (-d, detached, gives your prompt back), then check it:

docker compose up -d
docker compose ps
curl http://localhost:11434/api/version

The first up also downloads the Ollama image. docker compose ps should list 01-ollama-setup-ollama-1 as Up with 127.0.0.1:11434->11434/tcp under PORTS, and curl should print {"version":"0.40.19"}. If curl says connection refused, the server is not up; docker compose logs ollama shows why.

Pull a small model and check your memory

A model's size is counted in parameters, another name for its weights. llama3.2:3b has about 3 billion. Ollama downloads a compressed copy that stores each number in fewer bits, called quantization, so the model is about 2.0 GB.

More parameters usually means better answers, at a price. Every parameter sits in memory, and every token written uses all of them, so bigger models need more RAM and answer more slowly. A 3B model fits a typical 8 GB laptop, replies within seconds on a CPU and is good enough for this course; a 1B model is faster but makes more mistakes.

Plan for about 4 GB of free memory while the model answers: the weights plus working memory for the text. On Docker Desktop, containers share a memory limit set under Settings > Resources. Check it, then pull the model:

docker info | grep "Total Memory"
docker compose exec ollama ollama pull llama3.2:3b

docker info prints a line such as Total Memory: 7.653GiB; give Docker at least 6 GB. docker compose exec ollama runs a command inside the running container. The pull prints pulling manifest, a progress bar per file, then verifying sha256 digest, writing manifest and success. If it stops, run it again.

Your first prompt, two ways

Chat with ollama run

The quickest test is the built-in chat client. It runs inside the container, so no published port is involved:

docker compose exec ollama ollama run llama3.2:3b

You should see the prompt >>> Send a message (/? for help). Type a question and the answer appears word by word; /bye or Ctrl+D leaves. If a model is missing, ollama run downloads it first; /api/generate does not.

Call the API with curl

This is how your code will talk to it, over port 11434. /api/generate takes a model and a prompt. "stream": false asks for one complete reply instead of the default, a stream of small JSON pieces sent as tokens are written:

$ curl localhost:11434/api/generate -d '{"model":"llama3.2:3b","prompt":"Say hello in five words","stream":false}'
{"model":"llama3.2:3b","response":"Hello, how are you today?",
 "total_duration":5214033375,"load_duration":2895467125,"eval_count":8, …}

The real reply is one long line, trimmed here, with illustrative numbers; your words will differ. Durations are in nanoseconds: a total_duration of 5214033375 is about 5.2 seconds, and load_duration, loading the model into memory, took 2.9 of them. eval_count counts the tokens written.

Send it again and it is faster, because the model stays loaded, 5 minutes by default. It is still slower than a hosted chatbot: the Ollama FAQ notes that Docker Desktop on macOS has no GPU passthrough, so the model runs on the CPU; docker compose exec ollama ollama ps shows 100% CPU. The Ollama Mac app can use the GPU, but Docker keeps every setup identical.

Housekeeping and the three common errors

A few commands, run in the lab folder, keep the setup tidy:

docker compose exec ollama ollama list   # downloaded models
docker compose stop                      # stop, keep the container
docker compose start                     # start it again
docker compose down                      # remove it, keep the volume
docker compose down -v                   # also delete the models

ollama list should show llama3.2:3b with a SIZE of 2.0 GB. The files sit in /root/.ollama/models inside the container, which is the volume 01-ollama-setup_ollama-models on your machine: Compose puts the folder name in front.

Port already in use

up fails with an error ending in address already in use or port is already allocated. Usually an Ollama app or Linux service already holds port 11434. Quit the app, or run sudo systemctl stop ollama on Linux; lsof -i :11434 names whatever holds the port.

Out of memory

The reply is an error such as model requires more system memory (… GiB) than is available (… GiB), or one saying the runner process was killed. Raise Docker's memory limit, close heavy programs, or use llama3.2:1b, about 1.3 GB.

Model not found

{"error":"model 'llama3.2' not found"}: the name must match ollama list exactly, tag included. llama3.2 means llama3.2:latest, which you never pulled. Use llama3.2:3b, or pull the model first.

Check yourself

You run docker compose down, then up -d. Must you pull llama3.2:3b again? And after down -v?

Not after down: the model lives in the named volume, which down keeps. After down -v, yes, because -v deletes the volume.

Why is the first curl after a start much slower than the second?

The first loads about 2 GB from the volume into memory, shown as load_duration. The model then stays loaded for 5 minutes by default, so the next request skips that step.

A classmate on your Wi-Fi tries curl http://your-laptop-ip:11434 and gets no answer. Why?

The port is published on 127.0.0.1 only, the laptop's own loopback address, so other machines cannot connect. Good, because the API has no password.