Skip to content
SelfHostBR, home page
Guide

n8n with Ollama: run local AI on your VPS with Docker Compose

Run n8n and Ollama together in Docker Compose, pull a model, create the Ollama credential in n8n and build an AI workflow without a paid API.

MMarlon Published on 7 min read
On this page

Short answer: start n8n and Ollama in the same docker compose project, pull a model with ollama pull, and in n8n create the Ollama credential with the Base URL http://ollama:11434 (the service name, not localhost). Then connect the Ollama Chat Model node to a Basic LLM Chain or an AI Agent.

Running the AI on your own server has two clear advantages: nothing leaves your machine, and there is no per-token billing. The cost moves to the VPS, which needs RAM and CPU for the model. Below, everything is built from scratch, ending with a workflow that receives text through a webhook and returns a summary.

How much machine Ollama needs

Without a GPU, Ollama runs on the CPU, and RAM is what matters. A small model such as qwen2.5:0.5b (about 400 MB on disk) keeps the Ollama container around 650 MB of memory with the model loaded; n8n sits around 400 MB. Add the system and Docker, and a 4 GB VPS runs this combination comfortably.

Larger models need much more: a quantized 3B-parameter model takes about 2 GB, and a 7 to 8B model takes about 5 GB for the model alone. For those, think 8 GB of RAM or more. See the n8n RAM and CPU requirements and use the calculator, adding the model’s memory under Extra RAM.

Set aside disk space. The ollama/ollama image takes about 5.6 GB, because it ships the GPU libraries. Add the size of every model you download.

The docker-compose.yml

In a new folder (for example ~/n8n-ai), create docker-compose.yml:

services:
  n8n:
    image: docker.n8n.io/n8nio/n8n:stable
    restart: unless-stopped
    ports:
      - "127.0.0.1:5678:5678"
    environment:
      - GENERIC_TIMEZONE=Europe/Berlin
      - TZ=Europe/Berlin
    volumes:
      - n8n_data:/home/node/.n8n
    depends_on:
      - ollama

  ollama:
    image: ollama/ollama:latest
    restart: unless-stopped
    volumes:
      - ollama_data:/root/.ollama
    environment:
      - OLLAMA_KEEP_ALIVE=24h

volumes:
  n8n_data:
  ollama_data:

Notice two details:

  • Ollama does not publish a port. It has no authentication, so do not expose 11434 to the internet. n8n reaches it over the internal Compose network, using the service name ollama.
  • OLLAMA_KEEP_ALIVE=24h keeps the model loaded in memory. The default unloads it after 5 minutes without use, and the next call is slower because the model has to come back into RAM.

n8n is bound to 127.0.0.1 so you can put HTTPS in front of it. If you don’t have that yet, follow n8n with Docker Compose and HTTPS and just add the ollama service and the ollama_data volume to your file.

Start everything:

docker compose up -d

Pull a model

docker compose exec ollama ollama pull qwen2.5:0.5b
docker compose exec ollama ollama list

qwen2.5:0.5b is good for checking the setup, but it is too small for serious tasks: it gets facts wrong and sometimes ignores the question. For real use, switch to a larger model from the Ollama library, such as qwen2.5:3b or llama3.2:3b, if your RAM allows. The command is the same; only the name changes.

To confirm Ollama answers, call its API from inside the n8n container:

docker compose exec n8n wget -qO- \
  --post-data='{"model":"qwen2.5:0.5b","prompt":"Say hi","stream":false}' \
  http://ollama:11434/api/generate

Create the Ollama credential in n8n

  1. In n8n, open Credentials → Create credential (or Overview → Create → Credential) and choose Ollama.
  2. Under Base URL, enter http://ollama:11434.
  3. Leave API Key empty (it is only for Ollama behind an authenticating proxy, such as Open WebUI).
  4. Click Save. n8n tests the connection right away and tells you whether it worked.

The most common mistake is leaving the default http://localhost:11434. Inside the container, localhost is n8n itself, not Ollama, so the connection is refused. The n8n documentation covers the cases:

  • n8n and Ollama in the same Compose project: http://ollama:11434
  • Only n8n in Docker, Ollama directly on the host: http://host.docker.internal:11434
  • Neither in Docker: http://127.0.0.1:11434

In the second case, on Linux, add extra_hosts: ["host.docker.internal:host-gateway"] to the n8n service and make Ollama listen on 0.0.0.0.

Build the workflow

A simple example: receive text through a webhook and return a summary.

  1. Add a Webhook node: method POST, path summary and, under Respond, the option When Last Node Finishes.
  2. Connect a Basic LLM Chain to it. Under Source for Prompt, choose Define below and write the prompt in Expression mode: Summarize in one sentence: {{ $json.body.text }}
  3. On the Model connector of the Basic LLM Chain, add Ollama Chat Model, pick the credential you created and the qwen2.5:0.5b model (the list comes from Ollama itself).
  4. Save and publish the workflow.

To test it, run this on the VPS:

curl -X POST http://127.0.0.1:5678/webhook/summary \
  -H 'Content-Type: application/json' \
  -d '{"text":"n8n automates tasks and runs on your own server."}'

The response comes back as JSON, in the text field. With the 0.5B model on a shared CPU, the answer took about 15 to 20 seconds; larger models are slower. If whatever calls the webhook has a time limit, change Respond to Immediately and deliver the result later, for example with an HTTP Request node or a message.

The same Ollama Chat Model plugs into the AI Agent, which uses tools. For an agent, pick a model with tools support from the Ollama library: tiny models tend to fail at it. For RAG, there is the Embeddings Ollama node, which uses the same credential.

Updating and backing up

  • Update both with docker compose pull and docker compose up -d. Before that, follow the n8n backup guide; see also how to update n8n.
  • Models live in the ollama_data volume. They don’t need a backup: you can download them again with ollama pull.

Where to host it

For local AI, prioritize RAM. A plan with 8 GB, such as Hostinger KVM 2 (2 vCPU, 8 GB), has plenty of memory for n8n plus a 3B-parameter model, which takes about 2 GB: see the plan ↗. Compare current prices in how much it costs to host n8n.

Frequently asked questions

Which Base URL should I use for the Ollama credential in n8n?

With n8n and Ollama in the same Docker Compose project, use http://ollama:11434, the service name. The default http://localhost:11434 does not work inside the n8n container, because localhost points to n8n itself.

How much RAM do I need to run n8n with Ollama?

With a small model such as qwen2.5:0.5b, Ollama sits around 650 MB and n8n around 400 MB, so 4 GB is enough. A 3B model takes about 2 GB, and then 8 GB of RAM is the comfortable size.

Do I need a GPU to use Ollama with n8n?

No. Ollama runs on the CPU, only slower. For small models and tasks such as summarizing and classifying, a VPS without a GPU works.

Should I expose Ollama’s port 11434?

No. Ollama has no authentication. Leave the port unpublished and reach it over the internal Docker Compose network.

M

Written by

Marlon

I like automation and write here what I learn building automations with n8n. Spotted a mistake or an outdated detail? Let me know.

Keep reading