This guide will show you how to install and use Node.js on Ubuntu 20.04.
Node.js on Ubuntu
Node.js is very popular for scaling backend functionalities. In the case of Ubuntu, there are several sources to grab Node.js. Different methods install different versions of Node.js. You can also manually choose which one to choose.
Use the method that best suits your needs.
Install Node.js from Ubuntu repositories
This is the standard method for installing Node.js on Ubuntu. For most users, this will be more than enough. The only downside is you may not get the latest version of Node.js.
Installation is super easy. Update the APT cache and install Node.js along with npm (Node Package Manager).
Let’s run a quick test to verify the installation.
Install Node.js from NodeSource PPA
NodeSource is a dedicated PPA that offers multiple versions of Node.js. I recommend this method to others as it gives you more control. For advanced users, you can also specify the exact version of Node.js to install. At the time of this writing, NodeSource PPA is hosting Node.js v10, v12, v13, and v14.
Here I show how to configure NodeSource PPA for Node.js v14. If you’d like to install a different version of Node.js, see the NodeSource Readme for proper instruction.
First, make sure curl is installed on your system.
Now run the NodeSource installation script.
Voila! NodeSource PPA for Node.js v14 has been configured successfully! Install Node.js.
Verify the installation by checking the version of Node.js.
Install Node.js with nvm
It’s an interesting way to install Node.js. The nvm (Node Version Manager) is a tool with which several versions of Node.js together with the associated node packages can be installed and managed independently of one another. Check out nvm on GitHub.
Run one of the following commands to install nvm. Each of them will download and run the nvm installer script.
Close the terminal and open it again. This will load nvm. Otherwise you can reload the bashrc file manually.
Run the following command to verify the installation.
It’s time to use nvm. First, take a look at the available versions of Node.js. This will print a long list.
To install the version you want, use the command below. In this example, the command installs Node.js v14.9.0.
With nvm it is possible to install a version based on its aliases. For example, run this command to install the latest version of LTS Erbium.
The following command lists all installed Node.js versions.
If multiple versions are installed, nvm allows you to switch to a different one. First, check the current Node.js version.
Change the standard Node.js to a different version.
The version alias can also be used instead of the version number.
Test the change.
The following command sets the default version of Node.js.
Install Node.js from the source
As mentioned earlier, Node.js is an open source project. We can get the source code and build and use Node.js manually. However, this approach is highly recommended if you plan to use Node.js for production purposes.
Before getting into the process, it’s important to note Python. Node.js supports both Python 2 and Python 3. Node.js uses the installed. If both Python 2 and Python 3 are installed, the latter is used. If only Python 2 is installed, then Python 2 is used.
First, install the build dependencies. Run the following command. For Python 3 users, the python3-distutils Package is necessary.
Now download the source code. In this example I am compiling Node.js v12.18.3 (contains npm 6.14.6). Download the Node.js source code.
Extract the source code.
It’s time to build Node.js. Run the configuration script.
Start the compilation process. The “-j” should execute make in multithreaded mode. The “nproc” part indicates the number of available CPU cores.
Install Node.js.
Let’s check the installation. Check the Node.js and npm versions.
$ npm -v
Using Node.js
Node.js offers a lot of features and functionalities. It is a runtime for JavaScript. It’s up to you to leverage JavaScript to get the most out of Node. Here I show the very basic ways to use Node.js.
First, get a sample JavaScript. The following code was taken from W3 schools.
$ http.createServer(function (req, res) {
$ res.Print head(200, {‘Content Type’: ‘text / html’});
$ res.end(‘Hello World!’);
}).hear(8080);
Run the JavaScript code using Node.js.
To get the output, access your computer on port 8080.
Last thought
Node.js is a powerful and popular solution. There are several approaches to installing it on Ubuntu. Your situation will determine which method is best for you. While using the standard Node.js from the Ubuntu repo offers the simplest solution, NodeSource and nvm offer more flexibility.
As for how to use Node.js, there are tons of resources online to show you how to use the various Node functions in your JavaScript code. W3 schools is a good place to start your journey.
Have fun calculating!