0 Comments

Introduction Node.js

Node.js has become an indispensable component in modern web development, powering everything from simple command-line tools to large-scale enterprise applications. At its core, Node.js is a cross-platform, open-source JavaScript runtime environment that executes JavaScript code outside of a web browser, utilising Google’s V8 engine and an event-driven, non-blocking I/O model to deliver high performance and efficiency (Wikipedia). Whether you are building real-time chat applications, RESTful APIs, or data-intensive microservices, Node.js provides the versatility and scalability required by today’s development teams.

For many developers and organisations, deploying Node.js in a production environment involves using a Virtual Private Server (VPS). A VPS offers a dedicated portion of a physical server, granting you root access, guaranteed resources (CPU, RAM, disk), and full control over server configuration and software stack. Unlike shared hosting, where resources are shared among multiple users, a VPS isolates your environment, ensuring consistent performance, enhanced security, and the freedom to install and configure software as you see fit. This article will explain, in clear and detailed terms, how to install Node.js on a Linux VPS, guiding users without prior Linux or server-management experience through each step of the process.

Virtual Private Server

A Virtual Private Server (VPS) is a virtualised environment created on a physical host machine using hypervisor technology. From the perspective of the user, a VPS behaves like a dedicated server: you have your own operating system instance, full root (administrative) access, customisable resources, and the ability to install any software compatible with the OS. However, unlike dedicated hardware, a VPS shares the underlying physical resources—CPU cores, memory, storage, and network bandwidth—with other VPS instances on the same host. The hypervisor ensures fair allocation and strong isolation, so that your VPS performance and security remain unaffected by other tenants.

Key advantages of using a VPS include:

  • Cost-effectiveness: VPS plans are significantly cheaper than dedicated servers, making them ideal for small to medium projects or startups.
  • Scalability: You can often upgrade CPU, RAM, or storage with a few clicks, enabling your infrastructure to grow alongside your application.
  • Control & customisation: You have root access, allowing you to configure software, firewalls, and security policies precisely to your requirements.
  • Reliability: Unlike shared hosting, a VPS’s performance is not impacted by other users on the same server, resulting in more predictable performance.

For Node.js applications, a VPS is particularly well-suited because it provides the freedom to configure process managers (such as PM2), web servers (like Nginx or Apache), and other services (databases, caches) to work optimally with your Node.js stack. Whether you are deploying a single-page application backend, a real-time chat server, or multiple microservices, a VPS offers the perfect balance between control, performance, and cost.

In contrast to simpler hosting solutions, a VPS requires some level of server management knowledge, including connecting via Secure Shell (SSH), using the Linux command line, and understanding basic file permissions. This article assumes no prior experience and will walk you through each step—from initial SSH connection to verifying your Node.js installation—using clear commands and explanations.

For users who prefer a streamlined, one-click approach, many VPS providers—such as Hostinger—offer pre-configured templates to install Node.js automatically on Ubuntu servers. This method is ideal for beginners or those needing a quick setup without manual intervention (Hostinger). The template typically includes Ubuntu 22.04, OpenLiteSpeed (a lightweight web server), and the latest stable Node.js runtime, allowing you to deploy JavaScript applications immediately.

Node.js

Steps for Automatic Installation

  1. Log in to the VPS control panel: Navigate to your provider’s dashboard (e.g., Hostinger’s hPanel) and select your VPS instance.
  2. Choose the Operating System template: Under System → Operating System, click Application or Template.
  3. Select the Node.js template: Pick Ubuntu 22.04 with Node.js and OpenLiteSpeed (or similar).
  4. Confirm and provision: Acknowledge any data-wiping warnings by checking the confirmation box and setting a new root password.
  5. Wait for deployment: The VPS will automatically reinstall the OS, web server, and Node.js. This may take a few minutes.
  6. Access via SSH: Once completed, connect to your VPS using an SSH client (e.g., PuTTY on Windows or the Terminal on macOS/Linux).

Benefits of this approach include:

  • Speed: Get a fully functional environment in minutes.
  • Safety: Remove the risk of mistyped commands harming your system.
  • Integrated stack: Includes web server, dependencies, and runtime out of the box.

However, automatic templates often install a single Node.js version and limit customisation. If you require a specific version, advanced configuration, or different web servers (Nginx, Apache), manual installation is recommended.

Manual installation grants you full control over which Node.js version to install and how to manage updates. We will cover three methods:

  • Installing from Ubuntu’s Default Repository
  • Installing via NodeSource PPA
  • Installing with Node Version Manager (NVM)

Each method is suitable for different use-cases and experience levels.

Ubuntu’s default repositories include Node.js and NPM packages, making installation straightforward. Note: These packages may lag behind the latest Node.js releases (Hostinger).

Commands

# Update package lists
sudo apt update

# Install Node.js
sudo apt install -y nodejs

# Install NPM (Node Package Manager)
sudo apt install -y npm

# Verify installation
node -v && npm -v

After running these commands:

  • node -v displays the installed Node.js version.
  • npm -v confirms the NPM version for package management.

This method is best for applications that do not demand the absolute latest Node.js release but prioritise stability and simplicity.

To install a more recent version, use the NodeSource PPA, which maintains up-to-date Node.js packages (Hostinger, IONOS).

Step

  1. Install cURL (if not already installed): sudo apt install -y curl
  2. Add the NodeSource setup script (replace 22.x with desired major version): curl -fsSL https://deb.nodesource.com/setup_22.x | sudo -E bash -
  3. Install Node.js and NPM: sudo apt install -y nodejs
  4. Verify: node -v && npm -v

This approach ensures you can select from multiple Node.js major versions, benefiting from recent performance improvements and security patches.

For developers who need to switch between Node.js versions frequently—such as testing different projects—NVM is the ideal tool. NVM is a bash script that simplifies installing and managing multiple Node.js versions (Hostinger, IONOS).

Installation

# Download and install NVM (use latest release tag)
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash

# Load NVM script into current shell
source ~/.profile

# Verify NVM installation
nvm --version

Using NVM:

  1. List available Node.js versions: nvm ls-remote
  2. Install a specific version (e.g., 22.1.0): nvm install 22.1.0
  3. Switch between versions: nvm use 22.1.0
  4. Set a default version: nvm alias default 22.1.0
  5. List installed versions: nvm ls
  6. Uninstall an unused version: nvm uninstall 22.1.0

NVM ensures that your global environment remains clean, with no interference between projects requiring different Node.js releases.

Installing Node.js on a Linux VPS can be achieved via multiple methods, each catering to varying requirements and expertise levels. For rapid deployment, automatic templates from VPS providers like Hostinger offer a hassle-free, one-click solution, albeit with limited customisation (Hostinger). For those seeking more control, manual installation via Ubuntu’s default repositories provides simplicity, while the NodeSource PPA ensures access to the latest stable Node.js releases (IONOS). Developers requiring frequent version changes will find NVM indispensable for sandboxing multiple Node.js environments without polluting the system’s global space (Hostinger).

Beyond installation, remember to follow best practices:

  • Secure your VPS: Regularly update the OS (sudo apt update && sudo apt upgrade), configure a firewall (e.g., UFW), and disable root login over SSH.
  • Manage Node.js processes: Use a process manager like PM2 to daemonise applications, enable auto-restart on crashes, and simplify log management.
  • Automate deployments: Integrate CI/CD tools (e.g., GitHub Actions) to build, test, and deploy your Node.js applications seamlessly.
  • Monitor performance: Implement logging (Winston, Bunyan) and monitoring solutions (New Relic, Prometheus) to observe resource usage and preempt issues.

With your Node.js environment live on a Linux VPS, you are well-equipped to develop, deploy, and scale modern web applications. Should you encounter any hurdles, consult the official Node.js documentation, community forums, or detailed guides provided by your VPS host. Happy coding!

Jorge Ruiz

Jorge Ruiz Centelles

Filólogo y amante de la antropología social africana

Related Posts