NVM (Node Version Manager) installs and manages multiple Node.js versions per user. The apt nodejs package installs system-wide, is often outdated, and makes version switching difficult. NVM avoids both problems and is the standard for modern JS development.
Install NVM
The install script clones the NVM repo into ~/.nvm and adds initialization lines to ~/.bashrc or ~/.zshrc. Check the nvm-sh/nvm repo for the current release and replace v0.40.4 below if a newer version is out.
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.4/install.sh | bashActivate NVM
Either restart the terminal, or reload the shell config in the current session.
source ~/.bashrcVerify the install.
nvm --versionInstall Node.js
Install the current LTS, set it as the active version, and pin it as the default for new shells.
nvm install --lts
nvm use --lts
nvm alias default lts/*Confirm node and npm are available.
node -v
npm -vManage versions
nvm install 20 # install a specific major version
nvm use 20 # switch the current shell to that version
nvm alias default 20 # set the default for new shells
nvm ls # list installed versions
nvm uninstall 18 # remove a versionInspect before piping (optional)
Piping a remote script straight into bash should only be done for trusted sources. To inspect the script before running it, download first.
curl -O https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.4/install.sh
less install.sh
bash install.sh