This guide walks you through installing Node.js using NVM (Node Version Manager) on a Linux VPS. You’ll learn how to install NVM, use it to install the latest Node.js LTS release, and switch between versions as needed. This approach lets you manage multiple Node.js versions without sudo, making it ideal for development and production servers.
What You’ll Need
- A VPS running a modern Linux distribution (Ubuntu, Debian, CentOS, etc.) with SSH access.
- A user account with sudo privileges (you’ll install NVM as a regular user, not root).
- Basic familiarity with the command line.
Step-by-Step
- Update your system packages
SSH into your VPS and update the package list to ensure you have the latest security patches and dependencies.sudo apt update && sudo apt upgrade -y(for Debian/Ubuntu) orsudo yum update -y(for CentOS/RHEL). - Install prerequisite packages
NVM needscurlandbuild-essential(or equivalent) to compile Node.js from source if needed. Install them with:sudo apt install curl build-essential -y(Debian/Ubuntu) orsudo yum install curl gcc-c++ make -y(CentOS/RHEL). - Download and run the NVM install script
Fetch the latest NVM install script from the official GitHub repository and run it. As of this writing, the latest version is v0.39.7, but you can check the official repo for the latest release.curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
This will clone the NVM repository to~/.nvmand add the necessary lines to your shell profile (~/.bashrc,~/.zshrc, or~/.profile). - Load NVM into your current session
After installation, you need to either log out and back in, or source your profile file manually. For bash, run:source ~/.bashrc
If you’re using zsh, usesource ~/.zshrcinstead. - Verify NVM installation
Check that NVM is available by running:nvm --version
You should see the version number (e.g., 0.39.7). If you get a “command not found” error, see the Troubleshooting section below. - Install the latest LTS version of Node.js
To install the current Long-Term Support (LTS) release, run:nvm install --lts
This will download and install the latest LTS version (e.g., v20.x). If you prefer a specific version, usenvm install 18ornvm install 20.10.0. - Set the default Node.js version
After installation, NVM automatically uses the newly installed version. To make it the default for all new shells, run:nvm alias default 'lts/*'
This ensures that every new terminal session uses the latest LTS version. - Verify Node.js and npm
Check that Node.js and npm are correctly installed:node --version(should output e.g., v20.11.0)npm --version(should output e.g., 10.2.4) - Test with a simple script
Create a quick test file to confirm everything works:echo "console.log('Node.js is running');" > test.js && node test.js
You should see “Node.js is running” in the output. - Install a different version (optional)
If you need another Node.js version for a project, install it and switch between versions with:nvm install 16nvm use 16
To see all installed versions, usenvm ls.
Common Mistakes
- Running NVM install as root – NVM is designed to be installed per-user. If you run the install script as root, it may work but can cause permission issues later. Always use a regular user account.
- Not sourcing the shell profile – After installation, NVM won’t be available in the current session until you run
source ~/.bashrcor log out and back in. Many users miss this and think the installation failed. - Using outdated install script URL – The install script URL includes a version number. If you copy an old URL, you may get a 404 error. Always grab the latest version from the official repo.
- Forgetting to set a default alias – If you don’t set a default alias, every new shell will use the system Node.js (if any) or nothing. Set the default to avoid confusion.
- Installing Node.js globally with apt – If you already have Node.js installed via apt, it may conflict with NVM. Uninstall it first:
sudo apt remove nodejs npm.
Tips
- Use LTS for production – For production servers, stick with the LTS version. It receives security updates and is more stable than the current release.
- Keep NVM updated – Periodically update NVM itself with
nvm install --lts(it updates NVM as well) or by re-running the install script. - Use .nvmrc files – In each project, create a
.nvmrcfile containing the Node.js version (e.g.,18). Then runnvm usein the project directory to automatically switch to the correct version. - Install global packages without sudo – Since NVM installs Node.js in your home directory, you can install global npm packages without sudo, avoiding permission headaches.
- Check for updates – To see if a newer Node.js version is available, run
nvm ls-remoteto list all remote versions.
Troubleshooting
- “nvm: command not found” – This usually means NVM wasn’t loaded. First, try
source ~/.bashrc. If that fails, check if the NVM lines were added to your profile by runningcat ~/.bashrc | grep nvm. If not, manually add the following lines to your profile:export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" - Install script fails with “curl: (35) SSL connect error” – This is often due to an outdated SSL library. Update your system packages and try again. If it persists, use
wget -qO-instead ofcurl. - Node.js version not switching – If
nvm use 18doesn’t change the version, ensure the version is installed (check withnvm ls). Also, verify that NVM is being loaded in your shell by runningwhich node– it should point to~/.nvm/versions/node/.... - Permission errors when installing global packages – If you get EACCES errors, you might have an old Node.js installed via apt. Remove it and reinstall via NVM. Also, never use sudo with npm when using NVM.
- npm not found after installing Node – npm is bundled with Node.js, so if npm is missing, the installation may have been corrupted. Reinstall Node.js with
nvm uninstall <version>and thennvm install <version>.
Frequently Asked Questions
What is the difference between NVM and apt-installed Node.js?
NVM installs Node.js in your user directory, allowing you to switch between multiple versions without affecting the system. The apt version is system-wide and typically older, and requires sudo for global package installs.
Can I use NVM on a root user?
Technically yes, but it’s not recommended. NVM is designed for per-user installations. Using it as root can lead to permission issues and is a security risk. Create a regular user for development tasks.
How do I uninstall NVM?
Remove the ~/.nvm directory and delete the lines NVM added to your shell profile. Then remove any Node.js versions installed by NVM using nvm uninstall <version> before deleting the directory.
Do I need to reinstall global npm packages when switching Node versions?
Yes, global packages are installed per Node.js version. When you switch to a new version, you’ll need to reinstall any global packages you need. You can migrate them with nvm reinstall-packages <old_version>.
Can I use NVM with a Node.js version installed via apt?
Yes, but NVM will take precedence in your shell. If you want to use the apt version, you can run nvm use system. However, it’s cleaner to remove the apt version entirely.
How do I update Node.js to the latest LTS?
Run nvm install --lts to install the latest LTS, then nvm alias default 'lts/*' to make it the default. If you want to keep your old version, it will remain installed.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- TCL Mini LED TV: Best Picture Settings Guide
- Best Mac Games: Top Picks & Buying Guide
- Google Translate Live Conversation Privacy & Saved Transcripts
- Stripe Webhooks: Null Customer Email Fix
- Extend Boot Partition: Safe Steps & Tools
- Can’t Hear Text Messages Through Car Stereo? Fix It