Node.js is a JavaScript general-purpose programming framework that allows users to quickly develop network applications. Node.js makes development more consistent and integrated by employing JavaScript on both the front and backend.
This post will teach you how to install Node.js on an Ubuntu 18.04 server using three alternative techniques.
Prerequisites
This tutorial assumes you’re running Ubuntu 18.04. Before you begin, make sure your system has a non-root user account with sudo capabilities.
Apt Installing Node.js from Default Repositories
In its default repository, Ubuntu 18.04 includes a version of Node.js that can be used to give a consistent experience across various systems. The version in the repositories at the time of writing is 8.10.0. This will not be the most recent version, but it should be reliable and sufficient for first language exploration.
You can obtain this version by using the apt package manager. Update your local package index:
$ sudo apt update
Install Node.js now:
$ sudo apt install nodejs
Verify that Node.js was successfully installed by inquiring about its version number:
$ node –v
Output
v8.10.0
This is all you need to do to get started with Node.js if the package in the repository meets your requirements. You need also install npm, the Node.js package manager, in most circumstances. You can use apt to install the npm package:
$ sudo apt install npm
This will allow you to install Node.js modules and packages.
You have now installed Node.js and npm using apt and the default Ubuntu software repositories. You may, however, opt to work with various Node.js versions, package archives, or version managers. The following phases will go through these elements, as well as more flexible and durable installation methods.
Apt Installation of Node.js Using a NodeSource PPA
To install a more recent version of Node.js, add the NodeSource PPA (personal package archive). This will include more up-to-date Node.js versions than the official Ubuntu repository and will allow you to pick between different platform versions.
Install the PPA first to gain access to its contents. Curl the installation script for your selected version from your home directory, making sure to replace 17.x with your preferred version string (if different):
You can investigate the contents of this script with nano (or your choice text editor) if you want:
$ nano /tmp/nodesource_setup.sh
Exit the text editor after you’re sure the script is safe to run. If you used nano, simply hit CTRL + X to exit. The script should then be run with sudo:
$ sudo bash /tmp/nodesource_setup.sh.
The PPA will be added to your settings, and your local package cache will be automatically updated. You can now install the Node.js package as described in the previous section:
$ sudo apt install nodejs
Run node using the -v flag to ensure you’ve installed the new version:
$ node –v
Output
v17.3.0
This nodejs package, unlike the one in the usual Ubuntu package repositories, includes both node and npm, so you don’t need to install npm separately.
To keep track of updates, npm uses a configuration file in your home directory. When you run npm for the first time, it will be created. To check that npm is installed and to build the configuration file, run the following command:
$ npm –v
Output
8.3.0
Some npm packages (those that require compiling code from source, for example) require the installation of the build-essential package:
$ sudo apt install build-essential
You now have the tools you need to work with npm packages that need compilation from source code.
You installed Node.js and npm using apt and the NodeSource PPA in this section. The Node Version Manager will then be used to install and manage different versions of Node.js.
Using the Node Version Manager to Install Node
Another option for installing Node.js is to utilise nvm, the Node Version Manager (NVM). nvm operates at the level of an independent directory within your home directory, rather than at the level of the operating system. This means that you can install numerous self-contained Node.js versions without affecting the overall system.
Managing your environment with nvm allows you to access the most recent Node.js versions while retaining and managing prior releases. It is, however, a separate utility from apt, and the versions of Node.js you manage with it are distinct from the versions you manage with apt.
Before piping the command to bash, you should always audit the script to ensure it isn’t doing anything you don’t agree with. Remove the | bash element at the end of the curl command to accomplish this:
Review the output and ensure that you are happy with the adjustments it is making. When you’re finished, repeat the command with | bash appended at the end. The URL you use will change based on the newest version of NVM, but for the time being, you can download and execute the script by running the following:
It’s a large list, but you can install Node by entering any of the released versions listed. For instance, to obtain version v16.13.1, execute the following:
$ nvm install v16.13.1
Output
Now using node v16.13.1 (npm v8.1.2)
Occasionally, nvm will use the most recently installed version. However, you can instruct nvm to use the version you just downloaded (if it differs):
$ nvm use v16.13.1
Run the following commands to determine the current version:
$ node –v
Output
v16.13.1
If you have numerous Node versions installed, you can retrieve a list of them by running ls:
You can alternatively select one of the following versions as your default:
$ nvm alias default 16.13.1
Output
default -> 16.13.1 (-> v16.13.1)
When a new session starts, this version is automatically picked. You can also use an alias to refer to it, like in the following command:
$ nvm use default
Output
Now using node v16.13.1 (npm v8.1.2)
Every version of Node will maintain track of its own packages, which may be managed via npm.
You can also have npm install packages to the Node.js project’s./node modules directory. Install the express module using the following syntax:
$ npm install express
Output added 50 packages, and audited 51 packages in 4s
2 packages are looking for funding run `npm fund` for details
found 0 vulnerabilities npm notice npm notice New minor version of npm available! 8.1.2 -> 8.3.0 npm notice Changelog: https://github.com/npm/cli/releases/tag/v8.3.0 npm notice Run npm install -g npm@8.3.0 to update! npm notice
If you want to install the module globally, making it available to other projects running the same Node.js version, use the -g flag:
$ npm install -g express
Output added 50 packages, and audited 51 packages in 1s
2 packages are looking for funding run `npm fund` for details
found 0 vulnerabilities
This will install the package in the following locations:
You can run commands from the command line if you install the module globally, but you must link the package into your local sphere to use it from within a programme:
$ npm link express
Run the following commands to learn more about the choices available with nvm:
$ nvm help
You’ve successfully installed Node by utilising the Node Version Manager, nvm, to install and manage different Node versions.
Removing Node.js
Depending on the version you want to target, you can remove Node.js with apt or nvm. At the system level, you will use apt to delete the default repository version. This command uninstalls the software while keeping the configuration files. This is useful if you intend to reinstall the software later:
$ sudo apt remove nodejs
If you do not want to save the configuration files for future usage, run the following command to uninstall the package and delete the configuration files associated with it:
sudo apt purge nodejs
Finally, you can uninstall any unwanted packages that were installed automatically with the removed package:
$ sudo apt autoremove
To uninstall a Node.js version that you have activated with nvm, first establish whether the version you want to delete is the current active version:
$ nvm current
If the version you want is not the current active version, you can use the following command:
$ nvm uninstall node_version
Output:
Uninstalled node node_version
This command will uninstall the selected Node.js version.
If the version you want to uninstall is the current active version, you must first disable nvm before you can make changes:
$ nvm deactivate
You can now uninstall the current version with the previously used uninstall command. Except for the cached files that can be used for reinstallation, this eliminates all files connected with the targeted version of Node.js.
Conclusion
There are several ways to get Node.js up and running on your Ubuntu 18.04 server. Your circumstances will determine which strategy is best for you. While utilising the bundled version from the Ubuntu repository is one option, using nvm or a NodeSource PPA provides more freedom.