n8n can use a language model that runs on the same server as n8n. You add Ollama next to n8n in Docker Compose, point n8n's Ollama credential at http://ollama:11434, and the AI nodes work without an API key or a per-token bill. On a 4 vCPU, 8 GB server a 3B model answers at about 15 tokens a second, which is enough for sorting emails, extracting fields and short agent tasks.
This tutorial starts from the n8n stack in How to Self-Host n8n on Ubuntu 24.04 with Docker Compose, adds Ollama to it, and builds two workflows: an email triage chain that returns clean JSON, and an AI Agent that uses a tool. Every command and timing below was run on a Raff Ubuntu 24.04 server on 19 November 2026.
What you need
- n8n running with Docker Compose. The commands use the layout from our n8n tutorial: the Compose file is
/opt/n8n/compose.yamland the n8n service is calledn8n. If your file or service has another name, use yours. - SSH access with sudo.
- At least 4 vCPU and 8 GB of RAM. Ollama runs models on the CPU here, and the model has to fit in memory next to n8n.
- About 10 GB of free disk: the Ollama image is about 8.5 GB unpacked, and a 3B model is about 2 GB.
How big a server you need
These are the numbers from the test server: Raff Cost-Optimized, 4 vCPU and 8 GB, previous-generation AMD EPYC with DDR4 and local NVMe, CPU only.
| Model | Download | Memory when loaded | Output speed | First answer after loading |
|---|---|---|---|---|
| qwen2.5:3b | 1.9 GB | 2.2 GB | about 16 tokens/s | 14 s |
| llama3.2:3b | 2.0 GB | 2.6 GB | about 15 tokens/s | 13 s |
| qwen2.5:7b | 4.7 GB | 5.1 GB | about 7 tokens/s | 22 s |
n8n itself used 450 to 550 MB. So:
- 4 vCPU and 8 GB runs n8n with one 3B model comfortably. A 7B model also fits, but only on its own, and it answers at half the speed.
- 8 vCPU and 16 GB is the size for a 7B model in daily use, or for two models at once.
On Raff in US East, 4 vCPU and 8 GB is $19.99 a month on Cost-Optimized or $33.99 on General Purpose, and 8 vCPU and 16 GB is $39.99 or $71.99. That is the whole AI bill: there is no charge per token or per call. What the rest of an n8n setup costs is in n8n self-hosted pricing.
Step 1: Add Ollama to the Compose file
Open the Compose file:
sudo nano /opt/n8n/compose.yaml
Add an ollama service under services:, next to postgres and n8n:
ollama: image: ollama/ollama:0.40.16 restart: unless-stopped environment: OLLAMA_MAX_LOADED_MODELS: 1 volumes: - ollama_data:/root/.ollama
Then add its volume under volumes: at the end of the file:
ollama_data: name: n8n_ollama_data
Two choices here matter:
- No
ports:section. n8n reaches Ollama over the Compose network by its service name, so Ollama never needs a port on the server's public address. Ollama has no login of its own: a published port 11434 would let anyone on the internet use your server's CPU. OLLAMA_MAX_LOADED_MODELS: 1. Ollama keeps a model in memory for 5 minutes after it answers. On 8 GB, two models loaded at once can leave no room for a third: in our test, loading the 7B model while two 3B models were still in memory failed withllama-server process has terminated: signal: killed. With this setting Ollama unloads one model before it loads the next.
Step 2: Start Ollama and download a model
Start the new service. The n8n and postgres containers keep running:
cd /opt/n8n sudo docker compose up -d ollama
The first start downloads the Ollama image, about 50 seconds on our server. Check that it runs:
sudo docker compose exec ollama ollama --version
ollama version is 0.40.16
Download a model. qwen2.5:3b is the one this tutorial uses: it was the fastest in our test and it handles tool calls, which the AI Agent in Step 6 needs.
sudo docker compose exec ollama ollama pull qwen2.5:3b sudo docker compose exec ollama ollama list
NAME ID SIZE MODIFIED qwen2.5:3b 357c53fb659c 1.9 GB 5 seconds ago
Confirm that nothing listens on port 11434 on the server itself:
sudo ss -ltnp | grep 11434 || echo "11434 is not published"
11434 is not published
Step 3: Check that n8n can reach Ollama
Ask Ollama for its version from inside the n8n container. This is the address n8n will use:
sudo docker compose exec n8n wget -qO- http://ollama:11434/api/version
{"version":"0.40.16"}
If this works, the connection between the two containers is fine. For comparison, the same request to localhost fails, because inside the n8n container localhost is the n8n container itself:
sudo docker compose exec n8n wget -qO- http://localhost:11434/api/version
wget: can't connect to remote host: Connection refused
Step 4: Create the Ollama credential in n8n
In n8n, open Credentials, create a new credential and choose Ollama. n8n fills Base URL with http://localhost:11434. Replace it:
| Field | Value |
|---|---|
| Base URL | http://ollama:11434 |
| API Key | Leave empty |
Save the credential. n8n tests the connection when you save.
The default localhost address is the most common reason n8n cannot reach Ollama in Docker. With it, a workflow fails with connect ECONNREFUSED ::1:11434. Use the Compose service name instead. If Ollama runs directly on the server rather than in Compose, n8n in Docker still cannot use localhost; the cleanest fix is to move Ollama into the same Compose file as above.
Step 5: Build an email triage workflow with structured output
This workflow reads a customer email and returns the fields you would act on: a category, the order number, a one-line summary and whether a reply is needed. It uses four nodes:
- Manual Trigger (it shows as "When clicking 'Execute workflow'"), so you can run it from the editor. Later you can swap it for an email or webhook trigger.
- Edit Fields (Set) with one string field,
email, holding a test email. - Basic LLM Chain.
- Two sub-nodes on the chain: Ollama Chat Model and Structured Output Parser.
Set the Basic LLM Chain like this:
- Source for Prompt (User Message): Define below.
- Prompt (User Message):
Read this customer email and fill in the fields. category must be one of: * complaint: something went wrong with an order or a service (missing, late, damaged, wrong) * question: the customer asks for information and nothing went wrong * order: the customer wants to buy something new Email: {{ $json.email }}
- Require Specific Output Format: on.
Set the Ollama Chat Model:
- Credential: the Ollama credential from Step 4.
- Model:
qwen2.5:3b. The list shows the models you downloaded. - Options: add Sampling Temperature and set it to 0, and add Output Format and choose JSON.
Set the Structured Output Parser to Generate From JSON Example with this example:
{ "category": "complaint", "order_number": "48213", "summary": "One chair missing, box damaged.", "reply_needed": true }
Run the workflow with this test email in the Edit Fields node:
Hello, I ordered two of the blue office chairs last Tuesday (order 48213) and only one arrived. The box was also damaged. Can you send the second chair this week, and do I need to return the damaged box? Thanks, Maria
The chain returns:
{ "output": { "category": "complaint", "order_number": "48213", "summary": "The customer ordered two blue office chairs, but only one arrived and the box was damaged. The customer needs to know if they need to return the damaged box.", "reply_needed": true } }
In our test it took about 6 seconds of model time per run, and the result was the same in three runs out of three. Two settings made the difference:
- Output Format: JSON. Without it, the model wrapped its answer in a Markdown code block and left it unclosed, and the parser stopped with
Model output doesn't fit required format. With JSON on, Ollama only returns valid JSON. - Labels defined in the prompt. With only "category is one of: complaint, question, order", the 3B model called this email a question and the 7B model called it an order. Once each label had a one-line definition, the 3B model chose complaint every time. Small models follow definitions much better than bare labels.
The 7B model was not more accurate here, and it was slower: 11 seconds per run once loaded, 55 seconds when it had to load first.
Step 6: Give an AI Agent a tool
An AI Agent lets the model call tools, for example to look something up or do arithmetic it would otherwise guess. This test uses the built-in Calculator tool.
Add these nodes:
- Manual Trigger.
- AI Agent, with Source for Prompt (User Message) set to Define below and this prompt:
A customer orders 3 office chairs at $129.50 each. Sales tax is 8.25%. Use the calculator to work out the total including tax, then answer with the total only.
Under Options, add System Message: You are a helpful assistant for an office furniture shop. Always use the calculator tool for arithmetic.
3. On the agent: an Ollama Chat Model sub-node (the same credential, qwen2.5:3b; leave Output Format on Default here) and a Calculator tool.
Run it. The agent's answer:
The total cost for the 3 office chairs including 8.25% sales tax is $420.55.
The run log shows the model called the Calculator with (3 * 129.5) * (1 + 0.0825), got 420.55125 back, and answered from that. The whole run took 14 to 18 seconds: the agent calls the model twice, once to choose the tool and once to answer.
Tool calls need a model that supports them. qwen2.5 does; check a model's page on ollama.com/library for the "tools" tag before you use it in an agent.
Step 7: Keep answers fast
- The first answer after a pause is slow. Ollama unloads a model after 5 minutes without use, and loading it again took 5 to 8 seconds for a 3B model. If a workflow runs often, open Options on the Ollama Chat Model and set Keep Alive to a longer time such as
1h. The model then stays in memory, so leave room for it. - Context Length on the Ollama Chat Model defaults to 2048 tokens. That is enough for an email, not for a long document. A larger value uses more memory.
- One model per workflow. With
OLLAMA_MAX_LOADED_MODELS: 1, two workflows that use different models make Ollama swap them, and each swap costs a full load. Pick one model for everything that runs often. - Check what is loaded with
sudo docker compose exec ollama ollama ps.
Back up and update
- The models are in the
n8n_ollama_datavolume. You can leave them out of backups:ollama pulldownloads them again in seconds. Your workflows and the Ollama credential are in n8n's database, which the backup steps in our n8n tutorial already cover. - To update Ollama, change the image tag in
compose.yaml, then runsudo docker compose pull ollamaandsudo docker compose up -d ollama. The models stay in the volume.
Troubleshooting
| What you see | Why | Fix |
|---|---|---|
connect ECONNREFUSED ::1:11434 | The credential's Base URL is http://localhost:11434, the default. Inside the n8n container that is n8n itself | Set Base URL to http://ollama:11434 (Step 4) |
getaddrinfo ENOTFOUND ollama | n8n and Ollama are not in the same Compose project, so the name ollama does not resolve | Put the ollama service in the same compose.yaml as n8n |
Model output doesn't fit required format | The model returned text around the JSON, often an unclosed code block | Set Output Format to JSON on the Ollama Chat Model, and keep the temperature at 0 |
llama-server process has terminated: signal: killed | Not enough free memory to load the model | Set OLLAMA_MAX_LOADED_MODELS: 1, use a smaller model, or move to a server with more RAM |
| The first run takes 15 to 25 seconds, the next ones are fast | The model was being loaded into memory | Expected; use Keep Alive for workflows that run often |
| The model list in the Ollama Chat Model is empty | No model downloaded yet, or the credential cannot connect | Run ollama pull (Step 2) and check the credential (Step 4) |
Conclusion
n8n and Ollama on one server give you AI steps in your workflows with a fixed monthly cost and nothing sent to an outside AI provider. On 4 vCPU and 8 GB, a 3B model such as qwen2.5:3b is the practical choice: it fits next to n8n, answers in seconds, returns clean JSON with Output Format set to JSON, and calls tools from an AI Agent. Define your labels in the prompt, keep Ollama off the public network, and size up to 8 vCPU and 16 GB if you need a 7B model every day.
If you also want a chat window for the same models, see How to Install Open WebUI with Ollama on Ubuntu 24.04.
Sources
All read 2026-11-19.
- n8n, Ollama credentials: https://docs.n8n.io/integrations/builtin/credentials/ollama/
- n8n, Ollama Chat Model node: https://docs.n8n.io/integrations/builtin/cluster-nodes/sub-nodes/n8n-nodes-langchain.lmchatollama/
- Ollama, Docker: https://docs.ollama.com/docker
- Ollama, FAQ (keeping models loaded, concurrency settings): https://docs.ollama.com/faq
- Ollama image: https://hub.docker.com/r/ollama/ollama
- Model page: https://ollama.com/library/qwen2.5
- Raff prices: live pricing API, US East (https://api.rafftechnologies.com/api/v1/public/pricing/vm?region=us-east)
