How to Install n8n with Docker-compose on Ubuntu

How to Install n8n with Docker-compose on Ubuntu

n8n is a powerful, free, and self-hosted workflow automation tool that simplifies and automates tasks across various applications and services. It allows users to create complex workflows without needing extensive coding knowledge.

Prerequisites:

Before starting, ensure Docker and Docker Compose are installed and running on your Ubuntu system.

Follow these steps to install it on Ubuntu with Docker compose:


Step 1: Create a directory for n8n:

Run the following command to create a directory and navigate into it:

mkdir n8n && cd n8n

Step 2: Configuration Database:

Option 1: Use n8n with the Default SQLite Database:

  1. Create a docker-compose.yml file:
sudo nano docker-compose.yml
  1. Add the following content to the file:
version: '3.9'

volumes:
  n8n_data:
services:
  n8n:
    container_name: n8n
    image: docker.n8n.io/n8nio/n8n
    restart: always
    ports:
      - "5678:5678"
    environment:
      - N8N_SECURE_COOKIE=false
    volumes:
      - n8n_data:/home/node/.n8n

Option 2: Use n8n with an Existing PostgreSQL Database:

⚠️
Prerequisite: Before you start, make sure you’ve set up a PostgreSQL database and named it n8n
  1. Create a .env File:
sudo nano .env

Store the default database connection details (host, username, password, etc.) to a .env environment file.

Add the following content to the file, replacing placeholders with your actual PostgreSQL credentials:

POSTGRESDB_HOST=<PostgreSQL_Host_IP>      # IP address of the PostgreSQL server
POSTGRESDB_PORT=<PostgreSQL_Port_Number>  # Port number for PostgreSQL (default is 5432)
POSTGRES_DB=n8n                           # Name of the PostgreSQL database
POSTGRES_USER=<Non_Root_User>             # PostgreSQL Username
POSTGRES_PASSWORD=<Password>              # PostgreSQL Password
  1. Create a docker-compose.yml File:
sudo nano docker-compose.yml

Add the following content to the file:

version: '3.9'

volumes:
  n8n_data:
    external: true

services:
  n8n:
    container_name: n8n
    image: docker.n8n.io/n8nio/n8n
    restart: always
    ports:
      - "5678:5678"
    environment:
      - N8N_SECURE_COOKIE=false
      - DB_TYPE=postgresdb
      - DB_POSTGRESDB_HOST=${POSTGRESDB_HOST}
      - DB_POSTGRESDB_PORT=${POSTGRESDB_PORT}
      - DB_POSTGRESDB_DATABASE=${POSTGRES_DB}
      - DB_POSTGRESDB_USER=${POSTGRES_USER}
      - DB_POSTGRESDB_PASSWORD=${POSTGRES_PASSWORD}
    volumes:
      - n8n_data:/home/node/.n8n

Step 3: Create external data folder

Create the external Docker volume that's defined as n8n_data. This volume is used to store persistent data for n8n, such as the SQLite database file (if using SQLite) and the encryption key. This setup ensures data is retained even if the container is recreated or updated.

sudo docker volume create n8n_data

Step 4: Start n8n using Docker Compose:

Save and exit the file, then run the following commands:

sudo docker-compose up -d

That’s it! n8n should now be running on your system. You can access it by opening http://<Your_Server_IP>:5678 in your browser.

Do you enjoy this blog post?

Read more