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.
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.
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 npmSome scripts also rely on NPM to verify Node.js features or perform other tasks.
Optional: Verify your NPM 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 nodejsThis 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
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 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.
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.
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