
It's finally here:
>> The Road to Membership and Baeldung Pro.
Going into ads, no-ads reading, and bit about how Baeldung works if you're curious :)
Last updated: May 8, 2025
Node Version Manager (NVM) is a tool for managing multiple Node.js versions on a machine.
In this tutorial, we’ll learn how to install NVM in a Docker container. We’ll discuss three methods:
We can choose from a few Docker images for NVM that are available on Docker Hub. Let’s pull the guolin/node-nvm-docker image:
$ docker pull guolin/node-nvm-docker
And now, we can see it in our list of images:
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
guolin/node-nvm-docker latest 6dc6207658b5 8 years ago 703MB
Having downloaded the image, let’s run a container based on it in interactive mode:
$ docker run
--name node-nvm
-it
guolin/node-nvm-docker
bash
This starts a Bash shell in the container:
root@0787786b89f9:/#
The NVM version manager is pre-installed in the container; therefore, we can directly confirm its version:
root@0787786b89f9:/# nvm --version
0.30.1
Furthermore, Node.js is also pre-installed in the container, so let’s verify that version, too:
root@0787786b89f9:/# nvm current
v4.2.1
We can install NVM in an existing or new Docker container by running a few commands.
Let’s launch an interactive Ubuntu container to install NVM:
$ docker run
-it
--name ubuntu-demo
ubuntu
/bin/bash -c "echo 'Hello World'; /bin/bash"
This launches an interactive Bash shell after outputting a “Hello World” message:
root@8cc33fbb81e0:/#
First, we need to download the curl command-line tool:
root@8cc33fbb81e0:/# apt update && apt install curl -y
Afterward, we can download and install NVM using curl:
root@8cc33fbb81e0:/# curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.2/install.sh | bash
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 16631 100 16631 0 0 406k 0 --:--:-- --:--:-- --:--:-- 406k
=> Downloading nvm as script to '/root/.nvm'
=> Appending nvm source string to /root/.bashrc
=> Appending bash_completion source string to /root/.bashrc
We could close and reopen the terminal to start using NVM. Alternatively, let’s run an export command to load NVM and start using it:
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
With NVM loaded, we can find its usage with the nvm command:
root@8cc33fbb81e0:/# nvm
Node Version Manager (v0.40.2)
Usage:
nvm --help Show this message
nvm --version Print out the installed version of nvm
Let’s use it to install and use the latest Node version:
root@8cc33fbb81e0:/# nvm install node
Downloading and installing node v23.11.0...
Downloading https://nodejs.org/dist/v23.11.0/node-v23.11.0-linux-x64.tar.gz...
######################################################################### 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v23.11.0 (npm v10.9.2)
And then, we can confirm the Node version reported by the installation:
root@8cc33fbb81e0:/# nvm current
v23.11.0
Interestingly, we can run this command even if Node is not yet installed.
Perhaps the most suitable option when we want to automate the installation process is to use a Dockerfile. We’ll do this in three steps:
We can place much of what we did manually earlier into a Dockerfile:
FROM ubuntu:latest
ARG NODE_VERSION=20
# install curl
RUN apt update && apt install curl -y
# install nvm
RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.2/install.sh | bash
# set env
ENV NVM_DIR=/root/.nvm
# install node
RUN bash -c "source $NVM_DIR/nvm.sh && nvm install $NODE_VERSION"
# set ENTRYPOINT for reloading nvm-environment
ENTRYPOINT ["bash", "-c", "source $NVM_DIR/nvm.sh && exec \"$@\"", "--"]
# set cmd to bash
CMD ["/bin/bash"]
We can see that we’re installing cURL, NVM, and Node just as before onto an Ubuntu base.
Now, it’s time to build a Docker image from our Dockerfile. To do so, we need to be in the directory containing the Dockerfile.
Let’s run a docker build command to create a Docker image called nvm:latest:
$ docker build
-t nvm:latest .
docker images shows us we successfully created it:
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nvm latest 7383b5cdf243 6 minutes ago 345MB
ubuntu latest 602eb6fb314b 2 days ago 78.1MB
Now, we can build a container that’s much like the one we built earlier from the Docker Hub image that had Node.js and NVM already installed.
Let’s run an example container from the image we built:
$ docker run --rm -it nvm
root@c012e7d835d9:/#
And now, we can again verify the NVM, Node, and NPM versions installed in the Docker container:
root@c012e7d835d9:/# nvm -v
0.40.2
root@c012e7d835d9:/# nvm current
v20.19.0
root@c012e7d835d9:/# npm -v
10.8.2
However, we aren’t limited to the version configured in the Dockerfile. To demonstrate, let’s install a new version of Node in the same container:
root@c012e7d835d9:/# nvm install 23.11.0
Downloading and installing node v23.11.0...
Downloading https://nodejs.org/dist/v23.11.0/node-v23.11.0-linux-x64.tar.gz...
######################################################################### 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v23.11.0 (npm v10.9.2)
We can list all the installed versions with the nvm ls command:
root@c012e7d835d9:/# root@c012e7d835d9:/# nvm ls
v20.19.0
-> v23.11.0
default -> 20 (-> v20.19.0)
The -> indicates the active Node version.
In this article, we learned about the different methods to install NVM within a Docker container. We can install NVM in an existing container or a new container. Or, when we want to automate the installation process, we can use a Dockerfile that has all the installation instructions configured to build our own image and container.
As always, the Docker commands used in this article are available over on GitHub.