How to Install Node.js on Ubuntu
Updated on Oct 5, 2023
Let’s start with the prerequisites you will need on your Ubuntu server before you install Node.js.
- Firstly, you will need an up-to-date operating system. It does not have to be the most recent version, but the more recent, the better. If it can be the newest version, then that would be best;
- Next is either a root-level user on the server or a user with sudo privileges. We recommend the latter option, as a user with full root-level access, is only optimal if you are entirely certain how to use it and what damage it can cause.
And that is it. Those are the only two prerequisites for installing Node.js on an Ubuntu server. Next, the actual installation. There are three methods, and they all involve running specific commands in the home directory of your server.
- Package Manager - Your Ubuntu server already has a package manager that can be used to install Node.js. You can use the commands below to install it:
- sudo apt update - This command will refresh the local package index;
- sudo apt install nodejs - The command will install Node.js automatically;
- node -v - This command will show you the version of the currently installed Node.js. If you receive an output that shows the version the installation was successful;
- sudo apt install npm - This will install NPM on the server. We recommend you install it now, even if you may not need it immediately.
- NodeSource - This method will allow you to install a specific version of Node.js in case you want to have something different than the newest one. To do that, run the following commands in the home directory of your server:
- curl -sL https://deb.nodesource.com/setup_16.x -o /tmp/nodesource_setup.sh - This command will download the Node.js PPA (Personal Package Archive) to your server. You can see the version is specified in the setup_16.x portion of the command. You can change it to the version you need, with a list of versions supported by NodeSource available on their website;
- sudo bash /tmp/nodesource_setup.sh - This command will execute the PPA you downloaded and install the Node.js package on your server. It will not install Node.js, however. The following command will do that;
- sudo apt install nodejs - Finally, run this command to install Node.js. You can also use the version command from the previous method to verify the installation. As with the method earlier, NPM is included in the installation.
- Node Version Manager - The Node Version Manager (NVM) is a flexible solution for installing and managing Node.js versions, as the name suggests. It allows you to install Node.js and control which version the server uses. If a version is missing, you can install it with a simple command. For our purposes, however, this is what you need to do to install Node.js via NVM:
- curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash - This will download the latest NVM version to your server and install it. At the time of writing, v0.39.5 is the latest one, but you should check the README.md file in this repository for the latest version. After the installation is successful, close or refresh your current terminal, after which you can use NVM. It has many commands, but we will focus on the ones that install Node.js;
- nvm list-remote - This will show you which Node.js versions are available for installation;
- nvm install v20.5.1 - The command will install the specified version of Node.js on your server;
- nvm list - This will show you all Node.js versions that are installed on your server;
- nvm use - Finally, you can use this command to switch between Node.js versions. Simply specify the version you saw in the previous command.
As you can see, installing Node.js on Ubuntu is pretty straightforward. In the next section, we will discuss installing it on RedHat distributions, where the process is just as easy.