Self-Hosted AI on a VPS: n8n and Ollama with Docker
Run n8n and Ollama on one VPS with Docker Compose: AI workflows and agents with local models, no per-token bills, behind HTTPS. Includes plan sizing.
9 min read
n8n is a workflow tool: it connects apps, webhooks and databases, and its AI nodes can hand a step to a language model. Ollama runs that model on your own server. Put both on one VPS and you have AI automations, such as sorting incoming email, summarising support tickets or answering questions from your documents, with no per-token bills and nothing sent to an outside AI service.
This guide sets up n8n 3, its task runner, PostgreSQL and Ollama with Docker Compose, puts n8n behind HTTPS on its own subdomain, and keeps Ollama off the internet.
Which Plan You Need
RAM decides which model you can run. n8n itself is light: allow about 2 GB for n8n, its runner and PostgreSQL together, then add the model.
| What you want | Model | RAM for the model | Smallest VPS | Smallest Business Cloud |
|---|---|---|---|---|
| Try it out, simple text tasks | llama3.2:3b | 4 GB | VPS 600 (16 GB) | VPS Ignite 2 (8 GB) |
| AI agents that call tools | llama3.1:8b | 7 GB | VPS 600 (16 GB) | VPS Pulse 2 (16 GB) |
| Better reasoning and summaries | qwen3:14b | 12 GB | VPS 600 (16 GB) | VPS Pulse 2 (16 GB) |
| Best quality at a usable CPU speed | qwen3:30b | 21 GB | VPS 1200 (48 GB) | VPS Surge 2 (32 GB) |
The model figures come from Ollama Hosting on a VPS, which lists more models and explains the rule behind them. Models run on the CPU on GoZen plans, so smaller models answer faster.
Workflows that run all day, like a support inbox, keep the CPU busy every time they call the model. That’s where Business Cloud and its dedicated AMD EPYC vCPU earn their price. For testing and occasional jobs, a standard VPS gives you more RAM for the money.
Before You Start
You need:
- A VPS running Ubuntu 24.04, Debian 12 or 13, or AlmaLinux 9 or 10, with SSH access as a user with
sudo - Docker and the Compose plugin. If you don’t have them yet, follow Docker Basics
- A subdomain for n8n, such as
n8n.yourdomain.com, with an A record pointing at the VPS. Create it now so DNS has time to update. See Pointing Your Domain to Your Server - Ports 80 and 443 open in the firewall, for the web server and its certificate
Step 1: Create the Project Folder
mkdir ~/n8n-ai && cd ~/n8n-ai
Create the settings file. The three openssl lines generate random passwords and keys for you:
cat > .env <<EOF
N8N_VERSION=3.4.3
OLLAMA_VERSION=0.40.19
N8N_DOMAIN=n8n.yourdomain.com
TIMEZONE=Europe/London
POSTGRES_PASSWORD=$(openssl rand -hex 24)
N8N_ENCRYPTION_KEY=$(openssl rand -hex 32)
RUNNERS_AUTH_TOKEN=$(openssl rand -hex 32)
EOF
chmod 600 .env
Change N8N_DOMAIN to your subdomain and TIMEZONE to yours (n8n uses it for scheduled workflows). Then open the file with cat .env and copy the N8N_ENCRYPTION_KEY value to your password manager. n8n encrypts every saved credential with it. If you lose it, your workflows survive but every API key and password in them becomes unreadable.
Step 2: Write the Compose File
Create compose.yaml in the same folder:
services:
postgres:
image: postgres:18
restart: unless-stopped
environment:
POSTGRES_USER: n8n
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_DB: n8n
PGDATA: /var/lib/postgresql/data
volumes:
- postgres_data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U n8n -d n8n"]
interval: 5s
timeout: 5s
retries: 10
n8n:
image: n8nio/n8n:${N8N_VERSION}
restart: unless-stopped
ports:
- "127.0.0.1:5678:5678"
environment:
DB_TYPE: postgresdb
DB_POSTGRESDB_HOST: postgres
DB_POSTGRESDB_DATABASE: n8n
DB_POSTGRESDB_USER: n8n
DB_POSTGRESDB_PASSWORD: ${POSTGRES_PASSWORD}
N8N_ENCRYPTION_KEY: ${N8N_ENCRYPTION_KEY}
N8N_HOST: ${N8N_DOMAIN}
N8N_PROTOCOL: https
N8N_WEBHOOK_URL: https://${N8N_DOMAIN}/
N8N_PROXY_HOPS: 1
N8N_ENFORCE_SETTINGS_FILE_PERMISSIONS: "true"
GENERIC_TIMEZONE: ${TIMEZONE}
TZ: ${TIMEZONE}
N8N_RUNNERS_MODE: external
N8N_RUNNERS_AUTH_TOKEN: ${RUNNERS_AUTH_TOKEN}
N8N_RUNNERS_BROKER_LISTEN_ADDRESS: 0.0.0.0
volumes:
- n8n_data:/home/node/.n8n
depends_on:
postgres:
condition: service_healthy
n8n-runner:
image: n8nio/runners:${N8N_VERSION}
restart: unless-stopped
environment:
N8N_RUNNERS_AUTH_TOKEN: ${RUNNERS_AUTH_TOKEN}
N8N_RUNNERS_TASK_BROKER_URI: http://n8n:5679
depends_on:
- n8n
ollama:
image: ollama/ollama:${OLLAMA_VERSION}
restart: unless-stopped
environment:
OLLAMA_NO_CLOUD: "1"
OLLAMA_KEEP_ALIVE: 30m
volumes:
- ollama_data:/root/.ollama
volumes:
postgres_data:
n8n_data:
ollama_data:
What the less obvious parts do:
127.0.0.1:5678:5678publishes n8n to the server itself only. Nginx will serve it to the internet over HTTPS. Docker’s published ports skip UFW and firewalld rules, so binding to127.0.0.1is what actually keeps the plain-HTTP port private.- Ollama has no
ports:line at all. n8n reaches it over the Compose network athttp://ollama:11434, and nothing outside the server can. Ollama’s API has no login, so it should stay that way. N8N_PROXY_HOPS: 1tells n8n there’s one reverse proxy in front of it, so it trusts the visitor IP and protocol Nginx passes on.n8n-runneris the task runner that executes Code nodes in a separate container. n8n 3 recommends this “external” mode for production. Its version must always match n8n’s, which is why both useN8N_VERSION.PGDATApins PostgreSQL 18’s data directory to the volume. Don’t remove it.OLLAMA_NO_CLOUDturns off Ollama’s cloud models, so prompts never leave the server.OLLAMA_KEEP_ALIVE: 30mkeeps a model loaded for 30 minutes after its last use, so workflows don’t wait for it to reload each time.
Step 3: Start the Stack and Download a Model
docker compose up -d
docker compose ps
All four services should show as running (PostgreSQL also as healthy) within a minute. Now download a model into Ollama:
docker compose exec ollama ollama pull llama3.1:8b
Use the model you picked from the table. Check that it answers:
docker compose exec ollama ollama run llama3.1:8b "Say hello in five words."
Step 4: Put n8n Behind HTTPS
Install Nginx and Certbot:
server {
listen 80;
listen [::]:80;
server_name n8n.yourdomain.com;
# Large enough for files sent to webhooks
client_max_body_size 50M;
location / {
proxy_pass http://127.0.0.1:5678;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Proto $scheme;
# The editor uses a WebSocket for live updates
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
# AI steps on a CPU can take minutes
proxy_read_timeout 600s;
proxy_send_timeout 600s;
}
}
Change server_name to your subdomain, then test the config and get the certificate:
sudo nginx -t
sudo systemctl reload nginx
sudo certbot --nginx -d n8n.yourdomain.com
Certbot adds HTTPS to the file and redirects HTTP to it. Nginx Reverse Proxy Config explains each line if you want the detail.
Step 5: Create Your Owner Account
Open https://n8n.yourdomain.com straight away. The first person to load the page sets up the owner account, so don’t leave it waiting. Use a strong password, then turn on two-factor authentication under Settings > Personal.
Step 6: Connect n8n to Ollama
- In n8n, go to Credentials and create a new Ollama credential
- Set Base URL to
http://ollama:11434and leave the API key empty - Save. n8n tests the connection, and it should pass
The address is the service name from compose.yaml. localhost won’t work here, because inside the n8n container localhost is n8n itself.
Step 7: Build a First AI Workflow
A chat with an agent that can use tools takes three nodes:
- Create a workflow and add the When chat message received trigger
- Add an AI Agent node after it
- On the agent’s Chat Model input, add Ollama Chat Model, pick your Ollama credential, and choose the model you downloaded
- Optionally, on the agent’s Tool input, add a tool such as Calculator or an HTTP Request to one of your own APIs
- Click Open chat and ask it something
The first answer takes longer while Ollama loads the model. After that it stays in memory for 30 minutes.
For agents, use a model that supports tool calling. llama3.1:8b, qwen3:14b and qwen3:30b all do; the Ollama library marks these models with a tools label. Smaller models often ignore tools or call them wrongly.
The same credential works in the Ollama Model node (for plain text tasks inside any workflow) and Embeddings Ollama (for searching your own documents).
Keep It Running
Update
Read the n8n release notes first, then change N8N_VERSION (and OLLAMA_VERSION, if you want) in .env:
cd ~/n8n-ai
docker compose pull
docker compose up -d
The runner follows n8n’s version automatically, because both read N8N_VERSION. Take a backup before any jump to a new major version: n8n updates its database when it starts and can’t go back.
Back Up
Two things hold your data: the PostgreSQL database (workflows, credentials, history) and the encryption key in .env. Dump the database every night with a cron job:
mkdir -p ~/n8n-backups
cd ~/n8n-ai && docker compose exec -T postgres pg_dump -U n8n n8n | gzip > ~/n8n-backups/n8n-$(date +%F).sql.gz
Copy the dumps and .env off the server. Automated VPS Backups shows how to schedule this and send it to remote storage with rclone. A snapshot before an update is a quick extra safety net.
Never run docker compose down -v on this stack. The -v deletes the volumes: your database, n8n’s settings and every downloaded model. Plain docker compose down stops the containers and keeps the data.
The n8n Licence
n8n is released under its Sustainable Use License. You can self-host it free for your own business’s internal use, and you can charge clients for building and maintaining workflows for them. You can’t host n8n as a service that your clients log into to build their own workflows, or rebrand it. If that’s your plan, read n8n’s licence FAQ first.
Troubleshooting
| Problem | Fix |
|---|---|
| 502 Bad Gateway in the browser | n8n isn’t up yet, or failed to start. Run docker compose ps and docker compose logs n8n |
The Ollama credential test fails with ENOTFOUND or “connection refused” | The Base URL must be http://ollama:11434, not localhost. Check Ollama is running with docker compose ps |
| The AI Agent node times out | The model is too big for a quick answer on a CPU. Try a smaller one, or raise the timeout in the node’s options. The Nginx timeouts above allow 10 minutes |
| “model not found” in a workflow | The model in the node isn’t downloaded. Run docker compose exec ollama ollama list, and pull it if missing |
Webhook URLs show localhost:5678 | N8N_WEBHOOK_URL isn’t set. Check .env and the compose file, then docker compose up -d |
| Code nodes fail with a task runner error | The runner and n8n versions differ, or the auth token doesn’t match. Both read N8N_VERSION and RUNNERS_AUTH_TOKEN from .env; restart with docker compose up -d |
| The server runs out of memory | Model plus n8n is more than the plan’s RAM. Use a smaller model, or set OLLAMA_KEEP_ALIVE to 5m so it unloads sooner. Check with free -h and docker stats |
What to Do Next
- Ollama Hosting on a VPS - every model size with the plan it needs, and Ollama tuning
- Docker Basics - containers, volumes and Compose commands
- Server Hardening Basics - secure the VPS your workflows run on
- AMD EPYC VPS - dedicated vCPU for workflows that call the model all day
Last updated 03 Dec 2026, 00:00 +0200.