Introduction

Node.js is an open-source, cross-platform JavaScript runtime environment that executes JavaScript code outside a web browser. It allows developers to develop command-line tools and writing server-side code. Node.js unifying web application development using a single programming language rather than a different language for server-side and client-side code.

In this tutorial, we will see how to Install a specific or latest version of Node.js using apt with NodeSource PPA.

Prerequisites

In this tutorial, I am assuming you are using Ubuntu 20.04 and you have a non-root account with sudo privileges set up on your system

Install a specific or latest version of Node.js using apt with NodeSource PPA

To install a specific or latest version of Node.js we can use PPA (Personal Package Archive) which is maintained by NodeSource. PPA has more version of Node.js available than the official Ubuntu default software repositories.

To get access to the PPA package we have to install it first. To retrieve the installation script for the our prefered version we will use curl. In this tutorial, we will install version 14.x because this is the LTS version at the time of writing this tutorial, If you want to install a different version then replace 14.x with your prefered version. To know more about available versions refer to the NodeSource Documentation.

                    
                        cd ~
                        curl -sL https://deb.nodesource.com/setup_14.x -o nodesource_setup.sh
                    
                

If you want to look into the downloaded script you can use nano or any other text editor you prefer.

                    
                        nano nodesource_setup.sh
                    
                

When you are satisfied that the script is safe to run, exit your editor, then run the script with sudo:

                    
                        sudo bash nodesource_setup.sh
                    
                

The PPA will be added to configurations and the local package cache will be updated, now we can install Node.js package in the same way as described in Way 1.

                    
                        sudo apt install nodejs
                    
                

Once the installation has been completed, Let's verify the new installation by check its version number.

                    
                        node -v
                    
                
                    
                        Output
                        v14.17.0
                    
                

When we install Nodejs package using NodeSource then we don't have to install npm separately because it contains both node binary and npm. To confirm you can check npm version as well:

                    
                        npm -v
                    
                
                    
                        Output
                        6.14.13
                    
                

Conclusion

You have successfully installed Node.js and npm using apt and the NodeSource PPA.