In previous tutorials we have seen how to install Node-RED on Windows, MacOS and Raspbian (Raspberry PI). In this tutorial we will install Node-RED on Ubuntu 16.04 LTS (Long Term Support) on an Atom-based mini PC. We will install Node.js, npm (the package manager of Node.js) and Node-RED.
Install Node.js and npm
Installing Node.js is very simple on Ubuntu 16.04 LTS because it is included in the default repositories. In a Terminal, run the following command:
sudo apt-get install nodejs-legacy
This command installs Node.js version v4.2.x LTS which means that this version will be maintained for another 30 months from the release date, ie October 12, 2015.
After installation, check the installed version.
$ node -v v4.2.6
Node.js uses a package manager called npm (Node Package Manager). It allows you to install and manage packages. Since Node-RED is a “over-layer graphics” of Node.js, it is necessary to install it.
sudo apt-get install npm
When the installation is complete, check the version. If the version is returned without error, you can continue the installation of Node-RED.
$ npm -v 3.5.2
Install Node-RED on Ubuntu (16.04 LTS +)
Node-RED installs using the npm package manager. We will take advantage of this to install the node-red-admin module which adds some administration tools for Node-RED.
sudo npm install -g --unsafe-perm node-red node-red-admin
By default, npm installs the packages in the current directory. The -g option installs packages globally, meaning that they will be available for all Node.js. In this case, they are installed in the / usr / local / bin directory. The –unsafe-perm option overrides any errors that may occur during installation. For example, when certain modules are to be compiled at the time of installation.
Node-RED uses port 1880 by default. It must therefore be authorized.
sudo ufw allow 1880
And now, you can start Node-RED with the command node-red .
To access it, open a web browser and enter the following URL in the address bar http://localhost:1880
Run Node-RED automatically at startup
To be able to start Node-RED automatically when Ubuntu starts, we will create a script that will be launched by systemd.
Open an empty file named node-red.service
sudo nano /etc/systemd/system/node-red.service
Paste the contents of this script and then replace the USER key with your username. Save the file with CTRL+X and then Y.
[Unit] Description=Node-RED After=syslog.target network.target [Service] ExecStart=/usr/local/bin/node-red-pi --max-old-space-size=128 -v Restart=on-failure KillSignal=SIGINT # log output to syslog as 'node-red' SyslogIdentifier=node-red StandardOutput=syslog # non-root user to run as WorkingDirectory=/home/UTILISATEUR/ User=UTILISATEUR Group=UTILISATEUR [Install] WantedBy=multi-user.target
Some explanations on the configuration:
- The first section (Unit) indicates that the service should start after the network and the syslog service are up and running.
- The [service] section. ExecStart indicates the program started and the resources allocated for its operation.
- Restart = on-failure to restart Node-RED in case of crash
- The events will be identified in the system log (StandardOutput = syslog) with the node-red identifier.
Now that the configuration file is ready, we tell systemd to run it at startup.
sudo systemctl enable node-red
Let’s start it manually (Do not forget to stop Node-RED if another instance is already running).
sudo systemctl start node-red
Now that Node-RED works as a service, you have to go through systemd to stop it manually.
sudo systemctl stop node-red