Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add and use nvm in a DDEV web container?

Tags:

ddev

Currently, the DDEV web container does not come with nvm (node version manager). How can I add and use it via the DDEV config.yaml file?

like image 306
Michael Anello Avatar asked Oct 16 '19 14:10

Michael Anello


2 Answers

In current ddev v1.19+ nvm is installed by default, and can be used with ddev nvm, so you don't have to do any of this. See docs. So do ddev nvm install 12 for example. If you want to bake this into the config.yaml you might want to add something like this:

hooks:
  post-start:
  - exec: nvm install 12
  - exec: cd somepath && npm install

---- Original answer below ------

I recommend using the .ddev/web-build/Dockerfile approach, as it doesn't cost you every time you do a ddev start; it just builds one time in each project (and when you upgrade ddev).

Place this file in .ddev/web-build/Dockerfile:

ARG BASE_IMAGE
FROM $BASE_IMAGE

ENV NVM_DIR=/usr/local/nvm
ENV NODE_DEFAULT_VERSION=v6.10.1

RUN curl -sL https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh -o install_nvm.sh
RUN mkdir -p $NVM_DIR && bash install_nvm.sh
RUN echo "source $NVM_DIR/nvm.sh" >>/etc/profile
RUN bash -ic "nvm install $NODE_DEFAULT_VERSION && nvm use $NODE_DEFAULT_VERSION"
RUN chmod -R ugo+w $NVM_DIR

Change NODE_DEFAULT_VERSION to what you'd like it to be. You can add to this to use all the features of nvm; you could install more than one version, and use nvm use <otherversion> in a post-start hook if you wanted to.

For more about how to use and install nvm, see the README.

For more about how to use ddev's add-on Dockerfile capability, see ddev docs on add-on Dockerfile

For details about Dockerfile syntax, see Docker's Dockerfile reference

nvm is actually shell aliases, so it isn't available in the container to non-interactive commands by default. So it works fine inside ddev ssh but doesn't work out of the box in a post-start hook. To use it in a post-start hook you can do something like this:

hooks:
  post-start:
    - exec: 'bash -l -c "nvm install v12.15.0 && nvm use v12.15.0"'
like image 165
rfay Avatar answered Oct 07 '22 03:10

rfay


With the help of @greggles and @heddn on the #ddev Slack channel (on the Drupal Slack workspace), I got it working with the following post-start hook:

hooks:
 post-start:
   - exec: curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash
   - exec: rm -f ../.nvmrc && export NVM_DIR="$HOME/.nvm" && source "$NVM_DIR/nvm.sh" && nvm install 8.11.1 && nvm use 8.11.1

This installs nvm then sets node to version 8.11.1

-mike

like image 3
Michael Anello Avatar answered Oct 07 '22 04:10

Michael Anello