# How do I use Ollama?

> Ollama runs in the background, keeps a library of models and serves an API on your machine. Watch its default context length, which is too small for agents.

Ollama is a free, open-source runtime that runs as a background service. You pull models by name, run them from the terminal, and any app on your machine can use them through an API on port 11434.

## A background service with an API

Install it from [ollama.com](https://ollama.com). On Mac and Windows it installs a small app that starts the service at login. On Linux the install script sets it up as a system service.

**macOS**

Download the app from ollama.com and drag it to Applications. Or use Homebrew:

```bash
brew install ollama
```

**Windows**

Download and run the installer from ollama.com. The service starts in the system tray.

**Linux**

```bash
curl -fsSL https://ollama.com/install.sh | sh
```

Once it runs, the service listens on `http://localhost:11434`. It speaks its own API and the OpenAI-compatible one at `/v1`. Chat apps, coding tools and your own scripts all connect there. [How do I call a local model from code?](https://localagi.co/local-api) has the code.

Under the hood Ollama runs its own engine, built on the same ggml library as [llama.cpp](https://localagi.co/llama-cpp), and reads GGUF files. You never handle the files directly. Ollama stores them in its own folder by name.

## Pull, run, list, remove

Five commands cover daily use.

```bash
ollama pull qwen3:8b      # download a model
ollama run qwen3:8b       # chat in the terminal, pulls first if needed
ollama list               # models on disk, with sizes
ollama ps                 # models loaded in memory right now
ollama rm qwen3:8b        # delete a model from disk
```

The part after the colon is a tag. It picks the size and sometimes the [What is quantization?](https://localagi.co/quantization). Each model's page on ollama.com lists its tags.

With no tag you get `latest`, which is usually a 4-bit build of a mid-sized variant.

Type `/bye` to leave a chat. `ollama stop qwen3:8b` unloads a model without deleting it.

**Try it.** 

```bash
ollama run qwen3:8b --verbose
```

Ask it anything. After each answer, `--verbose` prints the prompt eval rate and eval rate, which are your prefill and decode speeds.

`ollama ps` is worth running whenever things feel slow. Its processor column shows how much of the model sits on the GPU. Anything other than 100% GPU means part of the model runs on the CPU. [Why is my GPU not being used?](https://localagi.co/gpu-not-used) covers what to do.

## The Modelfile

A Modelfile is a short text file that builds a new model from an existing one. Use it to bake in settings such as context length or a system prompt.

```text
FROM qwen3:8b
PARAMETER num_ctx 32768
PARAMETER temperature 0.6
SYSTEM You are a concise assistant. Answer in plain English.
```

Build it and run it under a new name:

```bash
ollama create qwen3-32k -f Modelfile
ollama run qwen3-32k
```

The new model shares the weights with the original, so it costs almost no disk space. `ollama show qwen3:8b --modelfile` prints the Modelfile of any model you have, which is a good starting point. [Should I touch temperature and the other sliders?](https://localagi.co/sampling) explains temperature and the other `PARAMETER` options.

## Context length

Ollama's default context is about 4K tokens on most machines. That is enough for a short chat. It is far too small for coding agents and document work, which need 32K tokens or more.

When the conversation outgrows the window, Ollama drops the oldest tokens without telling you. The model forgets your instructions, loses the file it was editing, or loops. This is the most common reason an agent "works in the cloud but not locally". [How much can the model keep in mind?](https://localagi.co/context-window) explains why.

There are three ways to raise it.

1. For every model: Set the `OLLAMA_CONTEXT_LENGTH` environment variable before the service starts, for example to `32768`. Restart Ollama after changing it.
2. For one model, permanently: Use a Modelfile with `PARAMETER num_ctx`, as above.
3. For one session: Inside `ollama run`, type `/set parameter num_ctx 32768`.

API clients can also send `num_ctx` in the request options on Ollama's native API.

**The catch.** 
A longer context costs memory. The KV cache grows with every token of context, and a model that fit at 4K can spill onto the CPU at 64K. Check `ollama ps` after raising it, and if the GPU share drops, pick a smaller context or a smaller model.

## Launching coding tools

Ollama has a `launch` command that sets up a coding tool to use a local model and starts it. It saves you editing the tool's config by hand.

```bash
ollama launch
```

Whatever starts the tool, the context rule above still applies. A coding agent on a 4K window fails in confusing ways. [Can I run a coding agent on a local model?](https://localagi.co/coding-agents) covers which models hold up.

## Where it trades speed for convenience

Ollama chooses settings for you, and the choices favour "it works" over "it is fast".

- Engine age: Ollama's engine does not track every daily llama.cpp change. New models and faster kernels can reach llama.cpp first.
- Hidden settings: Batch size, thread count and GPU layer split are picked automatically. You can override some, but not all, through Modelfile parameters and environment variables.
- Mac format: On a Mac, Ollama runs GGUF on Metal. An MLX build in [MLX](https://localagi.co/mlx) or [LM Studio](https://localagi.co/lm-studio) is often faster on the same machine.
- Unloading: Models unload after a few idle minutes by default. The next request waits for a reload. Set `OLLAMA_KEEP_ALIVE` to keep them in memory longer.

**Rule of thumb.** 
Use Ollama when you want models available to every app with no fuss. Drop to llama.cpp when you need the last bit of speed or a setting Ollama does not expose.

## Go deeper

### Useful environment variables

The service reads its settings from environment variables at startup. These are the ones that change behaviour most.

| Variable | What it does |
| --- | --- |
| `OLLAMA_HOST` | Address to listen on. Set `0.0.0.0` to serve other machines on your network |
| `OLLAMA_CONTEXT_LENGTH` | Default context for every model |
| `OLLAMA_KEEP_ALIVE` | How long an idle model stays loaded. `-1` keeps it loaded |
| `OLLAMA_NUM_PARALLEL` | Requests served at once per model |
| `OLLAMA_FLASH_ATTENTION` | `1` turns on flash attention |
| `OLLAMA_KV_CACHE_TYPE` | `q8_0` or `q4_0` to shrink the KV cache. Needs flash attention |

On a Mac, the app ignores variables set in your shell profile. Set them with `launchctl setenv` and restart the app. On Linux, add them to the systemd service with `systemctl edit ollama`. 

### Running models from Hugging Face

Ollama can pull GGUF files straight from Hugging Face by repository name, with a quantization tag.

```bash
ollama run hf.co/bartowski/Qwen_Qwen3-8B-GGUF:Q4_K_M
```

That opens up any GGUF upload, not only the ones in Ollama's own library. 

### Sharing it on your network

Setting `OLLAMA_HOST=0.0.0.0` exposes the API to every device on your network, with no password. Only do that on a network you trust. [How do I share one model with my household or office?](https://localagi.co/sharing-a-model) and [Is local AI actually private?](https://localagi.co/privacy-and-security) cover safer ways.
