In the world of Node.js development, it’s common to juggle multiple versions of Node across different projects. Using a single global Node installation can lead to version conflicts, dependency issues, or surprises in production. That’s where NVM (Node Version Manager) shines: it enables you to install, switch, and manage multiple Node.js versions side by side on the same machine.
In this guide, I’ll walk you through installing NVM on Ubuntu 24.04, show you how to use it effectively, and offer best practices to keep your setup stable. Let’s get started.
1. Switch Node.js versions seamlessly: Perfect for projects that require specific Node versions.
2. Isolated environments: Run multiple Node.js versions without interference between them.
3. User-friendly workflow: Install, remove, and manage Node.js versions with simple command-line operations.
It’s crucial to update your system first, to ensure dependencies and package sources are current:
sudo apt update
sudo apt upgrade -y
Also, consider rebooting if you haven’t done that in a while, especially if kernel or low-level packages were upgraded.
You’ll need tools to download files and compile modules when required:
sudo apt install -y curl build-essential libssl-dev
curl
→ For fetching remote scripts.build-essential
→ For compiling C/C++ add-ons (npm modules that need binaries).libssl-dev
→ Some Node versions/modules need SSL libraries.Here we fetch the official installer and run it:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/vX.Y.Z/install.sh | bash
vX.Y.Z
with the latest stable NVM version..nvm
directory in your home (~/.nvm), and updates your shell config (like ~/.bashrc
or ~/.zshrc
) to load NVM automatically.After installation, you need to enable it:
# Either in current session:
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
# Then, add these lines into ~/.bashrc or ~/.zshrc so they are loaded on login.
To apply changes right away:
source ~/.bashrc # or source ~/.zshrc
Check NVM is available:
nvm --version
If this returns something like 0.40.x
(or whatever current), you’re good.
With NVM ready, you’ll want to do things like:
nvm install node
nvm install --lts
nvm install 18.17.0
nvm use 18.17.0
nvm alias default 18.17.0
nvm ls
nvm ls-remote
These are the little practices that make your life easier:
.nvmrc
with a version (e.g. 18.17.0
). Then you and your team can use nvm use
inside that project to automatically pick the right version.-reinstall-packages-from=<old_version>
when installing a new version to copy over global packages.sudo apt remove nodejs
) to avoid conflicts.Here are issues you might encounter, plus how to fix them:
Problem | Symptoms | Fix |
---|---|---|
nvm: command not found after install | The shell doesn’t load NVM automatically | Ensure lines added to .bash / .zshrc by installer are present; then source the file or open a new terminal. |
Old Node still used by system | node -v returns old version even after nvm use | Remove or disable the system Node; ensure your PATH is set so NVM’s bin comes first. |
Permission errors when installing npm packages | Errors related to permissions when installing global packages | Don’t use sudo with npm when using NVM; global installs happen in user’s home directory. |
Setting up NVM on Ubuntu 24.04 gives you control, flexibility, and peace of mind when working with Node.js. Once it’s in place, switching versions becomes second nature, which is especially helpful when maintaining multiple projects, testing compatibility, or just staying up to date with Node.js.