Ken Muse

Using Node Version Manager (NVM)


The other day I was needing a quick and easy way to configure Node.js in an Ubuntu environment. Unfortunately, in this particular case I could simply rely on a package manager to update my dependencies. I needed some extra assistance. Enter Node Version Manager (NVM).

If you’re not familiar with it, NVM provides a quick and easy way to install and configure multiple versions of Node.js in a user environment. This works well, even in a container. It’s surprisingly simple and straight-forward as well! To set it up (install or upgrade), you can use the documented process:

1curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.2/install.sh | bash
2export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
3[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"

After that, you just need to install the node version you want. You can use node for the latest version, or provide a specific version:

1nvm install v18
2nvm install v16.12

You can activate a version with the use command:

1nvm use v18

Or execute a command using an installed version with run:

1nvm run v16.12 --version

It’s a handy tool for when you need to quickly setup and run Node.js (or change versions of the runtime on the fly)!