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 to refresh the available packages from your configured repositories.sudo apt install -y nodejs npm to install both the runtime and package manager. The nodejs package provides the runtime whilst npm adds the package manager.node -v and npm -v. You should see version numbers printed for each command.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 followed by sudo apt-get install -y ca-certificates curl gnupg to install the tools needed for adding the repository.sudo mkdir -p /etc/apt/keyrings, then download and save the signing key using curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | sudo gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg. This verifies package authenticity during installation.NODE_MAJOR=24, replacing 24 with your chosen major version number. Then run 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 to configure the repository. Check the Node.js release schedule to identify the current LTS version before running this command.sudo apt-get update, then install Node.js using sudo apt-get install -y nodejs. This single package includes the Node.js runtime and npm.node -v and npm -v to confirm both tools installed correctly.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 to install NVM. Check the NVM releases page for the latest version number and update the URL accordingly. The script modifies your shell profile to load NVM automatically.source ~/.bashrc to activate NVM in your current session without logging out.nvm install node to fetch and install the most recent version. The keyword node tells NVM to fetch the latest release. To install a specific version, specify the major version number instead: nvm install 20.node -v and npm -v to confirm both tools installed correctly.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