Set up Node.js LTS on Debian 12 Bookworm | Comprehensive Guide
This tutorial will show you how to Set up Node.js LTS on Debian 12 Bookworm. Node.js is a free and open-source environment for building fast and scalable server-side and networking applications. You can follow this guide on the Orcacore website to install Node.js using APT, PPA, and NVM on Debian 12.
To Set up Node.js LTS on Debian 12 Bookworm, you must have access to your server as a non-root user with sudo privileges. For this purpose, you can visit the Initial Server Setup with Debian 12 Bookworm.
Step 1 – Install Default Node.js with APT on Debian 12
Debian 12 ships with Node.js 18 LTS. You can easily install it on your server by using the APT package manager. First, run the system update with the command below:
sudo apt update
Then, use the command below to Set up Node.js LTS on Debian 12 Bookworm with APT:
sudo apt install nodejs
Verify your Node.js installation by checking its version:
node -v
**Output**
v18.13.0
Install NPM on Debian 12
Also, you need to install the npm (Node.js Package Manager) with the following command:
sudo apt install npm
Check its version:
npm -v
**Output**
9.2.0
Step 2 – Install Node.js LTS from PPA Repository on Debian 12
To install a different version of Node.js, you can use a PPA (personal package archive) maintained by NodeSource. These PPAs have more versions of Node.js available than the official Debian repositories.
To download the installation script for your preferred version, you can use the following curl command:
Here we download the Node.js 20 the upcoming LTS version.
# cd ~
# sudo curl -sL https://deb.nodesource.com/setup_20.x -o nodesource_setup.sh
When your download is completed, run your installation script:
sudo bash nodesource_setup.sh
The PPA will be added to your configuration and your local package cache will be updated automatically. You can now Set up Node.js LTS on Debian 12 Bookworm with the following command:
sudo apt install nodejs
Verify your Node.js installation on Debian 12 with the command below:
node -v
**Output**
v20.5.0
Note: With this method, your NPM will be installed during the Node.js installation.
You can verify your NPM by checking its version:
**Output**
9.8.0
Step 3 – Install Node.js LTS with NVM on Debian 12
The Node Version Manager (NVM) is another way that you can use to install and maintain many different independent versions of Node.js, and their associated Node packages, at the same time.
First, download and install the latest NVM installer script from GitHub with the command below:
sudo wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.4/install.sh | sudo bash
Next, source your .bashrc file with the command below:
source ~/.bashrc
To check which versions of Node are available, run the following command:
nvm list-remote
**Output**
v18.17.0 (Latest LTS: Hydrogen)
v19.0.0
v19.0.1
v19.1.0
...
v19.8.1
v19.9.0
v20.0.0
...
v20.4.0
v20.5.0
...
For example, to get v18.17.0 LTS, you can use the command below on Debian 12:
nvm install v18.17.0
Now you can see the different versions you have installed with the following command:
nvm list
Note: If you also have a version of Node.js installed through apt
, you may see an system
entry here. You can always activate the system-installed version of Node using nvm use system
.
Conclusion
At this point, you have learned to Set up Node.js LTS on Debian 12 Bookworm. You can easily install the default Nod.js and the LTS versions by using APT, PPA, and NVM on your server.
Hope you enjoy using it. You may like these articles too:
Steps To Install Gradle Build Automation Tool on Debian 12
Install Nextcloud with LAMP stack on Debian 12 Bookworm
Alternative Solutions for Installing Node.js LTS on Debian 12 Bookworm
While the article presents three excellent methods for installing Node.js LTS on Debian 12, there are other viable options. Here are two alternative solutions, including explanations and code examples:
1. Using Docker
Docker provides a containerization platform that allows you to run applications in isolated environments. This approach is particularly useful if you want to avoid conflicts with existing system packages or need a specific Node.js version for a particular project.
Explanation:
Docker containers encapsulate all the necessary dependencies for an application, including Node.js, its libraries, and the operating system environment. By using a Docker image that contains a pre-configured Node.js LTS environment, you can easily deploy and run Node.js applications on Debian 12 without directly installing Node.js on the host system. This ensures consistency across different environments (development, testing, production).
Steps:
-
Install Docker: If you don’t have Docker installed on your Debian 12 system, follow the official Docker documentation to install it. This usually involves adding the Docker repository to your system and installing the
docker-ce
package. -
Pull a Node.js LTS Docker Image: Docker Hub provides a variety of official and community-maintained Node.js images. Choose an image that corresponds to the LTS version you desire. For example, to pull the official Node.js 18 LTS image, use the following command:
docker pull node:18-slim
The
-slim
variant is usually smaller and contains only the essential components. -
Create a Dockerfile (Optional but Recommended): A Dockerfile allows you to define a custom image for your application, including installing dependencies and setting up the working directory. Here’s an example Dockerfile:
FROM node:18-slim WORKDIR /app COPY package*.json ./ RUN npm install COPY . . CMD [ "node", "server.js" ]
This Dockerfile does the following:
- Uses the
node:18-slim
image as a base. - Sets the working directory to
/app
. - Copies the
package.json
andpackage-lock.json
files (if you have one) to the container. - Runs
npm install
to install dependencies. - Copies the rest of your application code.
- Specifies the command to run when the container starts (in this case,
node server.js
).
- Uses the
-
Build the Docker Image (if using a Dockerfile): Navigate to the directory containing your Dockerfile and run:
docker build -t my-node-app .
This command builds an image named
my-node-app
from the Dockerfile in the current directory. -
Run the Docker Container: To run the Node.js application within the Docker container, use the
docker run
command. If you built an image from a Dockerfile:docker run -p 3000:3000 my-node-app
If you’re running a simple Node.js script directly:
docker run -it --rm -v "$PWD:/app" -w /app node:18-slim node your-script.js
Explanation of the command options:
-p 3000:3000
: Maps port 3000 on the host to port 3000 in the container.-it
: Runs the container in interactive mode.--rm
: Removes the container after it exits.-v "$PWD:/app"
: Mounts the current directory on the host to the/app
directory in the container, allowing you to edit your code on the host and see the changes reflected in the container.-w /app
: Sets the working directory inside the container.
2. Using a Pre-built Binary Distribution
Another approach is to download a pre-built binary distribution of Node.js directly from the Node.js website. This method avoids package managers and PPAs altogether.
Explanation:
Node.js provides pre-compiled binaries for various platforms, including Linux. Downloading and extracting these binaries provides a self-contained Node.js environment. You’ll need to manually manage the installation location and ensure that the node
and npm
executables are accessible in your system’s PATH.
Steps:
-
Download the Binary: Visit the official Node.js downloads page (https://nodejs.org/en/download/) and select the Linux Binaries (x64) corresponding to the LTS version you desire. Use
wget
orcurl
to download the tarball:wget https://nodejs.org/dist/v18.17.0/node-v18.17.0-linux-x64.tar.gz # Replace with the actual LTS version URL
-
Extract the Archive: Extract the downloaded tarball to a location of your choice. A common location is
/opt
:sudo tar -xzf node-v18.17.0-linux-x64.tar.gz -C /opt
-
Create Symbolic Links: To make the
node
andnpm
executables accessible from the command line, create symbolic links to them in/usr/local/bin
:sudo ln -s /opt/node-v18.17.0-linux-x64/bin/node /usr/local/bin/node sudo ln -s /opt/node-v18.17.0-linux-x64/bin/npm /usr/local/bin/npm
(Replace
/opt/node-v18.17.0-linux-x64
with the actual extraction path.) -
Verify Installation: Check the Node.js and npm versions to confirm the installation:
node -v npm -v
These alternative methods offer flexibility in how you Set up Node.js LTS on Debian 12 Bookworm, depending on your specific needs and preferences. Docker provides isolation and consistency, while pre-built binaries offer a straightforward installation process without relying on package managers.