Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save nwawrzyniak/58d087f9d0ad70c3e41584b077e1f13e to your computer and use it in GitHub Desktop.
Save nwawrzyniak/58d087f9d0ad70c3e41584b077e1f13e to your computer and use it in GitHub Desktop.
A tutorial to build any version of Node.js on the original Raspberry Pi.

First of all: Building a modern version of Node.js on (and for) the Raspberry Pi 1 / Raspberry Pi 1B takes a really long time. We're talking about around 2 and a half days.

As an alternative you should think about cross-compiling for the Raspberry Pi, as it is way faster once you got it working.

If you still want to compile Node.js on the RPi 1, here is how to do it:

  1. Go to the official Node.js download page and grab the latest source files (from the section "Source Code"). I recommend the latest long-term-support version (currently version 14.17.5). This will also include npm 6.14.14.

  2. Extract the .tar.gz file into a new directory (e.g. cd ~/Downloads/node-[version]). I will refer to the new directory as the source directory.

  3. Make sure you have gcc and g++ installed. Both are included in the package build-essential, so if you're unsure whether you have them installed already just run sudo apt-get install build-essential.

  4. Since the RAM of the RPi 1 is too small to handle the huge amount of files, it will likely freeze in the build process. However, we can prevent this by increasing the RPi's "Swap" size. The default Swap size on the RPi is only 100MB. To increase the Swap size, simply open the file /etc/dphys-swapfile with an editor of your choice. We will take nano, since it is preinstalled on the Pi: sudo nano /etc/dphys-swapfile. Next, change the line CONF_SWAPSIZE=100 to something like CONF_SWAPSIZE=1024 (for 1GB) or even CONF_SWAPSIZE=2048 (for 2GB). Restart the Swap by running /etc/init.d/dphys-swapfile restart or restart the entire Pi (e.g. with reboot).

  5. Use cd to change into the source directory and run ./configure.

  6. Now comes the actual building part. The creators recommend to build with the command make -j4 to build on four cores simulataneously. Since the RPi 1 only has one core, we run nice make -k instead. nice prevents the Pi from freezing by lowering the build process' priority and -k makes the build process continuable in case of a power failure or if something else goes wrong. After ~2.5 days we should finish with exit code 0.

Done. if you want to install Node.js now, simply run

  1. sudo nice make install -k. In this case sudo is required because the installation path defaults to /usr/local/bin/node which is owned by root. Alternatively you could change the ownership of the directory by running sudo mkdir /usr/local/bin/node and chown -fR pi /usr/local/bin/node (if you changed the default user of your Raspberry Pi make sure to replace "pi" by whatever your new user name is). nice and -k serve the same purpose as before.

  2. Now you should test the success of the installation. This can be done with which node or with node -v. If you see an output confirming your installation path or version: Congratulations! You successfully installed Node.js on a platform that does not officially support it.

@richsad
Copy link

richsad commented Dec 3, 2020

Thank you for this clear guide. I have a Raspberry Pi 4b w/4GB RAM. I am building all devtools except core C/C++ compiler from scratch. Most of the sites I found searching for "build node.js from source on Raspberry Pi 4" brought up tools to host an appropriate build image and install with apt-get. I encountered many problems and disinformation on sites. Maybe disinformation is harsh but certainly "no longer accurate" applies. One said: "Node can only be built with Python 2 and will not work with 3". False, but slightly true. I had already downloaded the latest stable version of Python which was 3.9.0 as of this writing. No info on this version existed in reference to the Raspberry Pi but after some initial difficulties I was able to build an appropriate openssl version and Python3.9.0. I configured "python3" to point to 3.9.0. This is where my node build problems began. I got a number of missing modules even though I had following all instructions with build-essential and other buid tools. I discovered this Raspberry Pi OS had a more modern Python version 3.7.3 whereas the Raspberry Pi OS that came with Noobs and even an image I downloaded from RPi site had 3.5.something. Had I had access to Python 3.7.3 all along I may not have felt the need to go all the way to 3.9.0. I continued to get missing modules in 3.9.0 even though it should have been good to go. I simply removed the alias to direct python3 to go to 3.9.0 and left it back to 3.7.3, a version I used successfully for a long time in production. Now with Python 3.7.3 and the Node.js 14.15.1 source code I was able to issue step 5 and 7. I expect this to ake less than 2.5 days on this RPi 4b/4GB but I have time to wait if it does. I've also done cross compilation of node for specific ARM processors in the past such as for an NVIDIA TK1 while we were early stages of building Jibo, the home social robot that was on the cover of Time Magazine Nov 27, 2017. The bring up of a new embedded device is a use case where you want to build as much as possible from source and down the road trim the distro and tools to bare essentials. It seems many people are over-spoiled by package managers and forget that open source code comes with source code. So why not securely build it from source to (or for in the case of cross compilation) the target device? Anyway long winded comment simply thanking you for having accurate and useful instructions.

@nwawrzyniak
Copy link
Author

@richsad Glad to hear this was helpful!

@kondo0429
Copy link

Thank you for this guide. I have installed node with Pi zero for 3 days. If this document were not here, I would have no way to use node 14.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment