This page looks best with JavaScript enabled

How to install n8n and run it during boot thanks systemd unit

 ·  ☕ 3 min read  ·  🐧 sysadmin
  1. Create a file:
1
touch n8n-install.sh
  1. Put the below content into the file:
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/bin/bash -e

log() {
  echo "[$(date +'%Y-%m-%d %H:%M:%S')]: $*"
}

# Function to display spinner
display_spinner() {
  local pid=$1
  local spin='-\|/'

  log "Loading..."

  while [ "$(ps a | awk '{print $1}' | grep $pid)" ]; do
    local temp=${spin#?}
    printf "\r [%c]" "$spin"
    local spin=$temp${spin%"$temp"}
    sleep 0.1
  done
  printf "\r"
}

row=2
col=2

countdown() {
        msg="Wait 30 seconds for n8n ${1}..."
        clear
        tput cup $row $col
        echo -n "$msg"
        l=${#msg}
        l=$(( l+$col ))
        for i in {30..1}
        do
                tput cup $row $l
                echo -n "$i"
                sleep 1
        done
}

execute_command() {
  local cmd="$*"
  log "Executing: $cmd"
  bash -c "$cmd" &
  display_spinner $!
}

error_exit() {
  log "$1"
  exit 1
}

check_root(){
  echo "This quick installer script requires root privileges."
  echo "Checking..."
  if [[ $(/usr/bin/id -u) -ne 0 ]]; 
    then
      echo "Not running as root"
      exit 0
  else
      echo "Installation continues"
  fi

  SUDO=
  if [ "$UID" != "0" ]; then
        if [ -e /usr/bin/sudo -o -e /bin/sudo ]; then
                SUDO=sudo
        else
                echo "*** This quick installer script requires root privileges."
                exit 0
        fi
  fi
}

update_upgrade(){
  echo 'updating system'
  sudo apt update
  sudo apt upgrade -y
}

check_packages(){
  if [[ $(command -v build-essential) ]]; then
    echo "build-essential already installed"
  else
    sudo apt install build-essential -y 
  fi

  if [[ $(command -v python3) ]]; then
    echo "python3 already installed"
  else
    sudo apt install python3 -y
  fi

  if [[ $(command -v nodejs) ]]; then
    echo "nodejs already installed"
  else
    sudo apt install nodejs -y
  fi

  if [[ $(command -v npm) ]]; then
    echo "npm already installed"
  else
    sudo apt install npm -y
  fi
}

install_n8n(){
  execute_command "echo 'install n8n globally'"
  npm install n8n -g
}

adding_systemd_entry(){
  echo 'adding systemd entry'
  sudo cat > /etc/systemd/system/n8n.service <<EOF
[Unit]
Description=n8n - Easily automate tasks across different services.
After=network.target

[Service]
Type=simple
User=adrian
ExecStart=/usr/local/bin/n8n start --tunnel
Restart=on-failure

[Install]
WantedBy=multi-user.target
Alias=n8n.service
EOF
}

n8n_service(){
  echo 'reloading, enabling on boot and starting n8n'
  sudo systemctl daemon-reload
  sudo systemctl enable n8n
  sudo systemctl start n8n
}

n8n_status(){
  systemctl status n8n.service
}

main(){
  check_root
  update_upgrade
  check_packages
  install_n8n
  adding_systemd_entry
  n8n_service
  countdown
  n8n_status
}

main
  1. Save and exit.

  2. Add permissions:

1
chmod +x n8n-install.sh
  1. Execute the below script to make it work:
1
./n8n-install.sh
  1. Check the IP address of the machine, where you execued the script using the below command:
1
hostnbame -I
  1. Open the browser and paste the IP address and port
http://IP_ADDRESS:5678
  1. Create login and password for n8n.

  2. That’s it. n8n is working. Enjoy!

Support the author with

sysadmin
WRITTEN BY
sysadmin
QA & Linux Specialist