Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Install node and npm with nvm using Docker.
#!/usr/bin/env bash
# confirm docker daemon is running and connected
docker version
# build the image based on the Dockerfile and name it `nvm`
docker build -t nvm .
# confirm image is present
docker images
# enter container terminal
docker run -it nvm bash
# set the base image to Debian
# https://hub.docker.com/_/debian/
FROM debian:latest
# replace shell with bash so we can source files
RUN rm /bin/sh && ln -s /bin/bash /bin/sh
# update the repository sources list
# and install dependencies
RUN apt-get update \
&& apt-get install -y curl \
&& apt-get -y autoclean
# nvm environment variables
ENV NVM_DIR /usr/local/nvm
ENV NODE_VERSION 4.4.7
# install nvm
# https://github.com/creationix/nvm#install-script
RUN curl --silent -o- https://raw.githubusercontent.com/creationix/nvm/v0.31.2/install.sh | bash
# install node and npm
RUN source $NVM_DIR/nvm.sh \
&& nvm install $NODE_VERSION \
&& nvm alias default $NODE_VERSION \
&& nvm use default
# add node and npm to path so the commands are available
ENV NODE_PATH $NVM_DIR/v$NODE_VERSION/lib/node_modules
ENV PATH $NVM_DIR/versions/node/v$NODE_VERSION/bin:$PATH
# confirm installation
RUN node -v
RUN npm -v
@ORESoftware

This comment has been minimized.

Copy link

commented Aug 31, 2017

Cool, how to install nodejs version 8? Do we just set ENV NODE_VERSION 4.4.7 to 8.x.x?

@ivpal

This comment has been minimized.

Copy link

commented Sep 13, 2017

You save my time.

@lauracowen

This comment has been minimized.

Copy link

commented Nov 3, 2017

Thank you for this! Just what I needed. Is there an accepted way to attribute reuse of a chunk of dockerfile in another dockerfile? (I'll put a link to this gist but happy to change that.)

@weifengHuang

This comment has been minimized.

Copy link

commented Nov 6, 2017

thank you !

@surakhchin

This comment has been minimized.

Copy link

commented Nov 21, 2017

yeah good shit

@ndedic

This comment has been minimized.

Copy link

commented Jan 25, 2018

Thanks

@tombh

This comment has been minimized.

Copy link

commented Jan 27, 2018

Seriously, thank you

@toddpi314

This comment has been minimized.

Copy link

commented Feb 1, 2018

To stay on LTS:

RUN source $NVM_DIR/nvm.sh \
    && nvm install --lts \
    && nvm alias default lts/* \
    && nvm use default
@mhuijser

This comment has been minimized.

Copy link

commented Feb 21, 2018

Works for centos too. Just replace FROM debian:latest to eg FROM centos:centos7. And probably you'll need to replace ENV NVM_DIR /usr/local/nvm with another target (eg. /root/.nvm)

Thanks!

@arsen-kha

This comment has been minimized.

Copy link

commented Mar 16, 2018

Thank you! I think it is not necessary to replace shell with bash as it is possible to replace source $NVM_DIR/nvm.sh with . $NVM_DIR/nvm.sh

@bjverhoe

This comment has been minimized.

Copy link

commented Mar 28, 2018

This also works for jessie with python installed. Replace FROM debian:latest to eg FROM python:3 All other commands are the same.

Thanks for this!

@celine-m-s

This comment has been minimized.

Copy link

commented Apr 2, 2018

Thank you so much for this!! You saved my day.

@zeroshree

This comment has been minimized.

Copy link

commented May 30, 2018

thanks

@BlueOrchard

This comment has been minimized.

Copy link

commented Jun 12, 2018

Thanks!

@floorz

This comment has been minimized.

Copy link

commented Jul 10, 2018

Thank you so much!

@ronzalo

This comment has been minimized.

Copy link

commented Jul 13, 2018

It works flawless!

@hthetiot

This comment has been minimized.

Copy link

commented Jul 22, 2018

Fail using Node 8, this also is the proper way you dont need nvm in your docker container.

# set the base image to Debian
# https://www.brianchristner.io/docker-image-base-os-size-comparison/
# https://hub.docker.com/_/debian/
FROM debian:jessie-slim

# Set debconf to run non-interactively
RUN echo 'debconf debconf/frontend select Noninteractive' | debconf-set-selections

# Install base dependencies
RUN apt-get update && apt-get upgrade -y
RUN apt-get install -y -q --no-install-recommends \
        apt-transport-https \
        build-essential \
        ca-certificates \
        curl \
        git \
        libssl-dev \
        python \
        rsync \
        software-properties-common \
        devscripts \
        autoconf \
        ssl-cert \
    && apt-get clean

# update the repository sources list
# and install dependencies
RUN curl -sL https://deb.nodesource.com/setup_8.x | bash -
RUN apt-get install -y nodejs

# confirm installation
RUN node -v
RUN npm -v

# Use latest npm
RUN npm i npm@latest -g
@kwhitejr

This comment has been minimized.

Copy link

commented Sep 18, 2018

This Dockerfile template blows up on me when it gets to setting the NVM_DIR env var: You have $NVM_DIR set to "/usr/local/nvm", but that directory does not exist. Check your profile files and environment. Additionally, is it implied that we've written a separate $NVM_DIR/nvm.sh?

FROM ubuntu:18.04

# Replace shell with bash so we can source files
RUN rm /bin/sh && ln -s /bin/bash /bin/sh

# Packages to
# --Acquire dependencies
ENV ACQUIRE_DEPENDENCIES "apt-transport-https ca-certificates curl gnupg git openssh-client"
# --Scripting dependencies
ENV SCRIPTING_DEPENDENCIES "jq"

RUN apt update \
  && apt install -y -q --no-install-recommends \
  ${ACQUIRE_DEPENDENCIES} \
  ${SCRIPTING_DEPENDENCIES}

# nvm environment variables
ENV NVM_DIR /usr/local/nvm
# ENV NVM_VERSION 0.33.11
ENV NODE_VERSION 8.10.0

# install nvm
# https://github.com/creationix/nvm#install-script
RUN curl --silent -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash

# install node and npm, set default alias
RUN source $NVM_DIR/nvm.sh \
  && nvm install $NODE_VERSION \
  && nvm alias default $NODE_VERSION \
  && nvm use default

# add node and npm to path so the commands are available
ENV NODE_PATH $NVM_DIR/v$NODE_VERSION/lib/node_modules
ENV PATH $NVM_DIR/versions/node/v$NODE_VERSION/bin:$PATH

# install yarn
RUN npm install -g yarn \
  && yarn --version
@kwhitejr

This comment has been minimized.

Copy link

commented Sep 18, 2018

Added RUN mkdir -p /usr/local/nvm above the env var declaration and problem appears resolved.

@blixit

This comment has been minimized.

Copy link

commented Oct 18, 2018

The initial gist was not working for me. Here is how I updated it:

# Installation of NVM, NPM and packages
RUN mkdir /usr/local/nvm
ENV NVM_DIR /usr/local/nvm
ENV NODE_VERSION 10.12.0
ENV NVM_INSTALL_PATH $NVM_DIR/versions/node/v$NODE_VERSION
RUN rm /bin/sh && ln -s /bin/bash /bin/sh
RUN curl --silent -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash
RUN source $NVM_DIR/nvm.sh \
   && nvm install $NODE_VERSION \
   && nvm alias default $NODE_VERSION \
   && nvm use default
ENV NODE_PATH $NVM_INSTALL_PATH/lib/node_modules
ENV PATH $NVM_INSTALL_PATH/bin:$PATH
RUN npm -v  # <= works
RUN node -v  # <= works
RUN nvm -v  # <= NOT works :( 
@KristerV

This comment has been minimized.

Copy link

commented Oct 25, 2018

Turns out v8 and v10 have different paths.

# Add node and npm to path so the commands are available
# Newer
# ENV NODE_PATH $NVM_DIR/v$NODE_VERSION/lib/node_modules
# ENV PATH $NVM_DIR/v$NODE_VERSION/bin:$PATH
# Older
ENV NODE_PATH $NVM_DIR/versions/node/v$NODE_VERSION/lib/node_modules
ENV PATH $NVM_DIR/versions/node/v$NODE_VERSION/bin:$PATH
@ljharb

This comment has been minimized.

Copy link

commented Oct 25, 2018

@KristerV the “newer” paths you mention are actually older; 0.12 and below use it. Node v8 and v10 have identical formats - perhaps you mean 0.10?

@agdula

This comment has been minimized.

Copy link

commented Oct 27, 2018

Here is as solution that does not mess with /bin/sh

ENV NVM_VERSION v0.33.11
ENV NODE_VERSION v7.5.0
ENV NVM_DIR /usr/local/nvm
RUN mkdir $NVM_DIR
RUN curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash

ENV NODE_PATH $NVM_DIR/v$NODE_VERSION/lib/node_modules
ENV PATH $NVM_DIR/versions/node/v$NODE_VERSION/bin:$PATH

RUN echo "source $NVM_DIR/nvm.sh && \
    nvm install $NODE_VERSION && \
    nvm alias default $NODE_VERSION && \
    nvm use default" | bash
@begin29

This comment has been minimized.

Copy link

commented Nov 5, 2018

@agdula,
tnx for solution, it works with small corrections.
There is small typo here ENV NODE_PATH $NVM_DIR/v$NODE_VERSION/lib/node_modules

NVM installs to $NVM_DIR/$NODE_VERSION but path is added as $NVM_DIR/v$NODE_VERSION

v letter is already included in NODE_VERSION here.

@shivrajbisht

This comment has been minimized.

Copy link

commented Dec 25, 2018

Thanks , really helpful

@mpahuja

This comment has been minimized.

Copy link

commented Apr 5, 2019

Thanks, very helpful.

@PEZO19

This comment has been minimized.

Copy link

commented Apr 9, 2019

To me it a was also necessary to create symlinks:

RUN ln -sf NVM_DIR/versions/node/v$NODE_VERSION/bin/node /usr/bin/nodejs
RUN ln -sf NVM_DIR/versions/node/v$NODE_VERSION/bin/node /usr/bin/node
RUN ln -sf NVM_DIR/versions/node/v$NODE_VERSION/bin/npm /usr/bin/npm

@renatoargh

This comment has been minimized.

Copy link

commented Apr 12, 2019

Thanks, @PEZO19! symlinks were the only thing that made it work! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.