How to Install Node.js and NPM on Debian 12
Introduction
Node.js is an open-source, cross-platform JavaScript runtime environment for developing web applications. The Node Package Manager (NPM) builds and manages dependency packages for Node.js. Node.js and NPM allow you to use JavaScript for server-side scripting.
This article explains how to install Node.js and NPM on Debian 12. You will install Node.js and manage multiple versions using the NVM (Node Version Manager).
Prerequisites
Before you begin:
- Deploy a Debian 12 Cloud Compute instance on Vultr.
- Access the instance using SSHas a non-root user with sudo privileges.
- Update the instance.
Choose a Node.js Version
The Node.js release cycle includes a new version that enters the Current release status for six months. Even-numbered versions transition to an Active LTS (Long-Term Support) status that lasts for 12 months followed by a maintenance status for 18 months. When selecting a Node.js version, consider the release stability, performance, features, and compatibility with your project environment.
The following Node.js releases are available when selecting a version:
- Current Release: Includes the latest updates, improvements, and features. It's ideal for applications that require new features.
- LTS Release: Marks a stable Node.js version supported for an extended period and receives continuous feature updates. LTS releases are suitable for production environments because they guarantee critical fixes to common bugs.
- Previous Releases: Enables compatibility with legacy projects.
Install Node.js and NPM
Node.js is available in the default package repositories on Debian 12, but the available version might be out of date. Follow the steps below to enable the NodeSource PPA repository and install Node.js:
Download the latest Nodesource PPA repository script for your target Node.js version. For example, Node.js version
22.x
.console$ curl -fsSL https://deb.nodesource.com/setup_22.x -o nodesource_setup.sh
Visit the official NodeSource distributions to verify the latest Node.js repository information packages.
Run the script to install the repository and update your server's package information index.
console$ sudo -E bash nodesource_setup.sh
Install Node.js and NPM.
console$ sudo apt-get install -y nodejs
View the Node.js version.
console$ node -v
Your output should be similar to the one below.
v22.5.1
View the NPM version.
console$ npm -v
Output:
10.8.2
Test Node.js
Follow the steps below to test your Node.js installation and run a sample HTTP server application that outputs a Greetings from Vultr
message when you access it using a web browser.
Create a new project directory. For example,
nodeapp
.console$ mkdir nodeapp
Switch to the
nodeapp
directory.console$ cd nodeapp
Create a new
greetings.js
file using a text editor likevim
.console$ vim greetings.js
Add the following contents to the
greetings.js
file.jsimport { createServer } from 'node:http'; const server = createServer((req, res) => { res.writeHead(200, { 'Content-Type': 'text/plain' }); res.end('Greetings from Vultr'); }); server.listen(3000, '127.0.0.1', () => { console.log('Listening on 127.0.0.1:3000'); });
Save and close the file.
The above application code starts a new HTTP server that listens for incoming requests on port
3000
and displays aGreetings from Vultr
message.Run the application using Node.js as a background process.
console$ node greetings.js &
Output:
Server running at http://127.0.0.1:3000/
Test access to the application using Curl on the host port
3000
.console$ curl http://127.0.0.1:3000
Output:
Greetings from Vultr
Install Multiple Versions
NVM (Node Version Manager) is a command-line tool for installing, managing, and switching between different Node.js versions. Follow the steps below to install NVM and run multiple Node.js versions:
Download the latest NVM installation script.
console$ wget https://raw.githubusercontent.com/nvm-sh/nvm/master/install.sh
Run the script to install NVM.
console$ bash install.sh
Output:
=> Downloading nvm as script to '/root/.nvm' => Appending nvm source string to /root/.bashrc => Appending bash_completion source string to /root/.bashrc => Close and reopen your terminal to start using nvm or run the following to use it now: export NVM_DIR="$HOME/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
Apply the NVM changes to your active shell environment.
console$ source ~/.bashrc
List all Node.js versions available for download.
console$ nvm ls-remote
Output:
v0.1.14 ... v20.15.1 (Latest LTS: Iron) ... v22.0.0 ... v22.5.0 v22.5.1
Install a specific Node.js version and NPM on your server. For example, Node.js version
22.5.1
.console$ nvm install 22.5.1
Output:
Downloading and installing node v22.5.1... Now using node v22.5.1 (npm v10.8.2) Creating default alias: default -> 22.5.1 (-> v22.5.1)
Install another Node.js version, such as
20.15.1
.console$ nvm install 20.15.1
Switch to a specific Node.js version. For instance,
22.5.1
.console$ nvm use 22.5.1
Output:
Now using node v22.5.1 (npm v10.8.2)
Install the latest Node.js LTS version on your server.
console$ nvm install --lts
Install a specific LTS version such as
lts/iron
.console$ nvm install --lts/iron
View the active Node.js version.
console$ node -v
Output:
v20.15.1
View the active NPM version.
console$ npm -v
Output:
10.7.0
Conclusion
You have installed Node.js and NPM on Debian 12. You installed multiple versions to enable the development of web applications that require a specific version. Node.js lets you to develop and deliver web applications with all necessary dependency packages using the Node Package Manager (NPM). For more information, visit the Node.js documentation.