How to Install n8n on a Linux Server: A Step-by-Step Guide

Automation has become a cornerstone of productivity in today’s digital world. Whether you want to automate marketing tasks, synchronize data between services, or streamline your business operations, tools like n8n can help you do this without writing complex code.

Unlike many commercial platforms, n8n offers you full control — including the ability to self-host it on your own server, ensuring your data remains private and secure.

In this comprehensive guide, you will learn how to install n8n on a Linux server. I’ll walk you through two popular methods: using Docker, which is the easiest and most flexible way, and a direct Node.js installation for those who prefer not to use containers. Along the way, I’ll provide tips on securing your n8n instance, running it as a background service, and making your automation platform production-ready.

Why Choose Self-Hosting for n8n?

Before diving into installation, let’s briefly discuss why you might want to self-host n8n on your Linux server:

  • Data Privacy & Security

    When you self-host, your data stays on your server. You control backups, access, and how your information is shared.
  • Cost Control

    Cloud automation services typically come with monthly fees. Hosting on your server lets you avoid subscription costs (beyond your server expenses).
  • Customization

    Self-hosting lets you modify environment settings, add custom integrations, or connect to private networks that cloud versions may not support.
  • Performance & Scalability

    You can allocate resources as needed and scale workflows for heavy workloads without depending on third-party limits.

Prerequisites: What You’ll Need

To follow this guide smoothly, ensure you have the following:

  • A Linux server — Ubuntu 22.04 LTS is highly recommended for its stability and support.
  • Root or sudo access to the server to install software.
  • Basic familiarity with terminal commands and editing files.
  • A server with at least 1 GB RAM and 1 CPU core (more recommended for production).
  • Docker and Docker Compose installed (optional but recommended).

If you do not have Docker installed yet, don’t worry. We’ll cover the installation process shortly.

Docker is widely used in the tech industry to package applications and run them in isolated environments called containers. Using Docker for n8n brings several benefits:

  • Simplifies installation and management.
  • Ensures consistent environments, reducing “works on my machine” problems.
  • Makes updating or migrating easy.
  • Enables quick setup of persistent storage and environment variables.

Step 1: Install Docker & Docker Compose

First, update your package lists and install prerequisites:

Bash
sudo apt update
sudo apt install -y apt-transport-https ca-certificates curl software-properties-common

Add Docker’s official GPG key and repository:

Bash
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

Update package lists again and install Docker:

Bash
sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io

Check Docker is installed and running:

Bash
sudo systemctl status docker

You should see Docker active and running.

Next, install Docker Compose, which helps run multi-container setups with a single command:

Bash
sudo curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
docker-compose --version

You now have Docker and Docker Compose ready.

Step 2: Create a Docker Compose File for n8n

Create a directory to hold n8n files:

Bash
mkdir ~/n8n
cd ~/n8n

Create a file named docker-compose.yml inside this directory. This file tells Docker how to run n8n, map ports, mount volumes for persistent data, and set environment variables for configuration.

Here’s a sample docker-compose.yml you can start with:

YAML
version: '3'

services:
  n8n:
    image: n8nio/n8n
    restart: unless-stopped
    ports:
      - "5678:5678"
    environment:
      - N8N_BASIC_AUTH_ACTIVE=true
      - N8N_BASIC_AUTH_USER=${N8N_USER}
      - N8N_BASIC_AUTH_PASSWORD=${N8N_PASSWORD}
      - N8N_HOST=${N8N_HOST}
      - N8N_PORT=5678
      - NODE_ENV=production
      - GENERIC_TIMEZONE=Europe/Berlin
    volumes:
      - ./n8n-data:/home/node/.n8n
      

Explanation:

  • Image: Uses the official n8nio/n8n Docker image.
  • Restart policy: Automatically restarts the container if it crashes.
  • Ports: Maps container port 5678 (n8n default) to the same port on your server.
  • Environment variables: Controls authentication, hostname, timezone, and production mode.
  • Volumes: Maps a local folder n8n-data to the container’s data directory, ensuring workflows and credentials are saved across restarts.

Step 3: Create Environment Variables File

For security, do not store sensitive information directly in your compose file. Instead, create a .env file in the same folder:

Bash
touch .env
nano .env

Add these lines:

Plaintext
N8N_USER=admin
N8N_PASSWORD=YourStrongPasswordHere123!
N8N_HOST=your.server.ip.or.domain

Replace YourStrongPasswordHere123! with a complex password and your.server.ip.or.domain with your server’s IP or domain.

The .env file lets Docker Compose substitute those variables automatically.

Step 4: Launch n8n Container

Start the n8n service:

Bash
docker-compose up -d

The -d flag runs the container in detached mode, meaning it runs in the background.

Check running containers:

Bash
docker ps

You should see n8nio/n8n listed with port 5678 exposed.

Step 5: Access Your n8n Instance

Open a web browser and go to:

Plaintext
http://your.server.ip.or.domain:5678

You will be prompted for the username and password you set in .env.

Upon successful login, you will see the n8n visual workflow editor ready for use.

Extra Tips for Docker Users

  • To view logs for troubleshooting:

Bash
docker-compose logs -f
  • To stop the container:

Bash
docker-compose down
  • To update n8n to the latest version:

Bash
docker-compose pull
docker-compose up -d

Option 2: Installing n8n Without Docker (Node.js Install)

If you prefer not to use Docker, you can install n8n directly on your Linux server using Node.js.

Step 1: Install Node.js and npm

First, install Node.js v16 or later. Using the NodeSource repository is a reliable method:

Bash
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt-get install -y nodejs

Check versions:

Bash
node -v
npm -v

Ensure Node is version 16 or higher for compatibility.

Step 2: Install n8n Globally

Run:

Bash
sudo npm install -g n8n

This installs n8n globally, making the n8n command available anywhere.

Check the version to confirm:

Bash
n8n --version

Step 3: Start n8n

Simply run:

Bash
n8n start

By default, n8n runs on port 5678 and binds to localhost.

Visit your server IP on port 5678 in a browser to access n8n.

Securing Your n8n Instance Without Docker

When running directly, it’s critical to secure n8n.

Two main approaches:

  1. Environment Variables for Basic Auth

Set these before starting n8n:

Bash
export N8N_BASIC_AUTH_ACTIVE=true
export N8N_BASIC_AUTH_USER=admin
export N8N_BASIC_AUTH_PASSWORD=YourStrongPasswordHere123!
n8n start
  1. Use a Reverse Proxy (covered in the next section)

Running n8n as a Background Service

Running n8n start directly in the terminal means it stops if your SSH session closes.

Use pm2, a process manager, to keep it running:

Bash
sudo npm install -g pm2
pm2 start n8n
pm2 startup
pm2 save

Alternatively, create a systemd service for automatic startup and restart on failures.

Optional: Set Up a Reverse Proxy with Nginx and SSL

Exposing n8n directly over HTTP is not secure. A reverse proxy adds a layer of security and flexibility.

Why Use a Reverse Proxy?

  • Handle HTTPS encryption with free Let’s Encrypt certificates.
  • Serve n8n under a custom domain or subdomain.
  • Add additional HTTP security headers.
  • Proxy traffic from port 80/443 to your internal n8n port (usually 5678).

Step 1: Install Nginx

Bash
sudo apt update
sudo apt install nginx
sudo systemctl enable nginx
sudo systemctl start nginx

Step 2: Configure Nginx

Create a file /etc/nginx/sites-available/n8n:

Nginx
server {
    listen 80;
    server_name your-domain.com;

    location / {
        proxy_pass http://localhost:5678;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}

Enable the site:

Bash
sudo ln -s /etc/nginx/sites-available/n8n /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx

Step 3: Enable SSL with Let’s Encrypt

Install Certbot:

Bash
sudo apt install certbot python3-certbot-nginx

Run Certbot:

Bash
sudo certbot --nginx -d your-domain.com

Follow the prompts to obtain and install a free SSL certificate.

Now your n8n instance will be available securely at https://your-domain.com.

Persistence and Data Storage

By default, n8n uses a lightweight SQLite database stored inside its data folder.

For small setups, this is fine. But for production, consider a more robust database like PostgreSQL or MySQL.

To connect an external database:

  • Set environment variables like DB_TYPE, DB_POSTGRESDB_HOST, DB_POSTGRESDB_PORT, etc.
  • Ensure your database allows connections from the n8n server.
  • Backup your database regularly.

When using Docker, always map the container’s data directory to a local folder using volumes for persistence.

Security Best Practices

Running any server exposed to the internet comes with security responsibilities.

Here are recommended practices for n8n:

  • Use Basic Auth or OAuth to prevent unauthorized access.
  • Always use HTTPS via a reverse proxy to encrypt data in transit.
  • Keep your server and n8n updated with the latest patches.
  • Use strong passwords and change them periodically.
  • Limit network access via firewalls — only allow trusted IPs if possible.
  • Regularly backup workflows and credentials to prevent data loss.

Troubleshooting Common Issues

  • Port 5678 is in use: Check what process uses the port with sudo lsof -i :5678 and stop it or change n8n port.
  • Cannot access n8n web interface: Verify firewall settings (ufw status) and open port 5678 or 80/443 if using Nginx.
  • Docker volume permission issues: Ensure correct user permissions on mapped folders.
  • Authentication not working: Double-check .env file variables and restart containers.
  • SSL certificate renewal errors: Use Certbot’s dry-run to test renewal before expiry.

Conclusion

Self-hosting n8n on a Linux server is an excellent way to gain full control over your automation workflows. Using Docker makes installation fast and repeatable, while direct Node.js install gives flexibility without containers.

With proper security, persistent data storage, and optional reverse proxy setup, your n8n server will be ready for production use. This opens up a world of possibilities — from automating business tasks to creating complex multi-app workflows tailored exactly to your needs.

Start exploring n8n today and watch your productivity soar.

For further reading and advanced configuration, check out the official n8n documentation.

Happy automating!