Skip to content
TheAgent Ecosystem
Automation

How to Connect Ollama to n8n (Free Local AI Agent, Step by Step)

The exact Base URL fix for native, Docker Desktop, and Linux server setups

Muhammad Qasim HammadAI-assisted8 min read1,657 words

AI-drafted, reviewed by Muhammad Qasim Hammad on June 7, 2026. See our AI disclosure.

Laptop running a local Ollama AI model connected to an n8n automation workflow, showing free local AI agent setup

The credential test fails because n8n is looking inside the container, not at Ollama on your host. Fix it by choosing the right Base URL for where n8n actually runs: http://localhost:11434 for native installs, or http://host.docker.internal:11434 when n8n is in Docker.

If you have hit that gray "Connection failed" error and restarted Ollama three times without result, you are not alone. The networking mismatch between Docker containers and the host machine is the single most common reason this setup breaks, and most tutorials skip it entirely.

Laptop running a local Ollama model connected to n8n automation workflow nodes, illustrating a free local AI agent setupRunning a local model through n8n costs $0 per token and keeps data on your machine.

What is the fastest way to connect Ollama to n8n?

Install Ollama, pull one model, create an Ollama credential in n8n with the correct Base URL for your setup, attach an Ollama Chat Model sub-node to an AI Agent node, and run a test prompt. The whole process takes under 15 minutes if the networking is right. The table in the next section tells you exactly which URL to use.

For context on how this fits into a broader stack, see the solopreneur AI automation cost guide, which puts the $0 per-token cost of local models against paid API pricing.

Which Ollama Base URL should you use in n8n?

The right Base URL depends entirely on where n8n is running, not where Ollama is running. Ollama always serves at port 11434 on the host machine. The question is whether n8n can see "the host machine" via localhost or needs a different address to escape its container.

Setupn8n runs where?Ollama runs where?Base URL in n8nExtra stepCommon failure
Local / nativen8n native on hostSame hosthttp://localhost:11434NoneOllama app not running
Docker Desktop (Mac/Windows)n8n containerSame hosthttp://host.docker.internal:11434Usually noneUsing localhost from inside container
Linux Docker / servern8n containerSame hosthttp://host.docker.internal:11434Add extra_hosts: host.docker.internal:host-gatewayhost.docker.internal not defined
Remote Ollama / proxyn8n anywhereRemote serverRemote HTTPS URLOptional Bearer API keyExposing Ollama without authentication

According to the Ollama n8n integration docs, host.docker.internal is the correct address when n8n runs through Docker. On Linux it must be explicitly mapped because Docker Desktop adds it automatically on Mac and Windows, but Docker Engine on Linux does not.

Connection map showing three n8n setups connecting to Ollama: native uses localhost, Docker Desktop uses host.docker.internal, Linux Docker needs host-gatewayPick your row from this map before setting the Base URL in n8n credentials.

How do you install Ollama and pull a model?

Download Ollama from ollama.com for your OS, then pull one small model before opening n8n. After installation, the local API listens at http://localhost:11434/api, as documented in the Ollama API introduction. If the server is not running, start the app or run ollama serve.

ollama pull llama3.1
curl http://localhost:11434/api/tags

The api/tags call returns a JSON list of every model you have pulled. If you see llama3.1 in that list, Ollama is running and the model is ready. A connection refused error means Ollama is not running. Start it with ollama serve in a terminal or launch the desktop app.

You can also test using the OpenAI-compatible endpoint that Ollama exposes at /v1/chat/completions. This is the same format n8n's OpenAI credential uses, which makes it useful for debugging:

curl -X POST http://localhost:11434/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{
    "model": "llama3.1",
    "messages": [{ "role": "user", "content": "Write a one-sentence email summary." }]
  }'

The API key field in that endpoint is required by the OpenAI client format but ignored by the local Ollama server. Pass any string or leave it as a placeholder.

Docker container networking diagram showing why localhost inside a container cannot reach Ollama running on the host machineInside Docker, localhost points to the container. Use host.docker.internal to escape it.

How do you create the Ollama credential in n8n?

Create a new credential in n8n, select the Ollama type, and set the Base URL to match your setup from the table above. No API key is needed for local use. The n8n Ollama credentials docs confirm the only required field is the Base URL, which defaults to http://localhost:11434.

Step by step:

  1. Open n8n and go to Settings in the left sidebar.
  2. Click Credentials, then Add Credential.
  3. Search for "Ollama" and select it.
  4. Set Base URL to http://localhost:11434 (native) or http://host.docker.internal:11434 (Docker).
  5. Leave API Key blank for local setups.
  6. Click Save, then Test to confirm the connection.

A green checkmark means n8n reached the Ollama server. A red error almost always means the wrong Base URL for your setup.

How do you build the first local AI Agent workflow?

Add a Chat Trigger or Manual Trigger node, connect it to an AI Agent node, then attach an Ollama Chat Model sub-node below the agent. The n8n AI Agent node docs require at least one tool sub-node connected alongside the model. Start with an HTTP Request or Data Table tool to satisfy that requirement.

The n8n Ollama Chat Model sub-node exposes a Model field, plus Temperature and other sampling controls. Use the exact model name you pulled in Ollama, for example llama3.1, unless your n8n version presents a fixed dropdown.

A minimal working workflow looks like this:

  1. Chat Trigger (receives the user message)
  2. AI Agent node (orchestrates reasoning and tool calls)
  3. Ollama Chat Model sub-node (attached to the agent's model input, using your Ollama credential)
  4. HTTP Request tool sub-node (attached to the agent's tool input, pointed at any API you need)

Test with a small, specific prompt first: "Summarize this text in one sentence." Once that returns clean output, wire in real tools like Google Sheets for automated lead follow-up workflows.

Why does host.docker.internal fail on Linux?

On Linux, Docker Engine does not automatically add host.docker.internal as a resolvable hostname inside containers. Docker Desktop on Mac and Windows handles this silently, which is why tutorials written on a Mac often skip this step. On a Linux server or desktop running Docker Engine directly, the hostname simply does not exist unless you add it.

The fix is one line in your docker-compose.yml, as specified in the Ollama n8n integration guide:

services:
  n8n:
    image: n8nio/n8n:latest
    extra_hosts:
      - "host.docker.internal:host-gateway"

host-gateway is a special Docker value that resolves to the host machine's IP from inside the container. After adding this line, run docker compose down && docker compose up -d to restart with the new config. The n8n Docker installation docs show the standard container runs on 5678:5678, so your n8n instance stays at http://localhost:5678 after the restart.

If you started the container with docker run instead of Compose, pass --add-host=host.docker.internal:host-gateway as a flag.

Private local AI agent processing email, calendar, and spreadsheet tasks offline without sending data to external serversLocal agents handle sensitive data tasks without a single byte leaving your machine.

When should you use Ollama instead of the Claude API in n8n?

Use Ollama when volume is high, data is sensitive, or the quality bar is moderate. Use a hosted API like Claude when reasoning quality, instruction-following precision, or low latency matter most. The decision is mostly about cost and privacy, not capability at small scale.

CriterionOllama (local)Claude / OpenAI API
Per-token cost$0Varies by model and provider
Data privacyFully local, no data leaves your machineData sent to third-party servers
Reasoning quality (complex tasks)Moderate (depends on model size)High
Max context windowDepends on pulled model and memoryDepends on model and provider
Setup time10-15 minutes2 minutes (API key only)
Offline useYesNo
Best forSummaries, extraction, classification, high-volume draftsComplex agents, precise instructions, client-facing outputs

For a deeper comparison of which local tool to pick before this step, the Ollama vs LM Studio vs Jan guide covers why Ollama fits automation builders best, including its OpenAI-compatible endpoints that work directly with n8n's OpenAI credential node as an alternative path.

What should you do next?

Get the credential test green first. The agent workflow and tool connections are straightforward once the Base URL is right. If you are on Linux and still stuck after adding extra_hosts, run docker exec -it <n8n-container-name> curl http://host.docker.internal:11434/api/tags from your terminal to confirm the container can reach Ollama before touching the n8n UI again.

The natural next build is a local AI Agent that reads from a Google Sheet, processes each row with Ollama, and writes results back, all at $0 per run. That kind of high-volume, private workflow is where local models pay for themselves.

Frequently asked questions

Can n8n use Ollama for free?
Yes. Ollama is free and open-source. Running it locally means $0 per-token cost. You only pay for electricity and hardware. The n8n Ollama credential and Chat Model node are available on all n8n plans including self-hosted.
What Base URL should I use for Ollama in n8n?
Use http://localhost:11434 when n8n runs natively on the same machine as Ollama. Use http://host.docker.internal:11434 when n8n runs inside a Docker container. For a remote server, use the HTTPS URL of that server.
Why does n8n fail to connect to Ollama in Docker?
Because localhost inside a Docker container refers to the container itself, not the host machine. Ollama is running on the host, so the container cannot reach it via localhost. Switch the Base URL to http://host.docker.internal:11434 to fix this.
Do I need an API key for local Ollama?
No. The n8n Ollama credential has an optional Bearer API key field, but the Ollama docs confirm the key is required by the client format but ignored by the local server. Leave it blank for local setups.
Can I use Ollama with the n8n AI Agent node?
Yes. Add the Ollama Chat Model as a sub-node connected to the AI Agent node. The agent node requires at least one tool sub-node as well. The Ollama Chat Model node exposes model selection and sampling controls.
Is Ollama good enough for production automations?
It depends on the task. Ollama works well for summarization, classification, data extraction, and draft generation at high volume with zero API cost. For tasks needing strong reasoning or precise instruction-following, a hosted model like Claude is more reliable.

Sources

Primary references and vendor documentation used while drafting and reviewing this article.

  1. Ollama API Introduction
  2. Ollama OpenAI Compatibility
  3. Ollama n8n Integration Guide
  4. n8n Ollama Credentials
  5. n8n Ollama Chat Model Sub-node
  6. n8n AI Agent Node
  7. n8n Docker Installation
  8. Docker Desktop Networking: Published Ports

Related reading