62

How do I install the latest node inside a docker ubuntu 15.10 container? apt-get install nodejs installs version 0.1 and no npm

Thanks

1
  • Did you solve it? For me it installs npm v 3.10, can't get 6+
    – simPod
    Commented Dec 23, 2016 at 10:55

11 Answers 11

61

OK got it,

# update 
apt-get update
# install curl 
apt-get install curl
# get install script and pass it to execute: 
curl -sL https://deb.nodesource.com/setup_4.x | bash
# and install node 
apt-get install nodejs
# confirm that it was successful 
node -v
# npm installs automatically 
npm -v

Use curl -sL https://deb.nodesource.com/setup_5.x | bash for node 5.x

Replace 5 by your desired node version e.g. 8, 12, etc.

6
  • 17
    I have seen these same instructions all over the web but I cannot get it to install npm. After running the apt-get -y install nodejs, running an npm command does not work. I get /bin/sh: 1: npm: not found
    – Alex White
    Commented Jun 21, 2017 at 21:22
  • I have the same problem. Commented Sep 4, 2017 at 12:23
  • What if I want to have the exact version as 8.9.4? The above command fails. Commented Apr 13, 2018 at 12:41
  • 3
    See this. In the README section it explains how to get different version. Also, 4 is being end-of-lifed, so don't use the above command exactly. Commented Apr 26, 2018 at 23:08
  • 1
    /bin/sh: apt-get: command not found /bin/sh: apk: command not found using FROM amazonlinux:1
    – OZZIE
    Commented Feb 18, 2019 at 13:46
42

Updated solution as of Jan 2019:

FROM ubuntu:latest
USER root
WORKDIR /home/app
COPY ./package.json /home/app/package.json
RUN apt-get update
RUN apt-get -y install curl gnupg
RUN curl -sL https://deb.nodesource.com/setup_11.x  | bash -
RUN apt-get -y install nodejs
RUN npm install
3
  • 1
    but this way you get the latest version 11 of nodejs. what if you want 11.5.4 ? Commented Sep 16, 2019 at 12:40
  • Should have down to the minor versions in that deb registry, just replace the X with 5.4 (untested on my side)
    – Dan
    Commented Sep 16, 2019 at 21:06
  • 1
    This is not good practice. You will not run it as root!
    – Khalid
    Commented Sep 23, 2019 at 15:41
15

This is how I have been installing nodeJS into a container. In my case, I am using an nginx base image.

Use the following command

    apt-get update -yq \
    && apt-get install curl gnupg -yq \
    && curl -sL https://deb.nodesource.com/setup_8.x | bash \
    && apt-get install nodejs -yq

GNUPG is needed by the nodeJS installer. Without it, you will get the following error message;

[91mE: gnupg, gnupg2 and gnupg1 do not seem to be installed, but one of them is required for this operation
1
9

You can add a single line to your Dockerfile.

FROM node:8.2

There is a list of supported tag names here: https://hub.docker.com/_/node/

2
  • 47
    This answer doesn't help anyone using a different base image. Commented Jan 13, 2018 at 18:45
  • 1
    Then they can use Tomasz's answer
    – posit labs
    Commented Jan 13, 2018 at 22:54
3

Here is my Dockerfile to do this:

FROM ubuntu:20.04
RUN apt update
# We directly answer the questions asked using the printf statement
RUN printf 'y\n1\n\1n' | apt install nodejs
RUN apt install -y npm

Here we do our docker build:

docker build -t mynpm . 

Here is the version check to verify its success:

docker run -it mynpm npm -v

The output I get as a result is: 6.14.4

2

installing nodejs v8 with ubuntu 16.04 base image:

FROM ubuntu:16.04

WORKDIR /app

RUN echo "LC_ALL=en_US.UTF-8" >> /etc/environment
RUN echo "LANG=en_US.UTF-8" >> /etc/environment
RUN echo "NODE_ENV=development" >> /etc/environment
RUN more "/etc/environment"
#RUN locale-gen en_US en_US.UTF-8
#RUN dpkg-reconfigure locales

RUN apt-get update
#RUN apt-get upgrade -y
#RUN apt-get dist-upgrade -y
RUN apt-get install curl htop git zip nano ncdu build-essential chrpath libssl-dev libxft-dev pkg-config glib2.0-dev libexpat1-dev gobject-introspection python-gi-dev apt-transport-https libgirepository1.0-dev libtiff5-dev libjpeg-turbo8-dev libgsf-1-dev fail2ban nginx -y

# Install Node.js
RUN curl -sL https://deb.nodesource.com/setup_8.x | bash
RUN apt-get install --yes nodejs
RUN node -v
RUN npm -v
RUN npm i -g nodemon
RUN nodemon -v

# Cleanup
RUN apt-get update && apt-get upgrade -y && apt-get autoremove -y

I also installed some extra dependencies I need so you can clean up this code for your needs. But it installs nodejs & npm & nodemon.

2
  • 1
    running apt-get upgrade is not a good idea in a container, unless you are ok with very large containers
    – Chad Grant
    Commented Feb 21, 2020 at 20:55
  • 1
    @ChadGrant good point, commented out upgrade and dist-upgrade Commented Feb 23, 2020 at 6:54
2

From Node official docker image:

Docker

Dockerfile:

# Install node and npm:
ENV NODE_VERSION 14.18.2

RUN ARCH= && dpkgArch="$(dpkg --print-architecture)" \
  && case "${dpkgArch##*-}" in \
    amd64) ARCH='x64';; \
    ppc64el) ARCH='ppc64le';; \
    s390x) ARCH='s390x';; \
    arm64) ARCH='arm64';; \
    armhf) ARCH='armv7l';; \
    i386) ARCH='x86';; \
    *) echo "unsupported architecture"; exit 1 ;; \
  esac \
  && curl -fsSLO --compressed "https://nodejs.org/dist/v$NODE_VERSION/node-v$NODE_VERSION-linux-$ARCH.tar.xz" \
  && tar -xJf "node-v$NODE_VERSION-linux-$ARCH.tar.xz" -C /usr/local --strip-components=1 --no-same-owner \
  && rm "node-v$NODE_VERSION-linux-$ARCH.tar.xz" \
  && ln -s /usr/local/bin/node /usr/local/bin/nodejs \
  # smoke tests
  && node --version \
  && npm --version
1
  • This is great, because there is no dependency on a 3rd party like nodesource! Commented Jan 21, 2023 at 13:59
2

You can combine two docker images:

FROM node:16 AS node_base 
FROM ubuntu:bionic
COPY --from=node_base / /

See: https://stackoverflow.com/a/61054246/10030693

4
  • 3
    This also works: COPY --from=node:18.16.0-slim /usr/local/bin /usr/local/bin if you just want the node binary
    – veuncent
    Commented May 9, 2023 at 13:19
  • Neat tricks. Thanks Gilboot and @veuncent
    – AndyFaizan
    Commented Jul 17, 2023 at 15:35
  • npm is not foound in this approach
    – silentsudo
    Commented May 23 at 5:25
  • @silentsudo what do you mean?
    – Gilboot
    Commented May 24 at 17:46
1

I am using following Dockerfile to setup node version 8.10.0.

Here I have used NVM (Node Version Manager ), so we can choose which node version should be installed on that container. Please use absolute path of npm when installing node modules (eg: /root/.nvm/versions/node/v${NODE_VERSION}/bin/npm install leasot@latest -g)

   FROM ubuntu:18.04
   ENV NODE_VERSION=8.10.0
   RUN apt-get update && \
       apt-get install wget curl ca-certificates rsync -y
   RUN wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.2/install.sh | bash
   ENV NVM_DIR=/root/.nvm
   RUN . "$NVM_DIR/nvm.sh" && nvm install ${NODE_VERSION}
   RUN . "$NVM_DIR/nvm.sh" &&  nvm use v${NODE_VERSION}
   RUN . "$NVM_DIR/nvm.sh" && nvm alias default v${NODE_VERSION}
   RUN cp /root/.nvm/versions/node/v${NODE_VERSION}/bin/node /usr/bin/
   RUN cp /root/.nvm/versions/node/v${NODE_VERSION}/bin/npm /usr/bin/
   RUN /root/.nvm/versions/node/v${NODE_VERSION}/bin/npm install  leasot@latest -g

Note: This is a cropped Dockerfile.

0

I tried something like this, which i usually do, download binary and put it in classpath.

FROM ubuntu:22.04

# Copy files
COPY ./ /app

#Change Workdir
WORKDIR /app


RUN apt-get update && apt-get install -y wget build-essential && apt-get clean

RUN mkdir -p /node

# Install Node dependencies.
RUN wget https://nodejs.org/dist/v20.13.1/node-v20.13.1-linux-x64.tar.xz \
    && mkdir -p /node && tar -xf node-v20.13.1-linux-x64.tar.xz -C /node \
    && chmod +x -R /node/node-v20.13.1-linux-x64

# Setup Path
ENV PATH="/node/node-v20.13.1-linux-x64/bin:${PATH}"

RUN npm install

#Start App
ENTRYPOINT [ "node", "/app/index.js" ]
-1
# Base OS
FROM ubuntu:latest

# Node installation
RUN apt -y install nodejs npm

# Node package manager
RUN npm install -g n

# Upgrade to latest stable version
RUN n stable

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .