How to Connect n8n to Ollama for Local AI Workflows

Aybars Altınyay
Aybars Altınyay
Platform Engineer & Technical Writer
IntermediateUpdated Nov 19, 202614 min read~30 minutes total
n8nOllamaDocker ComposeUbuntu 24.04Local LLMAI AgentSelf-Hosted
Last reviewed:
Tutorial
How to Connect n8n to Ollama for Local AI Workflows
rafftechnologies.com/learn
On this page

Prerequisites

  • n8n running with Docker Compose on Ubuntu 24.04 (our n8n tutorial), SSH sudo access, at least 4 vCPU and 8 GB RAM, and about 10 GB of free disk for models

Run your AI workflows on your own server, with no per-token bill.

Tested on 4 vCPU and 8 GB RAM: $33.99 a month

  1. 1Pick a Linux imageUbuntu, Debian and more, with your SSH key added.
  2. 2Choose a sizeFrom $4.99 a month. Resize when your app grows.
  3. 3Connect over SSHYour server is up in about 55 seconds.

Ubuntu, Debian and other Linux distributions with full root and SSH access, up and running in about 55 seconds.

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.yaml and the n8n service is called n8n. 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.

ModelDownloadMemory when loadedOutput speedFirst answer after loading
qwen2.5:3b1.9 GB2.2 GBabout 16 tokens/s14 s
llama3.2:3b2.0 GB2.6 GBabout 15 tokens/s13 s
qwen2.5:7b4.7 GB5.1 GBabout 7 tokens/s22 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:

Bash
sudo nano /opt/n8n/compose.yaml

Add an ollama service under services:, next to postgres and n8n:

Yaml
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:

Yaml
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 with llama-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:

Bash
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:

Bash
sudo docker compose exec ollama ollama --version
Text
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.

Bash
sudo docker compose exec ollama ollama pull qwen2.5:3b sudo docker compose exec ollama ollama list
Text
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:

Bash
sudo ss -ltnp | grep 11434 || echo "11434 is not published"
Text
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:

Bash
sudo docker compose exec n8n wget -qO- http://ollama:11434/api/version
Text
{"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:

Bash
sudo docker compose exec n8n wget -qO- http://localhost:11434/api/version
Text
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:

FieldValue
Base URLhttp://ollama:11434
API KeyLeave 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:

  1. 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.
  2. Edit Fields (Set) with one string field, email, holding a test email.
  3. Basic LLM Chain.
  4. 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):
Text
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:

Json
{ "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:

Text
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:

Json
{ "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:

  1. Manual Trigger.
  2. AI Agent, with Source for Prompt (User Message) set to Define below and this prompt:
Text
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:

Text
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_data volume. You can leave them out of backups: ollama pull downloads 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 run sudo docker compose pull ollama and sudo docker compose up -d ollama. The models stay in the volume.

Troubleshooting

What you seeWhyFix
connect ECONNREFUSED ::1:11434The credential's Base URL is http://localhost:11434, the default. Inside the n8n container that is n8n itselfSet Base URL to http://ollama:11434 (Step 4)
getaddrinfo ENOTFOUND ollaman8n and Ollama are not in the same Compose project, so the name ollama does not resolvePut the ollama service in the same compose.yaml as n8n
Model output doesn't fit required formatThe model returned text around the JSON, often an unclosed code blockSet Output Format to JSON on the Ollama Chat Model, and keep the temperature at 0
llama-server process has terminated: signal: killedNot enough free memory to load the modelSet 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 fastThe model was being loaded into memoryExpected; use Keep Alive for workflows that run often
The model list in the Ollama Chat Model is emptyNo model downloaded yet, or the credential cannot connectRun 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.

Was this article helpful?
Aybars Altınyay
Aybars Altınyay
Platform Engineer & Technical Writer

Aybars is a Platform Engineer & Technical Writer at Raff Technologies. He works on Linux VM templates, self-hosted application workflows, and security-focused deployment validation, turning tested infrastructure procedures into clear, practical tutorials for developers and teams.

Run your AI workflows on your own server, with no per-token bill.

Tested on 4 vCPU and 8 GB RAM: $33.99 a month

Ubuntu, Debian and other Linux distributions with full root and SSH access, up and running in about 55 seconds.

14-day money-back guarantee

Frequently asked questions

How do I connect n8n to Ollama?
Run Ollama as a service in the same Docker Compose file as n8n, then create an Ollama credential in n8n with Base URL `http://ollama:11434` instead of the default `http://localhost:11434`. Use that credential in an Ollama Chat Model node attached to a chain or an AI Agent.
Why does n8n say ECONNREFUSED when it connects to Ollama?
The credential still uses `http://localhost:11434`. Inside the n8n container, localhost is the n8n container itself, so the connection is refused (`connect ECONNREFUSED ::1:11434`). Point the Base URL at the Ollama service name, `http://ollama:11434`.
Which Ollama model works best in n8n on a CPU server?
On 4 vCPU and 8 GB, qwen2.5:3b was the best choice in our 2026-11-19 test: 2.2 GB in memory, about 16 tokens a second, correct structured output and working tool calls. qwen2.5:7b needs 5.1 GB and answers at about 7 tokens a second.
Can an n8n AI Agent use tools with a local Ollama model?
Yes, if the model supports tool calls. With qwen2.5:3b, an n8n AI Agent called the Calculator tool and used its result in the answer. Check the model's Ollama library page for the tools tag.
How much RAM do n8n and Ollama need together?
n8n used 450 to 550 MB, and a 3B model 2.2 to 2.6 GB. A server with 8 GB runs both with room to spare; a 7B model (5.1 GB) fits on 8 GB only on its own, so 16 GB is the size for daily 7B use.