Quick Notes: Install Node-Red on Ubuntu
Welcome to Quick Notes. A collection of short installation or introduction articles.
A major part of most automation systems I build includes Node-Red. In today’s Quick Note we will install Node-Red.
While some use a Raspberry Pi, for future-proofing and performance I prefer to use a more powerful mini-computer like an Intel NUC. My operating system of choice is Ubuntu, so today’s install will be a quick walkthrough for that OS.
Pre-Requisites
There is only one package required to install Node-Red and that is Node.js Currently version 12 is needed. Nodesource provides a script to add the repositories and configure Apt for install. It can be run with the following command.
curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
After the script has completed you can run the actual install.
sudo apt-get install -y nodejs
To verify that npm and node.js have successfully installed run the command.
npm --version && node --version
The expected output will be similar to below.
Install Node-Red
Now that node.js and it’s included installer npm are installed. We can continue on to install Node-Red. Run the command.
sudo npm install -g --unsafe-perm node-red
This process will take a moment. When complete we can test launch Node-Red by running.
node-red
Node-Red runs by default on port 1880. We can test the install by opening a web browser to http://ServerIP:1880 in my example that is http://172.16.33.75:1880
If you get a screen similar to the one above, the default installation of Node-Red is complete. You can end the test with CTRL-C.
Password Protecting the Interface
By default Node-Red’s interface is not password protected. While this might be fine on a home network, I still don’t recommend leaving it that way.
The authentication system is a separate package called node-red-admin is can be installed with the following command.
sudo npm install -g node-red-admin
Next we need to generate a password hash for the configuration file. Run the command below and enter a password when prompted.
node-red-admin hash-pw
The command will output the hashed password similar to below. Copy it and save for the next step.
Now that we have the password hash we need to edit the settings file. Unless you did something unusual in the earlier steps you can open the file with this command
sudo nano ~/.node-red/settings.js
You are looking for the section of the file beginning with Securing Node-RED. It is near the middle of the file or you can use the search function by hitting CTRL-W. Uncomment the section as in the image below. Replace the default hash on the password line with the one you generated earlier.
Once you have made the appropriate changes.
Press Ctrl + x to exit.
When prompted hit Y then Enter to save and exit
Restart the interface as before and head back to your browser. You should now see a login interface like below. Unless you changed the username in settings.js. Enter admin as the username and the password you used to generate the hash.
node-red
Correcting Node-Red Permissions
Because the root user is disabled by default on Ubuntu we installed Node-Red using our regular account. But the installation script makes root the owner on most related files in your home directory. Consequently, if you want to use the Palette Manager. (Which is by far the easiest way to add nodes). We will need to correct that. Run the following commands.
cd ~/.node-red sudo find ~ -type d -user root -exec sudo chown -R $USER: {} +
Auto-Start Node-Red As A Service
Having to manually start Node-Red manually on reboots would be very inconvenient so we will create a service. Before continuing if you have left the test running end the test with CTRL-C.
We are going to install pm2 which is a process manager for node.js apps.
Install pm2
Install pm2
using the following command.
sudo npm install -g pm2
If you have followed the directions so far then Node-Red will be in the default location of /usr/bin/node-red. We can now start Node-Red in the background with the command.
pm2 start /usr/bin/node-red -- -v
You can double check the status with the following commands.
pm2 info node-red pm2 logs node-red
Configure Auto-Start
Assuming all was well above, we need run two commands to generate the appropriate shell command to create the systemd service.
pm2 save pm2 startup systemd
The second command outputs the command line needed to create the service. It will vary depending on the user who is currently logged in but will be similar to below.
sudo env PATH=$PATH:/usr/bin /usr/lib/node_modules/pm2/bin/pm2 startup systemd -u pi --hp /home/pi
If all goes as expected you will see output like:
[PM2] [v] Command successfully executed.
Now restart the server and test you can get to the web interface. If you can then the service is successfully enabled and working.
Conclusion
Now that Node-Red is installed you are able to use all of the many functions it has to offer.
- We have one article on integrating with the Phoscon/Deconz Zigbee gateway: https://polargeek.com/deconz-5/
- There is also the Node-Red Library which contains example flows as well a repository of nodes
- You can check out this playlist from Opto Video. The first few are a great introduction: https://www.youtube.com/playlist?list=PLKYvTRORAnx6a9tETvF95o35mykuysuOw