BLOG POSTS
    MangoHost Blog / Install and Uninstall Node.js on Ubuntu – Step-by-Step
Install and Uninstall Node.js on Ubuntu – Step-by-Step

Install and Uninstall Node.js on Ubuntu – Step-by-Step

Installing and uninstalling Node.js on Ubuntu is a fundamental skill every developer working with JavaScript server-side applications needs to master. Whether you’re setting up a development environment, deploying applications, or managing multiple Node.js versions across different projects, understanding the various installation methods will save you countless headaches down the road. This guide walks through multiple installation approaches, from the straightforward APT package manager to version managers like NVM, plus covers clean uninstallation procedures and troubleshooting common issues you’ll inevitably encounter.

How Node.js Installation Methods Work on Ubuntu

Ubuntu offers several ways to install Node.js, each with distinct advantages and trade-offs. The APT package manager provides the most straightforward installation but often ships with older versions. NodeSource repositories give you access to current releases while maintaining the simplicity of APT. The Node Version Manager (NVM) offers maximum flexibility for managing multiple Node.js versions, which is essential for projects with different version requirements.

The snap package system provides another installation avenue, though it comes with sandboxing that can sometimes interfere with development workflows. Building from source gives you complete control but requires more system resources and time. Understanding these methods helps you choose the right approach for your specific use case.

Method 1: Installing Node.js via APT Package Manager

The default Ubuntu repositories include Node.js, making this the quickest installation method. However, the version is typically several releases behind the current stable release.

sudo apt update
sudo apt install nodejs npm

Verify the installation:

node --version
npm --version

The APT method installs both Node.js and npm (Node Package Manager) simultaneously. While convenient for getting started quickly, this approach often leaves you with Node.js 12.x or 14.x when the current LTS might be 18.x or 20.x.

Method 2: Installing Current Node.js via NodeSource Repository

NodeSource maintains official Ubuntu repositories with current Node.js versions. This method combines APT’s simplicity with access to recent releases.

First, add the NodeSource repository for your desired Node.js version:

# For Node.js 20.x LTS
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -

# Or for a specific version like Node.js 18.x
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -

Then install Node.js:

sudo apt-get install -y nodejs

This method automatically includes npm and handles dependency management. The installation includes build tools needed for compiling native npm packages:

sudo apt-get install -y build-essential

Method 3: Using Node Version Manager (NVM)

NVM is the most flexible approach for developers who work with multiple projects requiring different Node.js versions. It allows switching between versions seamlessly without affecting system-wide installations.

Install NVM using the install script:

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

Reload your shell or restart your terminal, then verify NVM installation:

nvm --version

Install the latest LTS version:

nvm install --lts
nvm use --lts

Install specific versions:

nvm install 18.17.0
nvm install 20.5.1
nvm use 18.17.0

List installed versions:

nvm list

Set a default version:

nvm alias default 18.17.0

Method 4: Installing via Snap Package

Snap packages provide isolated installations with automatic updates. While convenient, the sandboxing can sometimes cause issues with development tools.

sudo snap install node --classic

The --classic flag removes snap confinement, allowing Node.js to access your filesystem normally.

Installation Method Comparison

Method Pros Cons Best For
APT Default Simple, stable, integrates with system updates Outdated versions, limited flexibility Production servers requiring stability
NodeSource Current versions, APT integration, automatic updates Single version per system, third-party repository Most development and production scenarios
NVM Multiple versions, easy switching, per-user installation Not system-wide, requires shell integration Development environments, multiple projects
Snap Automatic updates, isolated installation Potential sandboxing issues, larger disk usage Desktop development, less critical environments

Uninstalling Node.js: Complete Removal Guide

Uninstalling Node.js completely requires different approaches depending on your installation method and ensuring all related files are removed.

Uninstalling APT-installed Node.js

Remove the packages:

sudo apt remove nodejs npm
sudo apt purge nodejs npm
sudo apt autoremove

Uninstalling NodeSource Installation

Remove Node.js and the repository:

sudo apt remove nodejs npm
sudo rm -f /etc/apt/sources.list.d/nodesource.list
sudo apt update

Uninstalling NVM and All Node.js Versions

Remove all Node.js versions managed by NVM:

nvm deactivate
nvm uninstall --lts
nvm uninstall node

Remove NVM itself:

rm -rf ~/.nvm
# Remove NVM lines from ~/.bashrc, ~/.zshrc, or ~/.profile

Uninstalling Snap Installation

sudo snap remove node

Complete System Cleanup

After uninstalling through any method, clean up remaining files:

# Remove global npm packages directory
sudo rm -rf /usr/local/lib/node_modules

# Remove npm cache
rm -rf ~/.npm

# Remove user-specific npm configuration
rm -f ~/.npmrc

# Remove any remaining Node.js binaries
sudo rm -f /usr/local/bin/node
sudo rm -f /usr/local/bin/npm

Real-World Use Cases and Examples

Development Environment Setup

Many developers use NVM to manage different project requirements:

# Switch to project requiring Node.js 16
cd /path/to/legacy-project
nvm use 16.20.0
npm install

# Switch to modern project using Node.js 20
cd /path/to/new-project
nvm use 20.5.1
npm install

Production Server Deployment

Production environments typically benefit from NodeSource installations for their stability and integration with system package management:

# Install specific LTS version for production
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt-get install -y nodejs

# Verify versions match production requirements
node --version  # Should output v18.x.x
npm --version

CI/CD Pipeline Integration

Automated deployment scripts often use APT or NodeSource for consistent installations:

#!/bin/bash
# deployment script
sudo apt update
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
sudo apt-get install -y nodejs
npm ci --only=production

Common Issues and Troubleshooting

Permission Issues with Global npm Packages

The most frequent issue is permission errors when installing global packages. Configure npm to use a different directory:

mkdir ~/.npm-global
npm config set prefix '~/.npm-global'
echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.profile
source ~/.profile

Multiple Node.js Versions Conflict

When multiple installation methods exist, conflicts arise. Check which Node.js is being used:

which node
which npm
node --version

Clean up conflicting installations by removing unwanted versions and updating your PATH.

NVM Command Not Found

If NVM commands don’t work after installation, the shell configuration wasn’t updated properly:

# Add to ~/.bashrc, ~/.zshrc, or ~/.profile
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"

Build Tools Missing

Some npm packages require compilation and fail without build tools:

sudo apt-get install -y build-essential
# Or on newer Ubuntu versions
sudo apt-get install -y gcc g++ make

Best Practices and Security Considerations

  • Always verify installation integrity using official checksums when downloading scripts
  • Use LTS versions for production environments to ensure stability and security updates
  • Regularly update Node.js and npm to patch security vulnerabilities
  • Avoid running npm with sudo to prevent permission issues and security risks
  • Use package-lock.json files to ensure consistent dependency versions across environments
  • Configure npm audit to automatically check for vulnerable dependencies
  • Consider using Docker containers for isolated Node.js environments in production

Security Configuration

Configure npm for better security:

# Enable audit automatically
npm config set audit-level moderate

# Configure registry security
npm config set fund false
npm config set audit true

Performance Considerations and Benchmarks

Different installation methods have varying performance characteristics. NVM installations typically have slightly slower startup times due to shell integration overhead, while system-wide installations via APT or NodeSource provide the fastest execution.

Here’s a comparison of typical installation sizes:

Installation Method Disk Usage Memory Overhead Startup Time
APT Default ~45MB Lowest Fastest
NodeSource ~50MB Low Fast
NVM ~55MB per version Medium Slightly slower
Snap ~85MB Higher Slower

Advanced Configuration and Integration

For development teams, consider creating standardized setup scripts that handle Node.js installation and project configuration:

#!/bin/bash
# team-setup.sh
if command -v nvm &> /dev/null; then
    nvm install 18.17.1
    nvm use 18.17.1
else
    curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
    export NVM_DIR="$HOME/.nvm"
    [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
    nvm install 18.17.1
    nvm use 18.17.1
fi

npm install -g typescript @angular/cli create-react-app

Understanding these installation methods and their trade-offs helps you make informed decisions based on your specific requirements. Whether you’re setting up a single development machine or managing multiple production servers, having multiple approaches in your toolkit ensures you can handle any Node.js deployment scenario effectively.

For official documentation and additional resources, visit the Node.js Package Manager Installation Guide and the NVM GitHub repository.



This article incorporates information and material from various online sources. We acknowledge and appreciate the work of all original authors, publishers, and websites. While every effort has been made to appropriately credit the source material, any unintentional oversight or omission does not constitute a copyright infringement. All trademarks, logos, and images mentioned are the property of their respective owners. If you believe that any content used in this article infringes upon your copyright, please contact us immediately for review and prompt action.

This article is intended for informational and educational purposes only and does not infringe on the rights of the copyright owners. If any copyrighted material has been used without proper credit or in violation of copyright laws, it is unintentional and we will rectify it promptly upon notification. Please note that the republishing, redistribution, or reproduction of part or all of the contents in any form is prohibited without express written permission from the author and website owner. For permissions or further inquiries, please contact us.

Leave a reply

Your email address will not be published. Required fields are marked