Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to install nvm in a Dockerfile?

I'm trying to install nvm within a Dockerfile. It seems like it installs OK, but the nvm command is not working.

Dockerfile:

# Install nvm
RUN git clone http://github.com/creationix/nvm.git /root/.nvm;
RUN chmod -R 777 /root/.nvm/;
RUN sh /root/.nvm/install.sh;
RUN export NVM_DIR="$HOME/.nvm";
RUN echo "[[ -s $HOME/.nvm/nvm.sh ]] && . $HOME/.nvm/nvm.sh" >> $HOME/.bashrc;
RUN nvm ls-remote;

Build output:

Step 23/39 : RUN git clone http://github.com/creationix/nvm.git /root/.nvm;
 ---> Running in ca485a68b9aa
Cloning into '/root/.nvm'...
 ---> a6f61d486443
Removing intermediate container ca485a68b9aa
Step 24/39 : RUN chmod -R 777 /root/.nvm/
 ---> Running in 6d4432926745
 ---> 30e7efc5bd41
Removing intermediate container 6d4432926745
Step 25/39 : RUN sh /root/.nvm/install.sh;
 ---> Running in 79b517430285
=> Downloading nvm from git to '$HOME/.nvm'
=> Cloning into '$HOME/.nvm'...
* (HEAD detached at v0.33.0)
  master
=> Compressing and cleaning up git repository

=> Appending nvm source string to /root/.profile
=> bash_completion source string already in /root/.profile
npm info it worked if it ends with ok
npm info using [email protected]
npm info using [email protected]
npm info ok
=> Installing Node.js version 6.9.5
Downloading and installing node v6.9.5...
Downloading https://nodejs.org/dist/v6.9.5/node-v6.9.5-linux-x64.tar.xz...
######################################################################## 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v6.9.5 (npm v3.10.10)
Creating default alias: default -> 6.9.5 (-> v6.9.5 *)
/root/.nvm/install.sh: 136: [: v6.9.5: unexpected operator
Failed to install Node.js 6.9.5
=> Close and reopen your terminal to start using nvm or run the following to use it now:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
 ---> 9f6f3e74cd19
Removing intermediate container 79b517430285
Step 26/39 : RUN export NVM_DIR="$HOME/.nvm";
 ---> Running in 1d768138e3d5
 ---> 8039dfb4311c
Removing intermediate container 1d768138e3d5
Step 27/39 : RUN echo "[[ -s $HOME/.nvm/nvm.sh ]] && . $HOME/.nvm/nvm.sh" >> $HOME/.bashrc;
 ---> Running in d91126b7de62
 ---> 52313e09866e
Removing intermediate container d91126b7de62
Step 28/39 : RUN nvm ls-remote;
 ---> Running in f13c1ed42b3a
/bin/sh: 1: nvm: not found
The command '/bin/sh -c nvm ls-remote;' returned a non-zero code: 127

The error:

Step 28/39 : RUN nvm ls-remote;
 ---> Running in f13c1ed42b3a
/bin/sh: 1: nvm: not found
The command '/bin/sh -c nvm ls-remote;' returned a non-zero code: 127

The end of my /root/.bashrc file looks like:

[[ -s /root/.nvm/nvm.sh ]] && . /root/.nvm/nvm.sh

Everything else in the Dockerfile works. Adding the nvm stuff is what broke it. Here is the full file.

like image 361
CR47 Avatar asked Feb 06 '17 23:02

CR47


2 Answers

I made the following changes to your Dockerfile to make it work:

First, replace...

RUN sh /root/.nvm/install.sh;

...with:

RUN bash /root/.nvm/install.sh;

Why? On Redhat-based systems, /bin/sh is a symlink to /bin/bash. But on Ubuntu, /bin/sh is a symlink to /bin/dash. And this is what happens with dash:

root@52d54205a137:/# bash -c '[ 1 == 1 ] && echo yes!'
yes!
root@52d54205a137:/# dash -c '[ 1 == 1 ] && echo yes!'
dash: 1: [: 1: unexpected operator

Second, replace...

RUN nvm ls-remote;

...with:

RUN bash -i -c 'nvm ls-remote';

Why? Because, the default .bashrc for a user in Ubuntu (almost at the top) contains:

# If not running interactively, don't do anything
[ -z "$PS1" ] && return

And the source-ing of nvm's scripts takes place at the bottom. So we need to make sure that bash is invoked interactively by passing the argument -i.

Third, you could skip the following lines in your Dockerfile:

RUN export NVM_DIR="$HOME/.nvm";
RUN echo "[[ -s $HOME/.nvm/nvm.sh ]] && . $HOME/.nvm/nvm.sh" >> $HOME/.bashrc;

Why? Because bash /root/.nvm/install.sh; will automatically do it for you:

[fedora@myhost ~]$ sudo docker run --rm -it 2a283d6e2173 tail -2 /root/.bashrc
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
like image 70
Nehal J Wani Avatar answered Oct 07 '22 05:10

Nehal J Wani


Instalation of nvm on ubuntu in dockerfile

In the case of Ubuntu 20.04 you can use only these commands and everything will be alright

FROM ubuntu:20.04
RUN apt update -y &&  apt upgrade -y && apt install wget bash -y

RUN wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash

RUN bash -i -c 'nvm ls-remote'

hopefully it will work

like image 1
Muhammad Irfan Aslam Avatar answered Oct 07 '22 03:10

Muhammad Irfan Aslam