AI and machine learning · Beginner · 16 min
Run a language model on your own machine with Ollama
By Padding · Updated 18 Nov 2026
Ubuntu 26.04 · Ollama 0.40.18 · qwen3:0.6b
You will have Ollama 0.40 serving a small model on 127.0.0.1:11434, answering from the command line and over its REST API, with a custom model of your own built from a Modelfile.
On this page
Ollama runs open-weight language models on your own hardware. It is one program that downloads models, loads them into memory, and answers prompts from a command line and from an HTTP API on port 11434. Nothing you type leaves the machine, there is no per-token bill, and once a model is downloaded it works offline.
In this tutorial you install Ollama 0.40 on Ubuntu 26.04, confirm it only listens on the machine itself, pull qwen3:0.6b (a 0.6-billion-parameter model of about 520 MB that runs on an ordinary CPU), talk to it from the terminal, call the REST API with curl, change how long it stays in memory and how much text it can hold, and build a custom model with a fixed system prompt. The last step explains how model size and quantisation decide how much RAM you need and how fast answers come.
A small model is the right choice to learn on: it downloads quickly and runs on a laptop with 8 GB of RAM. It is also wrong more often than a large one, so treat its answers as drafts that a person checks.
Step 1 · Install Ollama
Install curl and jq first. You use curl to download the installer and call the API, and jq to read the JSON that comes back:
sudo apt update
sudo apt install -y curl jqThe official install script puts the ollama binary in /usr/local/bin or /usr, creates an ollama system user, and starts a systemd service that runs ollama serve at boot. Setting OLLAMA_VERSION pins the release, so a reinstall next month gives you the same version this tutorial uses. Read the script first if you prefer: open https://ollama.com/install.sh in a browser.
curl -fsSL https://ollama.com/install.sh | OLLAMA_VERSION=0.40.18 shThe service writes its logs to the journal. If anything below fails, look there first:
sudo systemctl status ollama --no-pager
journalctl -e -u ollamaOn a Mac, download the app from ollama.com/download and open it. It runs the same server on the same port, adds a menu bar icon, and puts the ollama command on your path. Every command from the next step on works the same in macOS Terminal, except ss and systemctl.
Ollama installed this way is not an apt package, so apt upgrade will not update it. To upgrade, run the install script again with a newer OLLAMA_VERSION from the releases page on GitHub.
Check it worked
ollama --versionIt prints something like ollama version is 0.40.18. systemctl status shows the service as active (running).
Step 2 · Check that it only listens on this machine
By default Ollama binds to 127.0.0.1:11434, the loopback address, so only programs on the same machine can reach it. The API has no passwords or keys: anyone who can reach the port can run models, download new ones and delete yours. Confirm the address before you go further:
ss -ltn 'sport = :11434'
curl -fsS http://127.0.0.1:11434/The server's address is controlled by the OLLAMA_HOST environment variable. Do not set it to 0.0.0.0 on a machine with a public IP address. A model server open to the internet will be found by internet scanners quickly and used for free compute. If you need to reach Ollama on a server from your laptop, use an SSH tunnel, which keeps the port closed and reuses your SSH login:
# On your laptop. Replace you@your-server with your own login.
ssh -N -L 11434:127.0.0.1:11434 you@your-serverWhile that runs, http://127.0.0.1:11434 on your laptop reaches the server. If several people or apps need it over a network, put a reverse proxy with authentication and TLS in front of it, and keep Ollama itself on loopback.
Check it worked
The ss line shows LISTEN with a local address of 127.0.0.1:11434, not 0.0.0.0:11434 or *:11434. The curl call prints Ollama is running.
Step 3 · Pull a model and chat on the command line
Download qwen3:0.6b. It is about 520 MB, so on prepaid mobile data start it on Wi-Fi or at an off-peak bundle time. The download resumes if the connection drops, so run the same command again:
ollama pull qwen3:0.6b
ollama listStart a chat. Type a question at the >>> prompt and press Enter. /? lists the chat commands and /bye ends the session:
ollama run qwen3:0.6bQwen3 is a thinking model: before it answers, it writes out its reasoning, which Ollama shows first. That makes answers slow on a CPU. For a one-off question you can pass the prompt as an argument, turn thinking off and ask for timing figures:
ollama run --think=false --verbose qwen3:0.6b "In one sentence, what is a power bank?"Try a question in a language you speak other than English, such as Yoruba, Hausa, Swahili or French. Small models are much weaker outside English and Chinese, so check what comes back carefully before you rely on it.
Check it worked
ollama list shows qwen3:0.6b with a size of something like 523 MB. The one-off command prints a sentence about a portable battery, followed by statistics such as eval rate: ... tokens/s. That number is how fast the model writes on your hardware.
Step 4 · Call the REST API with curl
Everything the command line does goes through the HTTP API, and any program can use it. Create a folder for this tutorial's files:
mkdir -p ~/ollama-demo
cd ~/ollama-demo/api/tags lists the models you have downloaded:
curl -fsS http://127.0.0.1:11434/api/tags | jq -r '.models[].name'/api/generate takes one prompt and returns one answer. By default the answer arrives as a stream of JSON lines, one per few tokens. "stream": false waits and returns a single JSON object, which is easier to read with jq. "think": false skips the reasoning, and temperature 0 with a fixed seed makes the same prompt give the same answer each time:
curl -fsS http://127.0.0.1:11434/api/generate -d '{
"model": "qwen3:0.6b",
"prompt": "What is the capital of Kenya? Answer with the city name only.",
"stream": false,
"think": false,
"options": {"temperature": 0, "seed": 42}
}' > generate.json
jq -r '.response' generate.json
jq -r '"\(.eval_count) tokens in \(.eval_duration / 1e9) s"' generate.json/api/chat takes a list of messages with roles, so you can send a system prompt and earlier turns of a conversation. Your program keeps the history and sends all of it with each request; the server does not remember past calls:
curl -fsS http://127.0.0.1:11434/api/chat -d '{
"model": "qwen3:0.6b",
"messages": [
{"role": "system", "content": "You answer in at most two short sentences."},
{"role": "user", "content": "How do I keep rice from sticking to the pot?"}
],
"stream": false,
"think": false,
"options": {"temperature": 0, "seed": 42}
}' > chat.json
jq -r '.message.role, .message.content' chat.jsonCheck it worked
test "$(jq -r '.done' generate.json)" = true
test -n "$(jq -r '.response' generate.json)"
test "$(jq -r '.message.role' chat.json)" = assistant
test -n "$(jq -r '.message.content' chat.json)"
echo "API calls OK"/api/tags printed qwen3:0.6b. The generate call should print Nairobi and a token count with a time in seconds; a small model can still get facts wrong, which is why you check. The chat call prints assistant and a short answer of one or two sentences. The last command prints API calls OK.
Step 5 · Set keep-alive and context length
Loading a model from disk into memory takes a few seconds, so Ollama keeps it loaded for 5 minutes after the last request and then frees the RAM. ollama ps shows what is loaded and when it will be unloaded:
ollama psThe keep_alive field changes that for one request. "30m" keeps the model loaded for half an hour, -1 keeps it until the server stops, and 0 unloads it straight after the answer. Use a long value on a machine that does nothing else, and 0 on a laptop that needs its memory back.
The context length is how many tokens of prompt and answer the model can see at once. On a CPU-only machine or a GPU with less than 24 GiB of memory, Ollama's default is 4096 tokens, roughly 3,000 English words. A longer context lets you paste longer documents but uses more RAM. Set it per request with num_ctx in options:
curl -fsS http://127.0.0.1:11434/api/generate -d '{
"model": "qwen3:0.6b",
"prompt": "Reply with the word ready.",
"stream": false,
"think": false,
"keep_alive": "30m",
"options": {"num_ctx": 8192, "temperature": 0, "seed": 42}
}' | jq -r '.response'
ollama psTo change the defaults for every request, set environment variables on the service. sudo systemctl edit ollama.service opens an override file; add these lines, save, then restart:
[Service]
Environment="OLLAMA_KEEP_ALIVE=30m"
Environment="OLLAMA_CONTEXT_LENGTH=8192"sudo systemctl daemon-reload
sudo systemctl restart ollamaollama stop qwen3:0.6b unloads a model at any time without deleting it.
Check it worked
After the second request, ollama ps lists qwen3:0.6b with 100% CPU in the PROCESSOR column on a machine without a supported GPU, and an UNTIL value about 30 minutes away. The SIZE value is larger than the 523 MB download, because the bigger context needs memory of its own.
Step 6 · Build a custom model with a Modelfile
A Modelfile describes a model in a few lines: which model to start from, a system prompt, and default parameters. ollama create builds it into a new model name. It reuses the weights you already have, so it costs almost no disk space. This one makes a kitchen helper that answers briefly in metric units:
cat > Modelfile <<'EOF'
FROM qwen3:0.6b
SYSTEM """You are a kitchen helper. Answer in at most three short sentences. Use metric units and degrees Celsius. If you are not sure, say so."""
PARAMETER temperature 0.3
PARAMETER num_ctx 8192
EOF
ollama create kitchen-helper -f Modelfile
ollama list
ollama show kitchen-helper --modelfile > shown.txt
grep -q 'kitchen helper' shown.txt
grep -i 'num_ctx' shown.txtUse it like any other model, from ollama run kitchen-helper or from the API:
curl -fsS http://127.0.0.1:11434/api/generate -d '{
"model": "kitchen-helper",
"prompt": "How long should I boil an egg for a soft yolk?",
"stream": false,
"think": false
}' | jq -r '.response'Keep the Modelfile in version control next to the code that calls it. It is a short, reviewable record of exactly how the model is set up.
Check it worked
ollama create ends with success. The ollama list line now shows kitchen-helper as well as qwen3:0.6b. The grep line prints PARAMETER num_ctx 8192, and the egg answer is short and gives a time in minutes.
Step 7 · Work out what your hardware can run
How much memory a model needs comes from two numbers on its page: the parameter count and the quantisation.
Each parameter is a number. Stored at full 16-bit precision, a parameter takes 2 bytes, so an 8-billion-parameter model needs about 16 GB just for its weights. Quantisation stores each number in fewer bits. Most models in the Ollama library default to about 4 bits per parameter (tags such as q4_K_M), which cuts that to roughly 5 GB with a small loss in quality. q8_0 keeps more quality at about double the size.
A working rule for a CPU-only machine:
- RAM needed is about the download size, plus 1 to 2 GB for the context and the program itself, plus whatever else the machine is running.
- 8 GB of RAM: models up to about 4 billion parameters at 4-bit, such as
qwen3:4b(2.5 GB). - 16 GB: up to about 8 billion parameters, such as
qwen3:8b(5.2 GB). - Speed falls as size grows, because each new token reads every weight from memory. A 0.6b model can write tens of tokens per second on a recent laptop; an 8b model on the same CPU may manage only a few.
See what your downloaded model is made of:
ollama show qwen3:0.6bCompare speeds yourself with --verbose on two sizes before choosing one for a project. On a low-RAM laptop or a cheap VPS, a small model that answers in two seconds is often more useful than a large one that answers in a minute.
Check it worked
ollama show prints the architecture, a parameter count a little under one billion, a context length of something like 40960, and a quantisation line such as Q4_K_M. Take its download size from ollama list and add 1 to 2 GB, as in the rule above: for qwen3:0.6b that comes to under 2.5 GB.
Step 8 · Clean up
Remove the custom model, the downloaded model and the demo folder. Ollama stays installed, which the related tutorials assume:
ollama rm kitchen-helper
ollama rm qwen3:0.6b
cd ~
rm -rf ~/ollama-demo
ollama listTo remove Ollama itself, follow the uninstall section of the Linux page at docs.ollama.com/linux: it stops and disables the service, then deletes the binary, /usr/lib/ollama, the models in /usr/share/ollama and the ollama user.
Check it worked
ollama list prints only its header row, with no models under it.
Next steps
- Call the same server from Python code through its OpenAI-compatible endpoint: call OpenAI-compatible APIs from Python.
- Run a GGUF file directly, without Ollama in between: serve a quantised GGUF model with llama.cpp.
- Measure whether examples in the prompt help a small local model: improve LLM answers with few-shot examples.
- Compare with a hosted model: call an LLM API from Python.
Part of these paths
Related tutorials
- Call any OpenAI-compatible API from PythonYou will point the openai Python SDK at a local Ollama model through its /v1 endpoint, list models, send and stream chat completions, read token usage and handle errors, with the base URL, key and model kept in environment variables so a provider switch is a config change.
- Serve a quantised GGUF model with llama.cppYou will build llama.cpp v1.1.0 for CPU, compare a Q4_K_M and a Q8_0 copy of the same small model for size, memory and speed, and answer chat requests from its OpenAI-compatible server behind an API key.
- How to Call a Large Language Model API from PythonYou will store an API key safely, install Anthropic's Python SDK 2.8.0, send Claude Sonnet 5.5 a first request, give it instructions and a conversation history, handle the errors an API call can raise, and work out what each call costs.
Need this done for you?
Padding does this work for growing companies, from the first diagnosis to a system that keeps working.
Get new tutorials