Skip to content

Instantly share code, notes, and snippets.

@dev-kperera
Last active October 4, 2025 11:38
Show Gist options
  • Select an option

  • Save dev-kperera/3d7a074a39226b7ae662ab5bef35c0a4 to your computer and use it in GitHub Desktop.

Select an option

Save dev-kperera/3d7a074a39226b7ae662ab5bef35c0a4 to your computer and use it in GitHub Desktop.
Script to installs n8n on a server using NVM and npm.
#!/bin/bash
set -e
# --- This script installs n8n on a server using NVM and npm. ---
# This method avoids Docker and can be more resource-efficient.
echo "### Starting n8n installation via NVM/npm... ###"
# 1. Update System Packages and Install Prerequisites
echo "### Step 1: Updating system packages and installing build-essential... ###"
sudo apt-get update
sudo apt-get install -y curl build-essential
# 2. Install NVM (Node Version Manager)
echo "### Step 2: Installing NVM... ###"
# The version might change; this pulls the latest install script.
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
# Export NVM's environment variables to the current shell session
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
echo "### NVM installed. ###"
echo "NOTE: You may need to restart your terminal session for 'nvm' command to become available."
# 3. Install Node.js
# n8n requires a specific LTS version of Node.js. We'll use Node 18.
echo "### Step 3: Installing Node.js v18 (LTS)... ###"
nvm install 18
nvm use 18
nvm alias default 18
echo "### Node.js v18 has been installed and set as default. ###"
# 4. Install n8n
echo "### Step 4: Installing n8n globally with npm... ###"
npm install n8n -g
echo "### n8n has been installed successfully. ###"
# 5. Set up systemd service to run n8n on startup
echo "### Step 5: Creating a systemd service for n8n... ###"
# Find the path to the installed n8n executable
N8N_PATH=$(which n8n)
CURRENT_USER=$(whoami)
# Get the home directory of the current user
USER_HOME=$(eval echo ~$CURRENT_USER)
# Create the systemd service file
sudo bash -c "cat > /etc/systemd/system/n8n.service" << EOL
[Unit]
Description=n8n
Requires=network.target
After=network.target
[Service]
Type=simple
User=$CURRENT_USER
WorkingDirectory=$USER_HOME
ExecStart=$N8N_PATH
Restart=on-failure
RestartSec=10
[Install]
WantedBy=multi-user.target
EOL
echo "### systemd service file created. ###"
# 6. Enable and start the n8n service
echo "### Step 6: Enabling and starting the n8n service... ###"
sudo systemctl daemon-reload
sudo systemctl enable n8n
sudo systemctl start n8n
echo ""
echo "################################################################"
echo "### n8n Installation and Setup Complete! ###"
echo "################################################################"
echo ""
echo "n8n is now running as a background service."
echo "You can check its status with: sudo systemctl status n8n"
echo ""
echo "You can now access your n8n instance at: http://<your-server-ip>:5678"
echo ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment