How to Install Node.JS on Ubuntu 22.04
Traducciones al EspañolEstamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.
Before You Begin
If you have not already done so, create a Linode account and Compute Instance. See our Getting Started with Linode and Creating a Compute Instance guides.
Follow our Setting Up and Securing a Compute Instance guide to update your system. You may also wish to set the timezone, configure your hostname, create a limited user account, and harden SSH access.
sudo. If you’re not familiar with the
sudocommand, see the Users and Groups guide.
There are a number of options to configure Ubuntu 22.04 for Node.JS. These sections discuss the three most popular techniques.
Installing Node.JS from the Default Repositories
The following steps show the simplest method to get the current Node.JS implementation for Ubuntu 22.04 using the default repositories.
sudo apt install -y nodejs
Once the install is complete, verify your Node.JS installation:
This information is important, as you may need a different version of Node.JS to perform a particular task.
Optional: Enter the following command to install the Node Package Manager (NPM), which provides additional flexibility for Node.JS management:
sudo apt install -y npm
NoteSome scripts also rely on NPM to verify Node.JS features or perform other tasks.
Optional: Verify your NPM version:
Installing a Specific Version
A task may require a specific version of Node.JS. The example steps below show how to install the most current Node.JS version 16.x setup. However, the
16 in can be replaced with any other major supported version, including
Obtain the Node.JS source:
curl -sL https://deb.nodesource.com/setup_16.x | sudo -E bash -
Install the 16.x version of Node.JS:
sudo apt-get install -y nodejs
NoteThis process also automatically installs NPM.
Verify that the correct version of Node.JS is installed:
The output should display
Ensure that the most current version of NPM is installed:
sudo npm install -g npm@latest
Check the NPM version:
The output should display version
Node.JS for Developers
The Node Version Manager supports multiple versions of Node.JS on a single system. This is so it can test scripts using multiple Node.JS versions. You can find the procedure for working with NVM here.
Node.JS provides a powerful scripting engine that could be misused by others. Installing Node.JS without following best practices is an open invitation to hackers. This list provides basic steps you can use to make your instance of Node.JS more secure:
Do not run Node.JS as the root user: Assume that a hacker gains access to your system. Running code as the root user means the hacker has a valuable resource to break everything else down. Instead, run Node.JS with only the rights needed for the specific application in question.
Use strong authentication: The first line of defense for your application is to ensure that the user is not a hacker. The best practice is to use a tool such as Okta or OAuth for authentication.
Use a reverse proxy: A reverse proxy is a specialized kind of web server that makes it possible to do things like limit the number of requests a Node.JS application can receive. Basically, the reverse proxy receives the user request, vets it to ensure the request is valid, and only then passes it to the Node.JS application.
Set package access levels: One of the reasons to install a package manager like NPM is to control who can access packages and how they do so. In fact, NPM comes with a wealth of commands.
Validate user inputs: Node.JS is vulnerable to injection-based attacks, so it’s essential to verify that the user is sending data, and not an executable script.
Keep secrets secret: Storing sensitive information like database connection strings and API keys in code is a bad idea. Using a specially configured library like dotenv makes it possible to load and store environment variables in a secure manner.
Keep error messages generic: Error messages such as “Password Invalid” provide too much information. It tells the hacker that the name supplied was valid and reduces the amount of work the hacker must perform to gain access to the system. Use a message like “Invalid Input” instead. This conveys enough information for the user to make a correction without giving too much away.
Add HTTP response headers: An HTTP response header adds security that forces the user’s browser to take various actions. These include relying on strict transport security, displaying content in frames, and preventing Multipurpose Internet Mail Extensions (MIME) type from changing.
Maintain server-side logs and monitor them: Server-side logging ensures that administrators know what is going on with their servers. Keeping track of every transaction may seem like overkill, but it often surfaces patterns in transactions. These patterns can show if a hacker is interested in your site.
Check code using a security linter: A linter is an essential tool that helps improve code. A security linter specifically looks for security issues in code. A security linter helps locate the vast majority of security issues. There is no guarantee that a hacker won’t find another way in, so use the other methods in this list as well to secure your application.
Starting, Stopping, and Restarting Node.JS on Ubuntu 22.04
Working with Node.JS is easier when NPM is installed. Here are some useful commands to interact with installed packages:
npm ls: List the installed packages to determine if you need to install a package before you run it.
npm run-script: Starts the specified script.
npm start: Starts the specified package.
npm stop: Stops the specified package.
npm restart: Restarts the specified package.
NPM commands are the best way to manage scripts and packages and there are several to help do so.
Should a rogue process not work correctly with NPM or Node.JS, there is a three-stop process to stop it:
List all of the running node processes:
sudo ps -ef | grep node
Locate the node to be eliminated and obtain its process identifier (PID) from the second column of the following command’s output:
Stop the errant process:
kill -9 <PID>
-9is a kill signal (with
-15being the other common, less extreme, kill signal). A listing of the various
killcommands can be obtained with:
To remove the current version of Node.JS (and NPM, if installed), enter the following command:
sudo apt remove -y nodejs
To remove only the current version of NPM, use the following command:
sudo apt remove -y npm
Node.JS has a lot to offer in running server-side code. When paired with a package manager, it’s an unbeatable combination that makes the work of both administrators and developers easier. There are two main considerations for Node.JS use. First, to obtain and install the correct version for a particular need. Second, to then secure the installation in order to keep hackers at bay.
You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.
This page was originally published on