Installing Node.js and npm on Linux: Best Practices Using NVM

Dec 04, 2025 · Programming · 11 views · 7.8

Keywords: Node.js | npm | NVM | Linux Installation | Version Management

Abstract: This article explores common issues in installing Node.js and npm on Linux servers, particularly challenges with outdated versions and missing npm. By analyzing Q&A data, it highlights the advantages of using Node Version Manager (NVM) as a solution, including flexibility in version management, isolation of system environments, and simplicity in installation. The article details NVM installation steps, basic commands, and permission configurations, aiming to provide developers with a reliable and efficient guide for setting up Node.js environments.

Introduction

When deploying Node.js and npm on Linux servers, developers often encounter issues such as outdated versions or incorrect npm installations. Traditional package managers like apt-get may provide obsolete versions, leading to compatibility challenges. Based on Q&A data, this article analyzes these problems and recommends Node Version Manager (NVM) as the optimal solution.

Problem Analysis

In the provided Q&A data, the user attempted to install Node.js and npm via sudo apt-get install nodejs npm, but found outdated versions (e.g., 0.6.19) and npm not properly installed. Subsequently, the user tried NodeSource scripts (e.g., curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash -), yet faced version inconsistencies and missing npm. These methods rely on system package managers, which may be limited by repository update delays or configuration errors.

NVM Solution

NVM is a command-line tool that allows users to easily install and manage multiple Node.js versions. Its advantages include:

Steps to install NVM:
  1. Run the command: curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash. This downloads and executes the installation script.
  2. Verify installation: Close and reopen the terminal, then type nvm. If a "command not found" error appears, you may need to create a ~/.bash_profile file: touch ~/.bash_profile, and rerun the installation script.
Installing Node.js versions: Permission configuration: To run Node.js with sudo privileges (e.g., for accessing ports below 1024), execute the following commands:
n=$(which node)
n=${n%/bin/node}
chmod -R 755 $n/bin/* 
sudo cp -r $n/{bin,lib,share} /usr/local
This ensures proper permissions for binaries and system-level access.

Practical Recommendations

When using NVM, it is recommended to:

Compared to other methods, NVM avoids the limitations of relying on system package managers, offering a more controlled environment.

Conclusion

Installing Node.js and npm via NVM is an effective solution for version issues and missing npm on Linux servers. It provides flexibility, isolation, and ease of use, surpassing traditional installation methods. Developers should adopt this practice to build stable and maintainable Node.js development environments.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.