Node.js executes JavaScript code outside the browser, turning it into a language for building servers, command-line utilities and backend services. Installing Node.js on your Linux server gives you the node runtime for executing code and npm for managing third-party packages.
You will install Node.js using one of three approaches depending on your requirements. The default package manager installs a stable version quickly. NodeSource repositories provide current releases with security updates. NVM lets you run multiple Node.js versions side by side for testing across different releases.
sudo apt update before installing new packages.Your Linux distribution includes Node.js in its standard package repositories. This method installs a tested, stable version that may be several releases behind the current upstream version. Use this approach when you need Node.js running quickly and do not require the latest features.
sudo apt update
sudo apt install -y nodejs npm
node -v
npm -v
Your Node.js installation now works for running applications and installing dependencies. This method suits production environments where stability matters more than having the absolute latest version.
NodeSource maintains official Node.js packages for Debian and Ubuntu that track upstream releases closely. This method gives you access to recent Node.js versions including current and Long Term Support (LTS) lines. Use NodeSource when your application requires features or security patches from newer releases.
sudo apt-get update
sudo apt-get install -y ca-certificates curl gnupg
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | sudo gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg
NODE_MAJOR=24
echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" | sudo tee /etc/apt/sources.list.d/nodesource.list
sudo apt-get update
sudo apt-get install -y nodejs
node -v
npm -v
NodeSource packages receive regular updates that track official Node.js releases. Your server will receive security patches and minor version updates through the standard apt upgrade process. This method balances currency with the convenience of system package management.
Node Version Manager (NVM) installs Node.js versions into your home directory rather than system-wide locations. This lets you switch between releases without affecting other users or requiring root access after the initial setup. NVM works well on development servers where you need to test code against multiple Node.js versions.
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.4/install.sh | bash
source ~/.bashrc
nvm install node
nvm install 20
node -v
npm -v
NVM maintains separate npm installations for each Node.js version, preventing dependency conflicts between projects. Switch between installed versions with nvm use 18 or set a default version using nvm alias default 20. Each version remains isolated in your home directory at ~/.nvm/versions/node/.
Your Linux server now runs Node.js and npm for building JavaScript applications outside the browser. You installed using the default package manager for stability, NodeSource repositories for current releases or NVM for version flexibility. The node command executes JavaScript files whilst npm handles package installation.
Test your installation by creating a file called hello.js containing console.log('Node.js installed'); and running node hello.js. You should see your message printed to the terminal.
Get scalable resources with our VPS hosting with root access and optional software.
Get VPS HostingPerfect for websites and small businesses unlimited bandwidth with cPanel hosting.
Get cPanel Hosting