Learn Computing from the Experts | The Rheinwerk Computing Blog

How to Compile Node.js from Source: A Step-by-Step Guide for Ubuntu

Written by Rheinwerk Computing | Jan 15, 2025 3:53:31 PM

In this blog post, you’ll learn how to operate Node.js not by using binary packages or by installing it using an installer for your system, but by creating a customized installation from the source files for your system.

 

This explanation is based on the assumption of a standard Linux Ubuntu desktop installation in version 20.04.2.0 LTS. The first step of the installation is that you need to download the Node.js source code. This is available as a zipped tar archive in the download area on https://nodejs.org/en/download/.

 

Before you can start compiling Node.js, you should make sure that you have a compiler. By default, this isn’t the case with the desktop variant of Ubuntu. However, you can install the compiler via the system’s package manager, as shown in this code.

 

$ sudo apt-get install g++

 

Once this requirement is met, you can unpack the source code package and go to the resulting directory, as shown here.

 

$ tar xvzf node-v16.8.0.tar.gz

$ cd node-v16.8.0

 

The first step of the actual compilation process consists of creating a makefile. This file will later serve as the basis for the make command to compile Node.js. However, you don’t need to create the makefile manually; instead, you can use the configure program. You can pass additional information that’s relevant to the installation to this command.

 

The available options include --without-npm to avoid installing the Node.js package manager as well, or --without-ssl to build Node.js without Secure Sockets Layer (SSL). Another option is to include different libraries such as V8, OpenSSL, or zlib as shared libraries instead of linking them statically. The --dest-cpu= option allows you to select the target architecture; possible values here are arm, ia32, and x64. You can also select the operating system for which Node.js will be built. In this case, pass the --destos= option the values win, mac, or linux, among others.

 

An important option for configure is --prefix. It specifies the location where Node.js should be installed. If you don’t use this option, the default /usr/local will be used. Relative to this path, all files from Node.js are copied to the system. You can use this option to maintain the possibility of installing multiple versions of Node.js on your system. By using the ./configure --help command, you’ll get a list of all available configuration options, including short explanations.

 

This code shows the next steps in the process.

 

$ ./configure --prefix=/opt/node/node-v16.8.0

...

$ make

...

$ sudo make install

...

 

The first command—configure—is used to create the makefile for Node.js, as mentioned earlier. You can use the --prefix=/opt/node/node-v16.8.0 option to install Node.js in a separate directory. This information is recorded in the makefile. The next command, make, builds on the information in the makefile and searches for the first target in it, that is, the description of what is to be done. Then it builds the project, in this case, Node.js, according to the information it finds there.

 

The goal of this process step is to get an executable version of Node.js but still in the directory you’re currently in, which is the unpacked source code. Finally, the last command—make install—ensures that the compiled files are copied to the correct location in the system. For this reason, you must also run make install with root permissions because a user should not normally have permissions to write to /opt, that is, to your current installation target.

 

Once these steps have been completed, you should see a new directory called node in the /opt directory in your system. This is where your Node.js now resides. You can already test this in the node-v16.8.0/bin subdirectory by executing the node command with the -v option. The output you get is the version number of your Node.js installation.

 

If this test has worked, you can remove the directory along with the unpacked source code from your system, as you no longer need either. As with the binary packages on Linux, the problem of this installation option is that you can’t execute Node.js system-wide from the command line, but must always specify the complete path to the executable file. The next section of this chapter goes into further detail about this problem.

 

Installing Node.js in the /opt directory enables you to run multiple versions of Node.js in parallel on your system. If you develop an application that should work on different versions of Node.js and want to test this application, such an approach is a good idea. For example, you can run a version of each major version of Node.js and test your application in that environment.

 

Normally, however, you probably want to have the latest version of Node.js available globally on your system. The easiest and fastest way to accomplish this is to create a symbolic link to the version of Node.js you want to have available globally and include that symbolic link in your system’s search path. This procedure allows you to switch to another version of Node.js by customizing this link.

 

This code demonstrates how you can create the link.

 

$ ln -s /opt/node/node-v16.8.0 /opt/node/node

 

In a final step, you need to extend the search path with an entry in the /etc/profile file, as shown.

 

PATH=$PATH:/opt/node/node/bin

export PATH

 

You now have a working installation of Node.js and can easily test it by entering the node command with the -v option in the command line. The output is the version number of the Node.js installation. You can now start developing your own applications with Node.js.

 

Editor’s note: This post has been adapted from a section of the book Node.js: The Comprehensive Guide by Sebastian Springer.