How to Install Node.js on Debian 12 (through APT/NVM/Volta)

Today, I will show you how to install Node.js on the Debian 12 system, especially for sysadmin/ops and developers.

From a sysadmin perspective, you may want to install Node.js as a dependency for your applications. So you you always use the LTS version of Node.js. As for developers, you can install and manage Node.js using tools like NVM or Volta.

tl;dr:

  • For sysadmin or ops, you can install the Node.js directory via APT or using a third-party repository such as Nodesource.
  • For Node.js developers, you can install and manage Node.js via NVM (Node Version Manager) or Volta. This allows you to install and manage multiple Node.js versions for your projects.

Prerequisites

To get started, ensure you have a Debian 12 system initialized with the non-root user as sudo/administrator privileges.

Installing Node.js for Sysadmin or DevOps

The following methods are recommended for sysadmin or ops that need to install Node.js for deploying applications. Make sure to check which Node.js version that needed by your applications.

Installing Node.js via the Debian repository

To install Node.js via the Debian repository, follow these actions:

1. First, run the command below to update the Debian package list.

sudo apt update

2. Once updated, run the apt install command below to install nodejs and npm to your Debian system. At this time, the Debian 12 repository offer Node.js 18 and NPM (Node Package Manager) 9.2.

sudo apt install nodejs npm

Enter Y to proceed with the installation.

Installing Node.js through Debian repository
Installing Node.js through Debian repository

3. After installation is completed, run the command below to verify Node.js and NPM versions.

node --version
npm --version

As seen in the following, the Node.js 18.19.0 and NPM 9.2 are installed via the official Debian repository.

Checking Node.js and NPM version
Checking Node.js and NPM version

Installing Node.js (21, 20, 18) from Nodesource repository

To install Node.js on Debian via the Nodesource repository, complete the following steps:

1. Select the preferred Node.js version and add the Nodesource repository by executing the command below. This will download and run the Nodesource setup script, then add the Nodesource repository and update your package list.

# repository for node.js 21
curl -fsSL https://deb.nodesource.com/setup_21.x | bash -

# repository for node.js 20
curl -fsSL https://deb.nodesource.com/setup_20.x | bash -

# repository for node.js 18
curl -fsSL https://deb.nodesource.com/setup_18.x | bash -
Adding Nodesource repository for Debian
Adding Nodesource repository for Debian

2. After completing, execute the following command to install Node.js to your Debian system, then input Y to confirm.

sudo apt install nodejs

3. Now, execute the following command to check Node.js and NPM versions on your system. You should see an output of your selected Node.js version.

node --version

In this example, I’ve installed Node.js 21.7 on my Debian machine.

Checking Node.js version
Checking Node.js version

Installing Node.js For Developers

If you’re a developer, it’s recommended to use tools like NVM (Node Version Manager) or Volta for managing your Node.js installation. As you may also working on multiple projects with different Node.js versions, those tools help you switch different Node.js versions easily.

Installing Node.js with NVM (Node Version Manager)

To install and manage Node.js via NVM (Node Version Manager), execute these actions:

1. To start, execute the command below to download and run the NVM installer script.

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash

The NVM should be installed on the ~/.nvm directory and additional configuration added to the ~/.bashrc file.

Installing NVM (Node Version Manager)
Installing NVM (Node Version Manager)

2. Run the command below to reload the ~/,bashrc file and complete the NVM installation.

source ~/.bashrc

3. Once reloaded, execute the command utility below to ensure the nvm is installed.

command -v nvm

If the nvm is installed, the return/output should be nvm. You can now install Node.js via NVM.

Apply and check NVM (Node version Manager)
Apply and check NVM (Node version Manager)

4. Now, execute the nvm install command below to install Node.js. Make sure to adjust the target Node.js version as you need.

# installing latest node.js version
nvm install node

# installing node.js lts codename iron
nvm install lts/iron

# installing node.js v20.11.0
nvm install v20.11.0
Installing Node.js using NVM (Node Version Manager)
Installing Node.js using NVM (Node Version Manager)

5. After installation is complete, run the command below to check the installed Node.js version on your system.

nvm ls

In this case, I’ve installed Node.js 20.11 via NVM (Node Version Manager).

Checking installed Node.js versions
Checking installed Node.js versions

6. Now, run the nvm use command below to set your default Node.js version. This is useful when you have multiple Node.js versions installed, and also when you need to switch between Node.js versions for different projects.

nvm use 20.11

7. Lastly, verify your Node.js version with the command below. You should get the default Node.js version that you’ve configured.

node --version
Checking Node.js version
Checking Node.js version

Installing Node.js with Volta toolchain

To install Volta as your Node.js toolchain, follow these steps:

1. Execute the command below to download and run the Volta setup script. Similar to NVM, the setup script will create a new installation directory ~/.volta, and add a new configuration to ~/.bashrc.

Once the installation is finished, you should get the message success: Setup complete.

curl https://get.volta.sh | bash

2. Now, reload the ~/.bashrc script to apply the changes to the current session (without logging out).

source ~/.bashrc

3. After that, run the volta command below to check the Volta version. At this time, you should get Volta 1.1.1 installed on Debian.

volta --version
Installing Volta Node.js toolchain
Installing Volta Node.js toolchain

4. Next, run the volta install command below to install your preferred Node.js version. Or, you can also install multiple Node.js versions as you need.

# install latest node.js version
volta install node

# install latest node.js LTS version
volta install node@lts

# install node.js v21
volta install node@21

5. Once your Node.js is installed, run the command below to check available Node.js and toolchains installed on your system.

volta list all

In the Node runtimes section, you should get the list of installed Node.js versions.

Installing Node.js with volta
Installing Node.js with volta

6. Now, run the volta pin command below to set up the default Node.js version for your project. This will update and set the default Node.js version via the package.json file in your project directory.

Note: With the volta pin command, you can easily set up multiple Node.js projects with different Node.js versions for each project.

cd ~/project
volta pin node@lts
cat package.json

7. Additionally, you can also install Node.js package managers, Yarn, or NPM using the volta install command below.

volta install yarn
volta install npm
Installing Yarn and NPM package manager with volta
Installing Yarn and NPM package manager with volta

Optional: Installing Yarn package manager via Corepack

The new Yarn package manager is included in the Corepack, which is shipped by default on Node.js. This is because the new Yarn isn’t installed system-wide, nor via npm.

If you want to install the Yarn package manager as a standalone, follow these actions:

1. Run the command below to enable Corepack on your Node.js installation. Once executed, the yarn binary file will be added to your current PATH environment variable.

corepack enable

2. Once enabled, run the command below to initialize your project.

yarn init
Enable Yarn package manager through Node.js corepack
Enable Yarn package manager through Node.js corepack

Conclusion

In this guide, you have installed Node.js and Node.js package manager (Yarn and NPM) on Debian 12. You’ve installed Node.js through the official Debian repository, using a third-party Nodesource repository, using NVM or Node Version Manager, and also using Volta. From here, you can now deploy and develop your Node.js-based applications.

System administrator and devops enthusiast, leveraging over 10+ years of Linux expertise to optimize operations. Proficient in FreeBSD, VMWare, KVM, Proxmox, PfSense, Ansible, Docker, and Kubernetes.

Read Also: