Install n8n with Docker Compose on Ubuntu

Install n8n with Docker Compose on Ubuntu

n8n is a powerful workflow automation platform that lets you connect APIs, databases, WordPress, messaging apps, AI services, and many other tools. By self-hosting n8n, you keep control of your workflows, credentials, and execution data.

In this guide, you will learn how to install n8n with Docker Compose on Ubuntu. This approach is reliable, easy to maintain, and suitable for a VPS or dedicated server. Docker Compose also makes future upgrades, backups, and configuration changes much simpler. n8n documents Docker Compose as a suitable approach for production-style deployments that may use databases and additional services.

Prerequisites

Before you start, prepare an Ubuntu server with the following requirements:

  • Ubuntu 22.04 LTS or Ubuntu 24.04 LTS
  • A non-root user with sudo access
  • At least 2 GB RAM for a small n8n instance
  • A domain or subdomain, such as n8n.example.com, for production use
  • Open firewall ports for SSH, HTTP, and HTTPS when using a reverse proxy
  • Docker Engine and the Docker Compose plugin

Docker’s official Ubuntu installation guide uses Docker’s APT repository and installs docker-ce, docker-ce-cli, containerd.io, docker-buildx-plugin, and docker-compose-plugin.docker

Install Docker on Ubuntu

First, update the server packages:

bashsudo apt update && sudo apt upgrade -y

Install the dependencies required to add Docker’s official repository:

bashsudo apt install -y ca-certificates curl

Create a directory for Docker’s repository key:

bashsudo install -m 0755 -d /etc/apt/keyrings

Download and save Docker’s official GPG key:

bashsudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg \
  -o /etc/apt/keyrings/docker.asc

sudo chmod a+r /etc/apt/keyrings/docker.asc

Add Docker’s stable repository:

bashecho \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] \
  https://download.docker.com/linux/ubuntu \
  $(. /etc/os-release && echo "${UBUNTU_CODENAME:-$VERSION_CODENAME}") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

Update the package index and install Docker Engine with Docker Compose:

bashsudo apt update

sudo apt install -y \
  docker-ce \
  docker-ce-cli \
  containerd.io \
  docker-buildx-plugin \
  docker-compose-plugin

Verify that Docker and Compose are installed correctly:

bashdocker --version
docker compose version

The official Docker documentation provides these repository and package-installation steps for Ubuntu.docker

Allow Docker Without Sudo

By default, Docker commands usually require sudo. You can add your current user to the Docker group to make daily container management easier.

bashsudo usermod -aG docker $USER

Log out and log in again, or run:

bashnewgrp docker

Then test Docker access:

bashdocker ps

If the command runs without a permission error, your Docker user configuration is ready.

Create the n8n Project

Create a dedicated directory for the n8n deployment:

bashmkdir -p ~/n8n-compose
cd ~/n8n-compose

Keeping the Compose file and persistent configuration in one directory makes backups and upgrades easier. n8n’s Compose documentation similarly starts by creating a dedicated project directory before creating the Compose file.n8n

Create the Compose configuration:

bashnano compose.yaml

Paste the following configuration:

textservices:
  n8n:
    image: docker.n8n.io/n8nio/n8n:latest
    container_name: n8n
    restart: unless-stopped
    ports:
      - "5678:5678"
    environment:
      - TZ=Asia/Jakarta
      - GENERIC_TIMEZONE=Asia/Jakarta
      - N8N_ENCRYPTION_KEY=CHANGE_THIS_TO_A_LONG_RANDOM_SECRET
      - N8N_SECURE_COOKIE=false
    volumes:
      - n8n_data:/home/node/.n8n

volumes:
  n8n_data:

Save the file with Ctrl+O, press Enter, then exit with Ctrl+X.

The n8n_data Docker volume is important because it preserves workflows, credentials, settings, and execution-related configuration when the container is recreated. n8n supports configuration through environment variables in the environment section of a Compose file.n8n

Configure Environment Variables

Before starting the container, change this value:

textN8N_ENCRYPTION_KEY=CHANGE_THIS_TO_A_LONG_RANDOM_SECRET

Use a long random key. You can generate one with:

bashopenssl rand -hex 32

Then replace the placeholder with the generated output:

textN8N_ENCRYPTION_KEY=your_generated_random_key_here

The encryption key protects stored credentials in n8n, so keep it private and include it in your secure configuration backup. n8n’s self-hosting documentation lists environment variables as the standard mechanism for modifying deployment configuration.n8n

Security note: N8N_SECURE_COOKIE=false is acceptable only for local testing or direct HTTP access. For a production server behind HTTPS, configure a reverse proxy and use secure cookies.

Start n8n Container

Start n8n in detached mode:

bashdocker compose up -d

Check the running container:

bashdocker compose ps

View the n8n logs if needed:

bashdocker compose logs -f

Open the n8n editor in your browser:

texthttp://YOUR_SERVER_IP:5678

For example:

texthttp://203.0.113.10:5678

The standard command to launch the Docker Compose deployment is sudo docker compose up -d when the user has not been granted Docker group access.n8n

Access n8n Securely

For a production installation, do not leave n8n exposed directly on port 5678. Instead, use a reverse proxy such as Traefik, Nginx Proxy Manager, Caddy, or Nginx.

Your production setup should provide:

  • HTTPS certificates from Let’s Encrypt
  • A dedicated subdomain, such as n8n.example.com
  • Webhook URLs that use your public HTTPS domain
  • Firewall rules that expose only ports 80 and 443 publicly
  • Port 5678 restricted to the internal Docker network when using a reverse proxy

Add these variables when n8n is served through HTTPS:

text      - N8N_HOST=n8n.example.com
      - N8N_PROTOCOL=https
      - N8N_PORT=5678
      - WEBHOOK_URL=https://n8n.example.com/

Restart the service after editing the Compose file:

bashdocker compose up -d

Set Up Basic Authentication

If your n8n instance is publicly reachable, enable an additional authentication layer. Add these variables in the environment section:

text      - N8N_BASIC_AUTH_ACTIVE=true
      - N8N_BASIC_AUTH_USER=admin
      - N8N_BASIC_AUTH_PASSWORD=USE_A_STRONG_PASSWORD

After saving the file, apply the changes:

bashdocker compose up -d

Use a unique username and a strong password. You should also protect the Ubuntu server itself with SSH keys, firewall rules, regular security updates, and a non-root administrative user.

Update n8n Safely

To update n8n later, open the deployment directory:

bashcd ~/n8n-compose

Pull the newest image:

bashdocker compose pull

Recreate the container using the updated image:

bashdocker compose up -d

Check that n8n is healthy:

bashdocker compose ps
docker compose logs -f

n8n’s Docker guidance describes the update sequence as pulling the image, stopping or recreating the older container, and starting the Compose service again.n8n

Back Up n8n Data

Your workflows and credentials are stored inside the n8n_data Docker volume. Create a backup before upgrades or major server changes.

Check the available volumes:

bashdocker volume ls

Create an archive backup:

bashdocker run --rm \
  -v n8n_data:/data \
  -v $(pwd):/backup \
  alpine \
  tar czf /backup/n8n-data-backup.tar.gz -C /data .

Store the resulting backup file in an off-server location, such as encrypted cloud storage or another backup server. Also back up your compose.yaml file and any .env file that contains domains, encryption keys, or database credentials.

Common Troubleshooting

Port 5678 Is Already Used

If port 5678 is occupied, identify the process:

bashsudo ss -tulpn | grep 5678

You can either stop the conflicting service or change the host port in compose.yaml:

textports:
  - "5679:5678"

Then access n8n through:

texthttp://YOUR_SERVER_IP:5679

Container Does Not Start

Check the container logs:

bashdocker compose logs -f n8n

Common causes include an invalid YAML indentation, a port conflict, missing Docker permissions, or an incorrectly configured environment variable.

Workflows Disappear After Restart

This usually happens when persistent storage is missing. Confirm that your Compose file includes the volume mapping:

textvolumes:
  - n8n_data:/home/node/.n8n

Never remove the Docker volume unless you intentionally want to delete your n8n data.

Final Thoughts

You have now completed the process to install n8n with Docker Compose on Ubuntu. This deployment gives you a flexible base for building workflow automation with WordPress, APIs, MySQL, PostgreSQL, Telegram, WhatsApp, AI services, and custom webhooks.

For a production environment, the next step is to add a reverse proxy with HTTPS and configure a custom domain. After that, you can safely connect n8n to your applications and begin automating repetitive business processes.

Scroll to Top