The npm command is not found in my NodeJS docker container - node.js

I created a Docker image:
$ docker build -t stephaneeybert/nodejs .
Sending build context to Docker daemon 2.56 kB
Step 1 : FROM debian
---> 1b088884749b
Step 2 : RUN apt-get clean && apt-get update
---> Using cache
---> b12133d6342f
Step 3 : RUN apt-get install -y curl
---> Using cache
---> 22dfb4882b12
Step 4 : RUN curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.32.1/install.sh | bash
---> Using cache
---> 27f2fac45254
Step 5 : RUN . ~/.nvm/nvm.sh; nvm install stable
---> Using cache
---> 20d99d545755
Step 6 : RUN . ~/.nvm/nvm.sh; nvm use stable
---> Using cache
---> 9ec14efb2407
Step 7 : RUN . ~/.nvm/nvm.sh; npm install -g npm
---> Using cache
---> d264d38565f3
Step 8 : EXPOSE 9001
---> Using cache
---> 29e3589557e1
Step 9 : ENTRYPOINT /usr/bin/tail -f /dev/null
---> Using cache
---> 2ce499300fe1
Successfully built 2ce499300fe1
The image script is:
FROM debian
RUN apt-get clean && apt-get update
RUN apt-get install -y curl
# Installing nodesjs
RUN curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.32.1/install.sh | bash
RUN . ~/.nvm/nvm.sh; nvm install stable
RUN . ~/.nvm/nvm.sh; nvm use stable
RUN . ~/.nvm/nvm.sh; npm install -g npm
EXPOSE 9001
ENTRYPOINT ["/usr/bin/tail", "-f", "/dev/null"]
Then I run the container and open a bash shell:
$ docker run -d -p 127.0.0.1:9001:9001 --name nodejs stephaneeybert/nodejs
c6dddf0a5eb0f11c897f63910eb01f2868fe0f39a80e5e2a580ef3a82935b27b
[stephane#stephane-ThinkPad-X301 nodejs]
$ docker exec -it nodejs bash
root#c6dddf0a5eb0:/#
Once in there, I try to get the version:
root#c6dddf0a5eb0:/# npm -v
bash: npm: command not found
But npm is not found.
When typing the command nvm use stable in the interactive shell, it give the following error: N/A: version "N/A" is not yet installed.
I understand there is an alias against a non existant node version.
The nvm ls command shows:
root#60446f9286d0:/# nvm ls
N/A
node -> stable (-> N/A) (default)
iojs -> N/A (default)
The debugger has this to show:
root#60446f9286d0:/# nvm debug
nvm --version: v0.32.1
$SHELL: /bin/bash
$HOME: /root
$NVM_DIR: '$HOME/.nvm'
$PREFIX: ''
$NPM_CONFIG_PREFIX: ''
nvm current: none
which node:
which iojs:
which npm:
npm config get prefix: bash: npm: command not found
npm root -g: bash: npm: command not found
1- How come I need to source this script . ~/.nvm/nvm.sh; on each command?
2- Why is my Node package manager not found in the bash shell?
EDIT: I changed a bit the content of the Dockerfile file:
RUN curl -o-https://raw.githubusercontent.com/creationix/nvm/v0.32.1/install.sh | bash \
&& . ~/.nvm/nvm.sh \
&& nvm install stable \
&& nvm alias default stable \
&& nvm use default
And building it now shows this:
Step 4 :RUN curl -o https://raw.githubusercontent.com/creationix/nvm/v0.32.1/install.sh | bash && . ~/.nvm/nvm.sh && nvm install stable && nvm alias default stable && nvm use default
---> Running in 7d2c404135dd
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 10250 100 10250 0 0 18258 0 --:--:-- --:--:-- --:--:-- 18238
=> Downloading nvm as script to '/root/.nvm'
=> Appending source string to /root/.bashrc
=> Close and reopen your terminal to start using nvm or run the following to use it now:
export NVM_DIR="/root/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm
######################################################################## 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v7.2.0 (npm v3.10.9)
Creating default alias: default -> stable (-> v7.2.0 *)
default -> stable (-> v7.2.0 *)
Now using node v7.2.0 (npm v3.10.9)
---> ad960a4addbe
Removing intermediate container 7d2c404135dd
Step 5 : EXPOSE 9001
---> Running in df9284421302
---> 14d386f009fb
Removing intermediate container df9284421302
Step 6 : ENTRYPOINT /usr/bin/tail -f /dev/null
---> Running in fa2d71b6dfdf
---> d02c8e88eb7f
Removing intermediate container fa2d71b6dfdf
Successfully built d02c8e88eb7f
I can see it installed node v7.2.0 and is using it.
But when I log into the container with the command docker exec -it nodejs bash it does not see any node anywhere:
root#f8f2a32b462a:/# nvm --version
0.32.1
root#f8f2a32b462a:/# npm --version
bash: npm: command not found
root#f8f2a32b462a:/# echo $NVM_DIR
/root/.nvm
root#f8f2a32b462a:/# ls -l /root/.nvm
total 100
-rwxr-xr-x 1 root root 313 Nov 26 13:01 nvm-exec
-rw-r--r-- 1 root root 95660 Nov 26 13:01 nvm.sh
root#f8f2a32b462a:/# ls -l /root/.npm
ls: cannot access /root/.npm: No such file or directory

I changed the way I install Node and did it without the nvm tool:
RUN curl -sL https://deb.nodesource.com/setup_7.x | bash
RUN apt-get install -y nodejs
Now, when logging in the container, it can find the Node executable:
$ docker run -d -p 127.0.0.1:9001:9001 --name nodejs stephaneeybert/nodejs
f3a2f054934ef92a9b05486b6f6dbe53abd4390826c06d1b7a490d671d8e3422
[stephane#stephane-ThinkPad-X301 nodejs]
$ docker exec -it nodejs bash
root#f3a2f054934e:/# npm --version
3.10.9
Maybe, when I was using the nvm tool, I should have sourced the npm command in the client shell . ~/.nvm/nvm.sh npm --version.

For amazonlinux (Ubuntu Fedora) you can install it using yum like this inside the Dockerfile:
RUN curl -sL https://rpm.nodesource.com/setup_10.x | bash # for node version 10.x
RUN yum -y install nodejs
RUN node --version # optional to check that it worked
RUN npm --version # optional to check that it worked
Use docker build -t [name] [local_folder_like_dot]
Took me ages to google, don't know much about linux but it seems like every version is so different from the other. It's like google docker npm, use apt get nah doesn't work, figure out wth linux version amasonlinux is.. Ok called Fedora.. it has yum instead of apt.. ok .. you need to answer yes in the yum install, docker doesn't allow it just skips it then.. wth.. I can hackz0r it with | yes nah that just spams y forevah.. ok I canz killall nah not in Fedora.. okiz can yum installz without questionz askz??! -y
2h later and we have this :)

Related

installing nodejs in dockerfile issue

I am trying to install nodejs in dockerfile with pyenv but I keep getting this error when I run it through my gitlab runner. I am trying to install version 16.12.0. Is there a better solution to this issue?
Dockerfile
#install npm
ENV NODE_VERSION=16.12.0
RUN apt install -y curl
RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/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}
ENV PATH="/root/.nvm/versions/node/v${NODE_VERSION}/bin/:${PATH}"
RUN node --version
RUN npm --version
output
Step 15/25 : ENV NODE_VERSION=16.12.0
---> Running in 7623dfe4669c
Removing intermediate container 7623dfe4669c
---> c1486340596a
Step 16/25 : RUN apt install -y curl
---> Running in a4661b68566b
WARNING: apt does not have a stable CLI interface. Use with caution in scripts.
Reading package lists...
Building dependency tree...
Reading state information...
curl is already the newest version (7.68.0-1ubuntu2.7).
0 upgraded, 0 newly installed, 0 to remove and 1 not upgraded.
Removing intermediate container a4661b68566b
---> d727779ba39b
Step 17/25 : RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
---> Running in c3661e8eead3
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
curl: (60) SSL certificate problem: self signed certificate in certificate chain
More details here: https://curl.haxx.se/docs/sslcerts.html
curl failed to verify the legitimacy of the server and therefore could not
establish a secure connection to it. To learn more about this situation and
how to fix it, please visit the web page mentioned above.
Removing intermediate container c3661e8eead3
---> beffd784c86b
Step 18/25 : ENV NVM_DIR=/root/.nvm
---> Running in 22b69a1563b2
Removing intermediate container 22b69a1563b2
---> 821b73dfd5fa
Step 19/25 : RUN . "$NVM_DIR/nvm.sh" && nvm install ${NODE_VERSION}
---> Running in 54c168c88ec7
/bin/sh: 1: .: Can't open /root/.nvm/nvm.sh
The command '/bin/sh -c . "$NVM_DIR/nvm.sh" && nvm install ${NODE_VERSION}' returned a non-zero code: 127
Cleaning up project directory and file based variables
00:00
ERROR: Job failed: exit code 127
In general you should avoid using version managers like nvm in Docker. You shouldn't need more than one version of a language interpreter at a time, and the version managers often require shell dotfile setup that's complicated to configure in Docker.
You don't say why you need Node. The easiest thing to do is to use the Docker Hub node image
FROM node:lts
# and none of what you show in the question
If you're using this to build the front end of your otherwise Python application, you can use a multi-stage build for this
FROM node:lts AS frontend
WORKDIR /frontend
COPY frontend/package*.json ./
RUN npm ci
COPY frontend/ ./
RUN npm build
FROM python:3.10
...
COPY --from=frontend /frontend/dist ./static/
If you really need Node in your otherwise Ubuntu-based image, the next easiest thing to do is just install it. The default Ubuntu nodejs package should work fine for most practical uses.
FROM ubuntu:20.04
RUN apt-get update \
&& DEBIAN_FRONTEND=noninteractive \
apt-get --no-install-recomends --assume-yes \
nodejs
If you really need it in a custom image, and it really needs to be a super specific version of Node, you should be able to just download Node and install it.
FROM ubuntu:20.04
RUN apt-get update \
&& DEBIAN_FRONTEND=noninteractive \
apt-get --no-install-recomends --assume-yes \
curl \
xz-utils
ARG node_version=v16.14.2
RUN cd /opt \
&& curl -LO https://nodejs.org/dist/${node_version}/node-${node_version}-linux-x64.tar.xz \
&& tar xJf node-${node_version}-linux-x64.tar.xz \
&& rm node-${node_version}-linux-x64.tar.xz
ENV PATH=/opt/node-${node_version}-linux-x64/bin:${PATH}

/bin/sh: npm: command not found

I am creating a docker image for stf.installing nodejs directly using apt-get install nodejs,had many issues.So i decided to go the nvm way.but after installation RUN npm install fails
I am building the docker image with su username docker build ..
make sure apt is up to date
RUN apt-get update --fix-missing
RUN apt-get install -y curl
RUN sudo apt-get install -y build-essential libssl-dev
ENV NVM_DIR /usr/local/nvm
ENV NODE_VERSION 6
# Install nvm with node and npm
RUN curl https://raw.githubusercontent.com/creationix/nvm/v0.30.1/install.sh | bash \
&& source $NVM_DIR/nvm.sh \
&& nvm install $NODE_VERSION \
&& nvm alias default $NODE_VERSION \
&& nvm use default
ENV NODE_PATH $NVM_DIR/v$NODE_VERSION/lib/node_modules
ENV PATH $NVM_DIR/versions/node/v$NODE_VERSION/bin:$PATH
#WORKDIR /usr/app
# Install app dependencies
RUN npm install
and the output
=> Downloading nvm from git to '/usr/local/nvm'
=> Cloning into '/usr/local/nvm'...
* (HEAD detached at v0.30.1)
master
=> Appending source string to /root/.bashrc
=> Close and reopen your terminal to start using nvm
Downloading https://nodejs.org/dist/v6.13.0/node-v6.13.0-linux-x64.tar.xz...
######################################################################## 100.0%
WARNING: checksums are currently disabled for node.js v4.0 and later
Now using node v6.13.0 (npm v3.10.10)
default -> 6 (-> v6.13.0)
Now using node v6.13.0 (npm v3.10.10)
Removing intermediate container eb9cb6c46f34
---> eeef6bf9f0f1
Step 38/52 : ENV NODE_PATH $NVM_DIR/v$NODE_VERSION/lib/node_modules
---> Running in 185bef8e530c
Removing intermediate container 185bef8e530c
---> 0e5bf7b1cfd9
Step 39/52 : ENV PATH $NVM_DIR/versions/node/v$NODE_VERSION/bin:$PATH
---> Running in 00d58493e199
Removing intermediate container 00d58493e199
---> 81ed9823020b
Step 40/52 : RUN npm install
---> Running in 1c7577133e24
/bin/sh: npm: command not found
The command '/bin/sh -c npm install' returned a non-zero code: 127
Please Help.Thanks
Ran the container and checked NPM path
root#69e513b99e68:/home/mobile/MobileFarmDocker#
echo $PATH
/usr/local/nvm/versions/node/v6.13.0/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
root#69e513b99e68:/home/mobile/MobileFarmDocker# which npm
/usr/local/nvm/versions/node/v6.13.0/bin/npm
Your $NODE_VERSION variable has value 6 as defined in the Dockerfile (ENV NODE_VERSION 6) but it should be 6.13.0 so that the following line may work properly:
ENV PATH $NVM_DIR/versions/node/v$NODE_VERSION/bin:$PATH
(mapping to /usr/local/nvm/versions/node/v6.13.0/bin)
Otherwise, you are actually generating the following (wrong) path:
/usr/local/nvm/versions/node/v6/bin

How to yum install Node.JS on Amazon Linux

I've seen the writeup on using yum to install the dependencies, and then installing Node.JS & NPM from source. While this does work, I feel like Node.JS and NPM should both be in a public repo somewhere.
How can I install Node.JS and NPM in one command on AWS Amazon Linux?
Stumbled onto this, was strangely hard to find again later. Putting here for posterity:
sudo yum install nodejs npm --enablerepo=epel
EDIT 3: As of July 2016, EDIT 1 no longer works for nodejs 4 (and EDIT 2 neither). This answer (https://stackoverflow.com/a/35165401/78935) gives a true one-liner.
EDIT 1: If you're looking for nodejs 4, please try the EPEL testing repo:
sudo yum install nodejs --enablerepo=epel-testing
EDIT 2: To upgrade from nodejs 0.12 installed through the EPEL repo using the command above, to nodejs 4 from the EPEL testing repo, please follow these steps:
sudo yum rm nodejs
sudo rm -f /usr/local/bin/node
sudo yum install nodejs --enablerepo=epel-testing
The newer packages put the node binaries in /usr/bin, instead of /usr/local/bin.
And some background:
The option --enablerepo=epel causes yum to search for the packages in the EPEL repository.
EPEL (Extra Packages for Enterprise Linux) is open source and free community based repository project from Fedora team which provides 100% high quality add-on software packages for Linux distribution including RHEL (Red Hat Enterprise Linux), CentOS, and Scientific Linux. Epel project is not a part of RHEL/Cent OS but it is designed for major Linux distributions by providing lots of open source packages like networking, sys admin, programming, monitoring and so on. Most of the epel packages are maintained by Fedora repo.
Via http://www.tecmint.com/how-to-enable-epel-repository-for-rhel-centos-6-5/
Like others, the accepted answer also gave me an outdated version.
Here is another way to do it that works very well:
$ curl --silent --location https://rpm.nodesource.com/setup_16.x | bash -
$ yum -y install nodejs
You can also replace the 16.x with another version, such as 18.x, 14.x, etc.
You can see all available versions on the NodeSource Github page, and pull from there as well if desired.
Note: you may need to run using sudo depending on your environment.
The accepted answer gave me node 0.10.36 and npm 1.3.6 which are very out of date. I grabbed the latest linux-x64 tarball from the nodejs downloads page and it wasn't too difficult to install: https://nodejs.org/dist/latest/.
# start in a directory where you like to install things for the current user
(For noobs : it downloads node package as node.tgz file in your directlry)
curl (paste the link to the one you want from the downloads page) >node.tgz
Now upzip the tar you just downloaded -
tar xzf node.tgz
Run this command and then also add it to your .bashrc:
export PATH="$PATH:(your install dir)/(node dir)/bin"
(example : export PATH ="$PATH:/home/ec2-user/mydirectory/node/node4.5.0-linux-x64/bin")
And update npm (only once, don't add to .bashrc):
npm install -g npm
Note that the -g there which means global, really means global to that npm instance which is the instance we just installed and is limited to the current user. This will apply to all packages that npm installs 'globally'.
Simple install with NVM...
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.2/install.sh | bash
. ~/.nvm/nvm.sh
nvm install node
To install a certain version (such as 18.12.1) of Node change the last line to
nvm install 18.12.1
For more information about how to use NVM visit the docs:
https://github.com/nvm-sh/nvm
The procedure that worked for me (following these rather old instructions with a few updates):
check git is installed git --version or install it via:
sudo yum install git
install gcc and openssl:
sudo yum install gcc-c++ make
sudo yum install openssl-devel
clone the git repo into a directory called node (which you can remove later):
git clone https://github.com/nodejs/node.git
decide which version of node you want at https://github.com/nodejs/node/releases
go to the node directory just created and install node
cd node
git checkout v6.1.0 - put your desired version after the v
./configure
make
sudo make install
test that node is installed / working with either node --version or simply node (exit node via process.exit() or ^C x 2 or ^C + exit)
check the npm version: npm --version and update if necessary via sudo npm install -g npm
Optional: remove the node directory with rm -r node
Notes:
The accepted answer didn't work since sudo yum install nodejs --enablerepo=epel-testing returns the error: No package nodejs available.
...and sudo yum install nodejs --enablerepo=epel (ie without -testing) only gave very old versions.
If you already have an old version of node installed you can remove it with:
sudo npm uninstall npm -g ...since npm can uninstall itself
sudo yum erase nodejs
sudo rm -f /usr/local/bin/node
(sudo yum rm nodejs in the accepted answer won't work as rm is not a valid yum command see yum --help)
It's possible to clone the node repo via git clone git://github.com/nodejs/node.git rather than git clone https://github.com/nodejs/node.gitbut you may get a various errors (see here).
If you already have a /node dir from a previous install, remove it before using the git clone command (or there'll be a conflict):
rm -r node
If you have trouble with any sudo npm... command - like sudo: npm: command not found and/or have permissions issues installing node packages without sudo, edit sudo nano /etc/sudoers and add :/usr/local/bin to the end of the line Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin so that it reads Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin
Seems no one is mentioning this. On Amazon Linux 2, official way to load EPEL is:
sudo amazon-linux-extras install epel
...then you may:
sudo yum install nodejs
See Extras Library (Amazon Linux 2)
For the v4 LTS version use:
curl --silent --location https://rpm.nodesource.com/setup_4.x | bash -
yum -y install nodejs
For the Node.js v6 use:
curl --silent --location https://rpm.nodesource.com/setup_6.x | bash -
yum -y install nodejs
I also ran into some problems when trying to install native addons on Amazon Linux. If you want to do this you should also install build tools:
yum install gcc-c++ make
I just came across this. I tried a few of the more popular answers, but in the end, what worked for me was Amazon's quick setup guide.
Tutorial: Setting Up Node.js on an Amazon EC2 Instance
The gist of the tutorial is:
Make sure you are ssh'd onto the instance.
Grab nvm: curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.32.0/install.sh | bash
Active . ~/.nvm/nvm.sh
Install node using nvm nvm install 4.4.5 (NOTE: You can choose a different version. Check out the remote versions first by running $ nvm ls-remote)
Finally, test that you have installed node Node correctly by running $ node -e "console.log('Running Node.js' + process.version)"
Hopefully this helps the next person.
RHEL, CentOS, CloudLinux, Amazon Linux or Fedora:
# As root
curl -fsSL https://rpm.nodesource.com/setup_12.x | bash -
# No root privileges
curl -fsSL https://rpm.nodesource.com/setup_12.x | sudo bash -
sudo yum install -y nodejs
I had Node.js 6.x installed and wanted to install Node.js 8.x.
Here's the commands I used (taken from Nodejs's site with a few extra steps to handle the yum cached data):
sudo yum remove nodejs: Uninstall Node.js 6.x (I don't know if this was necessary or not)
curl --silent --location https://rpm.nodesource.com/setup_8.x | sudo bash -
sudo yum clean all
sudo yum makecache: Regenerate metadata cache (this wasn't in the docs, but yum kept trying to install Node.jx 6.x, unsuccessfully, until I issued these last two commands)
sudo yum install nodejs: Install Node.js 8.x
sudo yum install nodejs npm --enablerepo=epel works for Amazon Linux AMI.
curl --silent --location https://rpm.nodesource.com/setup_6.x | bash -
yum -y install nodejs
works for RedHat.
The easiest solution is this( do these as root)
sudo su root
cd /etc
mkdir node
yum install wget
wget https://nodejs.org/dist/v9.0.0/node-v9.0.0-linux-x64.tar.gz
tar -xvf node-v9.0.0-linux-x64.tar.gz
cd node-v9.0.0-linux-x64/bin
./node -v
ln -s /etc/node-v9.0.0-linux-x64/bin/node node
https://nodejs.org/en/download/package-manager/#debian-and-ubuntu-based-linux-distributions
curl --silent --location https://rpm.nodesource.com/setup_10.x | sudo bash -
sudo yum -y install nodejs
Official Documentation for EC2-Instance works for me: https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/setting-up-node-on-ec2-instance.html
1. curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.32.0/install.sh | bash
2. . ~/.nvm/nvm.sh
3. nvm ls-remote (=> find your version x.x.x =>) nvm install x.x.x
4. node -e "console.log('Running Node.js ' + process.version)"
MAY 2022
I spent way too long on this. My Amazon Linux 2 configuration, running as root.
#!/usr/bin/env zsh
# https://stackoverflow.com/questions/11542846/nvm-node-js-recommended-install-for-all-users
echo "=================================N=O=D=E========================================"
cd /usr/local/bin || exit 90
git clone https://github.com/nvm-sh/nvm.git .nvm
\. "/usr/local/bin/.nvm/nvm.sh"
nvm install --lts
node -e "console.log('Running Node.js ' + process.version)"
cat << "EOF" > /etc/profile.d/npm.sh
#!/usr/bin/env bash
export NVM_DIR="/usr/local/bin/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm'}
EOF
chmod 755 /etc/profile.d/npm.sh
npm install -g npm
June 2022 - The system really hates when things arn't linked in the bin. Here's a small update to help if you need things accessible by other users. Admittedly adding /etc/profile.d/npm.sh is just what nvm suggests, but I find it over-rated. I think it could be removed in place of purely the ln -s. happy hacking
#!/bin/zsh
# https://stackoverflow.com/questions/11542846/nvm-node-js-recommended-install-for-all-users
echo "=================================N=O=D=E========================================"
cd /usr/local/bin || exit 90
git clone https://github.com/nvm-sh/nvm.git .nvm
# this uncontrolled script has an unbound variable $HOME
# #link https://github.com/Drop-In-Gaming/dropingaming.com/runs/6437329820?check_suite_focus=true
\. "/usr/local/bin/.nvm/nvm.sh" || true
# todo - try to install 18
nvm install --lts
nvm install 17
node -e "console.log('Running Node.js ' + process.version)"
cat << "EOF" > /etc/profile.d/npm.sh
#!/usr/bin/env bash
export NVM_DIR="/usr/local/bin/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm'}
EOF
echo 'source /etc/profile.d/npm.sh' >> /root/.bashrc
echo 'source /etc/profile.d/npm.sh' >> /root/.zshrc
echo 'source /etc/profile.d/npm.sh' >> /home/ssm-user/.bashrc
echo 'source /etc/profile.d/npm.sh' >> /home/ssm-user/.zshrc
echo 'source /etc/profile.d/npm.sh' >> /home/www-data/.bashrc
echo 'source /etc/profile.d/npm.sh' >> /home/www-data/.zshrc
chmod 755 /etc/profile.d/npm.sh
npm install -g npm
echo "===========================WHERE==IS==NODE==========================="
which node
which npm
echo "symlinking to /usr/bin/"
if [ -e /usr/bin/node ]; then
sudo rm -f /usr/bin/node
fi
if [ -e /usr/bin/npm ]; then
sudo rm -f /usr/bin/npm
fi
sudo ln -s "$(which node)" /usr/bin/
sudo ln -s "$(which npm)" /usr/bin/
For those who want to have the accepted answer run in Ansible without further searches, I post the task here for convenience and future reference.
Accepted answer recommendation: https://stackoverflow.com/a/35165401/78935
Ansible task equivalent
tasks:
- name: Setting up the NodeJS yum repository
shell: curl --silent --location https://rpm.nodesource.com/setup_10.x | bash -
args:
warn: no
# ...
As others mentioned using epel gives a really outdated version, here is a little script I just wrote instead to add to the CI pipeline or pass it to ec2 user-data to install the latest version of node, simply replace the version with what you want, and the appropriate distro of Linux you are using.
The following example is for amazon-Linux-2-AMI
#!/bin/bash
version='v14.13.1'
distro='linux-x64'
package_name="node-$version-$distro"
package_location="/usr/local/lib/"
curl -O https://nodejs.org/download/release/latest/$package_name.tar.gz
tar -xvf $package_name.tar.gz -C $package_location
rm -rfv $package_name.tar.gz
echo "export PATH=$package_location/$package_name/bin:\$PATH" >> ~/.profile
if you want to test it in the same shell simply run
. ~/.profile
I usually use NVM to install node on server. It gives me option to install multiple version of nodejs.
Commands are given below :
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash
then check if it's installed properly using :
command -v nvm
after that, run this to install latest version :
nvm install node
or
nvm install 11
As mentioned in official documentation , simple below 2 steps -
curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
sudo apt-get install -y nodejs
You can update/install the node by reinstalling the installed package to the current version which may save us from lotta of errors, while doing the update.
This is done by nvm with the below command. Here, I have updated my node version to 8 and reinstalled all the available packages to v8 too!
nvm i v8 --reinstall-packages-from=default
It works on AWS Linux instance as well.
As stated in the Amazon docs (Setting Up Node.js on an Amazon EC2 Instance), just run the following commands:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
. ~/.nvm/nvm.sh
nvm install --lts
Done!

How to install nvm in docker?

I am in the process of building a new Docker image and I'm looking to get NVM installed so I can manage nodejs.
Reading the docs on how to install NVM they mention that you need to source your .bashrc file in order to start using NVM.
I've tried to set this up in a Dockerfile, but so far building fails with the error:
"bash: nvm: command not found"
Here are the relevant lines from my Dockerfile:
ADD files/nvm_install.sh /root/
RUN chmod a+x /root/nvm_install.sh
RUN bash -c "/root/nvm_install.sh"
RUN bash -l -c "source /root/.bashrc"
RUN cd /root
RUN bash -l -c "nvm install 0.10.31"
Here is the output from trying to build:
docker build -t nginx_dock .
Step 0 : FROM ubuntu
---> 826544226fdc
Step 1 : MAINTAINER dficociello
---> Using cache
---> da3bc340fbb3
Step 2 : RUN apt-get update
---> Using cache
---> 6b6b611feb4f
Step 3 : RUN apt-get install nginx curl -y
---> Using cache
---> 159eb0b16d23
Step 4 : RUN touch /root/.bashrc
---> Using cache
---> 5e9e8216191b
Step 5 : ADD files/nginx.conf /etc/nginx/
---> Using cache
---> c4a4a11296a2
Step 6 : ADD files/nvm_install.sh /root/
---> Using cache
---> b37cba2a18ca
Step 7 : RUN chmod a+x /root/nvm_install.sh
---> Using cache
---> bb13e2a2893d
Step 8 : RUN bash -c "/root/nvm_install.sh"
---> Using cache
---> 149b49a8fc71
Step 9 : RUN bash -l -c "source /root/.bashrc"
---> Running in 75f353ed0d53
---> 0eae8eae7874
Removing intermediate container 75f353ed0d53
Step 10 : RUN cd /root
---> Running in feacbd998dd0
---> 284293ef46b0
Removing intermediate container feacbd998dd0
Step 11 : RUN bash -l -c "nvm install 0.10.31"
---> Running in 388514d11067
bash: nvm: command not found
2014/09/17 13:15:11 The command [/bin/sh -c bash -l -c "nvm install 0.10.31"] returned a non-zero code: 127
I'm pretty new to Docker so I may be missing something fundamental to writing Dockerfiles, but so far all the reading I've done hasn't shown me a good solution.
When you RUN bash... each time that runs in a separate process, anything set in the environment is not maintained. Here's how I install nvm:
# Replace shell with bash so we can source files
RUN rm /bin/sh && ln -s /bin/bash /bin/sh
# 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 install -y -q --no-install-recommends \
apt-transport-https \
build-essential \
ca-certificates \
curl \
git \
libssl-dev \
wget \
&& rm -rf /var/lib/apt/lists/*
ENV NVM_DIR /usr/local/nvm # or ~/.nvm , depending
ENV NODE_VERSION 0.10.33
# Install nvm with node and npm
RUN curl https://raw.githubusercontent.com/creationix/nvm/v0.20.0/install.sh | bash \
&& . $NVM_DIR/nvm.sh \
&& nvm install $NODE_VERSION \
&& nvm alias default $NODE_VERSION \
&& nvm use default
ENV NODE_PATH $NVM_DIR/v$NODE_VERSION/lib/node_modules
ENV PATH $NVM_DIR/v$NODE_VERSION/bin:$PATH
Update 20/02/2020: This solution works if you're using a debian base image. If you're using ubuntu, see this answer.
Here is the cleanest way to install nvm that I have found:
SHELL ["/bin/bash", "--login", "-c"]
RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash
RUN nvm install 10.15.3
Explanation
The first line sets the Dockerfile's default shell to a bash login shell. Note: this means that every subsequent RUN, CMD, and ENTRYPOINT will be run under the current user (usually root), and source the ~/.bashrc file if run in the shell form.
The second line installs nvm with bash. When the script is run with bash, it appends to the ~/.bashrc file.
The third line installs a particular version of nodejs and uses it. The nvm, npm, and node commands are available because they are run via a bash login shell (see line 1).
To help everyone that are looking for a way to install the Node.js with NVM on Ubuntu (last version), I made the dockerfile below. I'm using the last version of Docker, Ubuntu, Node.js and the NVM is working properly (the $PATH was fixed). I'm using this in a production environment.
$ docker info \
Server Version: 1.9.1
Kernel Version: 4.1.13-boot2docker
Operating System: Boot2Docker 1.9.1 (TCL 6.4.1); master : cef800b - Fri Nov 20 19:33:59 UTC 2015
Node.js Version: stable 4.2.4 LTS
Ubuntu Version: 14.04.3
dockerfile:
FROM ubuntu:14.04.3
# Replace shell with bash so we can source files
RUN rm /bin/sh && ln -s /bin/bash /bin/sh
# make sure apt is up to date
RUN apt-get update --fix-missing
RUN apt-get install -y curl
RUN apt-get install -y build-essential libssl-dev
ENV NVM_DIR /usr/local/nvm
ENV NODE_VERSION 4.2.4
# Install nvm with node and npm
RUN curl https://raw.githubusercontent.com/creationix/nvm/v0.30.1/install.sh | bash \
&& source $NVM_DIR/nvm.sh \
&& nvm install $NODE_VERSION \
&& nvm alias default $NODE_VERSION \
&& nvm use default
ENV NODE_PATH $NVM_DIR/v$NODE_VERSION/lib/node_modules
ENV PATH $NVM_DIR/versions/node/v$NODE_VERSION/bin:$PATH
RUN mkdir /usr/app
RUN mkdir /usr/app/log
WORKDIR /usr/app
# log dir
VOLUME /usr/app/log
# Bundle app source
COPY . /usr/app
# Install app dependencies
RUN npm install
EXPOSE 3000
CMD ["node", "server.js"]
Nvm paths have changed since the accepted answer, so if you want to use a more up-to-date nvm version, you need to make a few changes. Also, it is not necessary to remap sh to make it work:
ENV NVM_DIR /usr/local/nvm
RUN curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.1/install.sh | bash
ENV NODE_VERSION v7.9.0
RUN /bin/bash -c "source $NVM_DIR/nvm.sh && nvm install $NODE_VERSION && nvm use --delete-prefix $NODE_VERSION"
ENV NODE_PATH $NVM_DIR/versions/node/$NODE_VERSION/lib/node_modules
ENV PATH $NVM_DIR/versions/node/$NODE_VERSION/bin:$PATH
Not sure if you will need the --delete-prefix option on the nvm use - I did, but that may be something strange about my base image.
Took me an hour or two to figure out the cleanest way to do it. --login doesn't seem to execute .bashrc so you have to supply -i to launch it in interactive mode. This causes Docker to yell at you for a bit so I only launch this way for the installation, then reset to my standard shell.
# Installing Node
SHELL ["/bin/bash", "--login", "-i", "-c"]
RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.2/install.sh | bash
RUN source /root/.bashrc && nvm install 12.14.1
SHELL ["/bin/bash", "--login", "-c"]
Each RUN in a Dockerfile is executed in a different container. So if you source a file in a container, its content will not be available in the next one.
That is why when you install an application and you need to do several steps, you must do it in the same container.
With your example:
ADD files/nvm_install.sh /root/
RUN chmod a+x /root/nvm_install.sh && \
/root/nvm_install.sh && \
source /root/.bashrc && \
cd /root && \
nvm install 0.10.31
This is based on the top answer and works in 2018:
# Replace shell with bash so we can source files
RUN rm /bin/sh && ln -s /bin/bash /bin/sh
# Install base dependencies
RUN apt-get update && apt-get install -y -q --no-install-recommends \
apt-transport-https \
build-essential \
ca-certificates \
curl \
git \
libssl-dev \
wget
ENV NVM_DIR /usr/local/nvm
ENV NODE_VERSION 8.11.3
WORKDIR $NVM_DIR
RUN curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash \
&& . $NVM_DIR/nvm.sh \
&& nvm install $NODE_VERSION \
&& nvm alias default $NODE_VERSION \
&& nvm use default
ENV NODE_PATH $NVM_DIR/versions/node/v$NODE_VERSION/lib/node_modules
ENV PATH $NVM_DIR/versions/node/v$NODE_VERSION/bin:$PATH
Note that nvm is not a bash command, it is an alias. This can screw you up if you're relying on $PATH.
Updated 2022
Just one answer put the curl installation but did not work the entire Dockerfile
Here my Dockerfile ready to copy/paste in which I install latest nvm 2022 version with latest Ubuntu
FROM ubuntu
# nvm requirements
RUN apt-get update
RUN echo "y" | apt-get install curl
# nvm env vars
RUN mkdir -p /usr/local/nvm
ENV NVM_DIR /usr/local/nvm
# IMPORTANT: set the exact version
ENV NODE_VERSION v16.17.0
RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
RUN /bin/bash -c "source $NVM_DIR/nvm.sh && nvm install $NODE_VERSION && nvm use --delete-prefix $NODE_VERSION"
# add node and npm to the PATH
ENV NODE_PATH $NVM_DIR/versions/node/$NODE_VERSION/bin
ENV PATH $NODE_PATH:$PATH
RUN npm -v
RUN node -v
Log
Notes
Set the exact version of nodejs is mandatory because if you set nvm use v16, 16.17.0 will be downloaded and then there is no way to get the 16.17.0 from v16 to be set in the PATH
nvm force us to search last and specific version raw.githubusercontent../nvm/v0.39.1/install.sh of the script its readme https://github.com/nvm-sh/nvm#install--update-script instead to have something like this: raw.githubusercontent../nvm/latest/install.sh
If your nodejsapp is classic and standard, don't use nvm. Instead use FROM node:16 directly
You could use this nvm snippet in another projects in which the base image is not a node:16. For example, I have a python docker image and nodejs was required.
Here is my working version
FROM ubuntu:14.04
# Declare constants
ENV NVM_VERSION v0.29.0
ENV NODE_VERSION v5.0.0
# Replace shell with bash so we can source files
RUN rm /bin/sh && ln -s /bin/bash /bin/sh
# Install pre-reqs
RUN apt-get update
RUN apt-get -y install curl build-essential
# Install NVM
RUN curl -o- https://raw.githubusercontent.com/creationix/nvm/${NVM_VERSION}/install.sh | bash
# Install NODE
RUN source ~/.nvm/nvm.sh; \
nvm install $NODE_VERSION; \
nvm use --delete-prefix $NODE_VERSION;
Took help from #abdulljibali and #shamisis answers.
Based upon the suggestion in #Kuhess answer, I replaced the source command with the following in my Dockerfile
RUN cat ~/.nvm/nvm.sh >> installnode.sh
RUN echo "nvm install 0.10.35" >> installnode.sh
RUN sh installnode.sh
25-Feb-2021
The main problem is with use of the 'source' directive, which is bash shell specific.
What worked for me was replacing 'source' with '.' for a Ubuntu 18 install.
My Dockerfile:
FROM ubuntu:bionic
RUN \
apt update && \
apt upgrade -y && \
apt install -y curl
ENV NVM_DIR /root/.nvm
ENV NODE_VERSION 14.16
# Install nvm with node and npm
RUN curl -sL https://raw.githubusercontent.com/creationix/nvm/v0.35.3/install.sh | bash \
&& . $NVM_DIR/nvm.sh \
&& nvm install $NODE_VERSION
I must begin with the fact that I searched all over to get a working example of nvm inside docker and I found none. Even the answers in this thread did not work.
So, I spent quite some time and came up with one that works:
# install dependencies
RUN apt-get update && apt-get install -y \
curl \
npm \
nodejs \
git;
# compatibility fix for node on ubuntu
RUN ln -s /usr/bin/nodejs /usr/bin/node;
# install nvm
RUN curl https://raw.githubusercontent.com/creationix/nvm/v0.24.1/install.sh | sh;
# invoke nvm to install node
RUN cp -f ~/.nvm/nvm.sh ~/.nvm/nvm-tmp.sh; \
echo "nvm install 0.12.2; nvm alias default 0.12.2" >> ~/.nvm/nvm-tmp.sh; \
sh ~/.nvm/nvm-tmp.sh; \
rm ~/.nvm/nvm-tmp.sh;
Notice how I have installed nodejs via apt-get as well. I found that some packages don't get installed inside docker unless this is done.
A key difference between the attempt to get the nvm command in the question:
RUN bash -l -c "source /root/.bashrc"
which doesn't work and the attempt to do the same in the accepted answer:
source $NVM_DIR/nvm.sh
Is that the second version sources the nvm.sh script directly, whereas the original tries to do it via the .bashrc file.
The .bashrc file has a line in it early on which exits if it's being run in a non interactive shell:
# If not running interactively, don't do anything
case $- in
*i*) ;;
*) return;;
esac
So it never gets to the bit where it would have sourced nvm.sh which actually puts the nvm command in your shell.
I wouldn't be surprised if docker is running this stuff in a non interactive shell. This hadn't been explicitly pointed out, so I thought I would mention it as it's what caught me out when I was doing something similar with vagrant.
None of these worked for me, for my python3-onbuild container I had to force-create symbolic links to the nvm installation.
# Install npm and nodejs
RUN apt-get install -y build-essential libssl-dev
RUN mkdir /root/.nvm
ENV NVM_DIR /root/.nvm
ENV NODE_VERSION 8.9.4
RUN curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.9/install.sh | bash
RUN chmod +x $HOME/.nvm/nvm.sh
RUN . $HOME/.nvm/nvm.sh && nvm install $NODE_VERSION && nvm alias default $NODE_VERSION && nvm use default && npm install -g npm
RUN ln -sf /root/.nvm/versions/node/v$NODE_VERSION/bin/node /usr/bin/nodejs
RUN ln -sf /root/.nvm/versions/node/v$NODE_VERSION/bin/node /usr/bin/node
RUN ln -sf /root/.nvm/versions/node/v$NODE_VERSION/bin/npm /usr/bin/npm
This is what worked for me (I'm using debian buster):
RUN apt-get update
RUN apt-get install -y build-essential checkinstall libssl-dev
RUN curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.35.1/install.sh | bash
SHELL ["/bin/bash", "--login", "-c"]
You should now be able to do nvm install <version>.
This installs the lts-version of nodejs when extending image "php:7.4.15" (debian:buster-slim):
# Install nvm to install npm and node.js
ENV NVM_DIR /root/.nvm
ENV NODE_VERSION lts/*
RUN mkdir $HOME/.nvm && \
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh | bash && \
chmod +x $HOME/.nvm/nvm.sh && \
. $HOME/.nvm/nvm.sh && \
nvm install --latest-npm "$NODE_VERSION" && \
nvm alias default "$NODE_VERSION" && \
nvm use default && \
DEFAULT_NODE_VERSION=$(nvm version default) && \
ln -sf /root/.nvm/versions/node/$DEFAULT_NODE_VERSION/bin/node /usr/bin/nodejs && \
ln -sf /root/.nvm/versions/node/$DEFAULT_NODE_VERSION/bin/node /usr/bin/node && \
ln -sf /root/.nvm/versions/node/$DEFAULT_NODE_VERSION/bin/npm /usr/bin/npm
nvm not found can result from it being installed for a different user than the one who is executing the container. You may need to prefix the installation with the custom user who is executing the container. The last USER statement defines the container user.
USER $USERNAME
RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
Reason
Diving into a nvm install script, e. g. v0.39.1, one can see that is installed into $HOME of the current user. If you have not changed it, the default user of a ubuntu image is root. When starting the container with a different user however, nvm won't be found; hence make sure user of installation and execution align.
nvm_default_install_dir() {
[ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm"
}
2022 update:
based off https://stackoverflow.com/a/60137919/2047472 I came up with:
FROM python:3.10
RUN touch .profile
SHELL ["/bin/bash", "--login", "-i", "-c"]
RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
SHELL ["/bin/bash", "--login", "-c"]
RUN nvm install
RUN node -v
RUN npm -v
if you use .nvmrc and use source to init nvm, beware of a bug in nvm.sh causing it to exit with return code 3 when .nvmrc is present in current or parent directory
I had to touch .profile as it didn't exist, otherwise nvm is not activated in subsequent RUN commands
touch .bashrc didn't work
After testing most information here as well as other posts, turned out in my case it was related to permission issues, that lead to weird bugs, like failing to install a npm project unless run as root user, my setup was to run VueJs along a PHP CMS, the final portion that worked was:
ENV NVM_DIR $TMP_STORE/nvm
ENV NODE_VERSION 16.15.0
RUN chown -R www-data:www-data /var/www/
USER www-data
RUN export XDG_CONFIG_HOME=$TMP_STORE \
&& curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
#RUN chown -R www-data:www-data $NVM_DIR
RUN source $NVM_DIR/nvm.sh \
&& nvm install $NODE_VERSION \
&& nvm alias default $NODE_VERSION \
&& nvm use default
ENV NODE_PATH $NVM_DIR/v$NODE_VERSION/lib/node_modules
ENV PATH $NVM_DIR/versions/node/v$NODE_VERSION/bin:$PATH
RUN npm install -g #vue/cli \
&& npm install -g vue
USER root
The whole docker configuration can be found here
Also had an oddly hard time for my docker file extending the CircleCI runner image - this worked for me:
FROM circleci/runner:launch-agent
SHELL ["/bin/bash", "--login", "-c"]
USER $USERNAME
RUN wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.2/install.sh | bash;
ENV NODE_VERSION 18.12.1
ENV NVM_DIR $HOME/.nvm
RUN \
. ~/.nvm/nvm.sh \
&& nvm install $NODE_VERSION \
&& nvm alias default $NODE_VERSION \
&& nvm use default;
ENV NODE_PATH $NVM_DIR/v$NODE_VERSION/lib/node_modules
ENV PATH $NVM_DIR/versions/node/v$NODE_VERSION/bin:$PATH
RUN npm -v
RUN node -v
I had a really hard time getting NVM working properly on an alpine-based image. I ultimately just copied over a bunch of the shared directories from an official Node alpine image. Seems to be working quite well so far.
# Dockerfile
###############################################################################
# https://docs.docker.com/develop/develop-images/multistage-build/
# Builder Image
# This image is intended to build the app source code, not to run it.
###############################################################################
FROM node:16-alpine as builder
WORKDIR /build-tmp
COPY ./dist/src/yarn.lock .
COPY ./dist/src/package.json .
RUN yarn install --production
###############################################################################
# Execution image
# Nothing from the builder image is included here unless explicitly copied
###############################################################################
FROM osgeo/gdal:alpine-normal-3.4.2
# It's seemingly very difficult to build a specific version of node in an Alpine linux
# image, so let's copy node from the builder image into this execution image!
COPY --from=builder /usr/lib /usr/lib
COPY --from=builder /usr/local/share /usr/local/share
COPY --from=builder /usr/local/lib /usr/local/lib
COPY --from=builder /usr/local/include /usr/local/include
COPY --from=builder /usr/local/bin /usr/local/bin
...
CMD ["node", "main"]
Here is a solution I recently used:
# Install nvm/Node.js
ENV NVM_VERSION=0.39.1
ENV NODE_VERSION=16.17.1
RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v$NVM_VERSION/install.sh | bash
RUN bash --login -c "nvm install $NODE_VERSION"
# Do whatever with nvm
RUN bash --login -c "nvm use $NODE_VERSION && npm [...]"
2023 to use as dev-container
I started to use the dev-contieners and to set up my enviroment I used the next Dockcerfile that works perfectly with the purpose I've described. I share this because spend a hard time to achive it.
FROM ubuntu:22.04
ENV HOME="/root"
ENV NVM_DIR="${HOME}/.nvm"
ENV NVM_VERSION=v0.39.3
ENV NODE_VERSION=18
RUN apt-get update \
&& apt-get install -y --no-install-recommends build-essential\
libssl-dev \
git \
curl \
ca-certificates \
&& git clone https://github.com/nvm-sh/nvm.git "${NVM_DIR}"
WORKDIR "${NVM_DIR}"
RUN git checkout ${NVM_VERSION} \
&& \. "./nvm.sh" \
&& nvm install "${NODE_VERSION}" \
&& echo '[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"' >> "${HOME}/.bashrc" \
&& echo '[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"' >> "${HOME}/.bashrc"
WORKDIR "${HOME}"
This is intended to work with bash (I don't have idea if works with another type of shell). The command that I used to run the image was:
docker run -ti --rm --name node_test <your-image-name | id-image> /bin/bash

Install nodejs using docker

I have tried the following to install nodes into a centos box but I get an error when it reaches ./configure
Step 6 : RUN tar -zxf node-v0.10.28-linux-x64.tar.gz
---> Running in ebc71472544d
---> c97289348900
Removing intermediate container ebc71472544d
Step 7 : RUN cd /node-v0.10.28-linux-x64
---> Running in 3470f862c586
---> 1771d01a5da0
Removing intermediate container 3470f862c586
Step 8 : RUN ./configure
---> Running in 16a811766136
/bin/sh: ./configure: No such file or directory
My Dockerfile
#Install NodeJS
RUN cd /usr/src
RUN wget http://nodejs.org/dist/v0.10.28/node-v0.10.28-linux-x64.tar.gz
RUN tar -zxf node-v0.10.28-linux-x64.tar.gz
RUN cd /node-v0.10.28-linux-x64
RUN ./configure
RUN make &&
RUN make install
Am I using the right way of installing nodes to centos using the Dockerfile?
I'm assuming this isn't the whole Dockerfile, right? Otherwise you're missing at least a FROM.
Try to change the last 4 lines like that:
RUN cd /node-v0.10.28-linux-x64 && ./configure
RUN cd /node-v0.10.28-linux-x64 && make
RUN cd /node-v0.10.28-linux-x64 && make install
or like this
RUN cd /node-v0.10.28-linux-x64 && ./configure && make && make install
As far as I can tell, docker is running each RUN command as a separate shell, so just changing the directory won't be remembered in the next commands.
Here is an example Docker file to test this:
FROM ubuntu
RUN cd /etc
RUN pwd
And here is the build log:
Step 0 : FROM ubuntu
---> 99ec81b80c55
Step 1 : RUN cd /etc
---> Running in a4c25ee340a8
---> 82ad93bdd18c
Removing intermediate container a4c25ee340a8
Step 2 : RUN pwd
---> Running in f535178df40c
/
---> 495c68757268
[EDIT]
Another option is to use WORKDIR, like this:
#Install NodeJS
WORKDIR /usr/src
ADD http://nodejs.org/dist/v0.10.28/node-v0.10.28-linux-x64.tar.gz .
RUN tar -zxf node-v0.10.28-linux-x64.tar.gz
WORKDIR node-v0.10.28-linux-x64
RUN ./configure
RUN make &&
RUN make install
My solution using nvm:
RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
RUN source $HOME/.bashrc && nvm install 12.14.1
RUN ln -s $HOME/.nvm/versions/node/v12.14.1/bin/node /usr/bin/node
RUN ln -s $HOME/.nvm/versions/node/v12.14.1/bin/npm /usr/bin/npm
RUN node -v
RUN npm -v

Resources