'Install libmysqlclient-dev along with npm in dockers

I have an error when trying to install the libmysqlclient-dev package together with npm for some reason when installing libmysqlclient-dev it removes npm

Step 10/26 : RUN apt-get install -y libmysqlclient-dev
 ---> Running in beae8aee9cd4
Reading package lists...
Building dependency tree...
Reading state information...
The following packages were automatically installed and are no longer required:
  gyp javascript-common libjs-async libjs-inherits libjs-jquery
  libjs-node-uuid libjs-underscore libuv1-dev node-abbrev node-ansi
  node-ansi-color-table node-archy node-async node-balanced-match
  node-block-stream node-brace-expansion node-builtin-modules
  node-combined-stream node-concat-map node-cookie-jar node-delayed-stream
  node-forever-agent node-form-data node-fs.realpath node-fstream
  node-fstream-ignore node-github-url-from-git node-glob node-graceful-fs
  node-hosted-git-info node-inflight node-inherits node-ini
  node-is-builtin-module node-isexe node-json-stringify-safe node-lockfile
  node-lru-cache node-mime node-minimatch node-mkdirp node-mute-stream
  node-node-uuid node-nopt node-normalize-package-data node-npmlog node-once
  node-osenv node-path-is-absolute node-pseudomap node-qs node-read
  node-read-package-json node-request node-retry node-rimraf node-semver
  node-sha node-slide node-spdx-correct node-spdx-expression-parse
  node-spdx-license-ids node-tar node-tunnel-agent node-underscore
  node-validate-npm-package-license node-which node-wrappy node-yallist
Use 'apt autoremove' to remove them.
The following additional packages will be installed:
  libssl-dev
Suggested packages:
  libssl-doc
The following packages will be REMOVED:
  libssl1.0-dev node-gyp nodejs-dev npm
The following NEW packages will be installed:
  libmysqlclient-dev libssl-dev
0 upgraded, 2 newly installed, 4 to remove and 133 not upgraded.
Need to get 2,583 kB of archives.
After this operation, 5,175 kB disk space will be freed.
Get:1 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 libssl-dev amd64 1.1.1-1ubuntu2.1~18.04.9 [1,566 kB]
Get:2 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 libmysqlclient-dev amd64 5.7.34-0ubuntu0.18.04.1 [1,016 kB]
dpkg-preconfigure: unable to re-open stdin: 
Fetched 2,583 kB in 2s (1,412 kB/s)
(Reading database ... 25174 files and directories currently installed.)
Removing npm (3.5.2-0ubuntu4) ...
Removing node-gyp (3.6.2-1ubuntu1) ...
Removing nodejs-dev (8.10.0~dfsg-2ubuntu0.4) ...
Removing libssl1.0-dev:amd64 (1.0.2n-1ubuntu5.6) ...

That gives me the following error:

Step 14/26 : RUN npm install -g [email protected]
 ---> Running in 18f70438b2ae
/bin/sh: 1: npm: not found
ERROR: Service 'webapp' failed to build: The command '/bin/sh -c npm install -g [email protected]' returned a non-zero code: 127

This is my Dockerfile:

RUN apt-get update
RUN apt-get install -y tzdata
RUN apt-get install -y libfontconfig1
RUN apt-get install -y libxrender1
RUN apt-get install -y nodejs
RUN apt-get install -y npm
RUN apt-get install -y yarn
RUN apt-get install -y mysql-client
RUN apt-get install -y libmysqlclient-dev
RUN apt-get install -y shared-mime-info
RUN apt-get clean
RUN rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

RUN npm install -g [email protected]
RUN npm install -g yarn

I am working on ruby on rails and I need the libmysqlclient-dev package for the mysql2 gem

How can i fix this?



Solution 1:[1]

You will want to read the Dockerfile best practices for the RUN instruction from the Docker docs. Each line in a Dockerfile is an image layer and the state after a RUN instruction is executed command is not always persisted on the next layer.

So the apt-get install -y npm won't affect the build when you run npm install -g ... so you received the error: npm command not found.

Please read the guide and attempt to use this single RUN instruction instead.

RUN apt-get update \
    && apt-get install -y tzdata \
    && apt-get install -y libfontconfig1 \
    && apt-get install -y libxrender1 \
    && apt-get install -y nodejs \
    && apt-get install -y npm \
    && apt-get install -y yarn \
    && apt-get install -y mysql-client \
    && apt-get install -y libmysqlclient-dev \
    && apt-get install -y shared-mime-info \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* \
    && npm install -g [email protected] \
    npm install -g yarn

Solution 2:[2]

using a new node that comes with npm helped for me:

  apt-get -qq update && \
  apt-get -qq --no-install-recommends install curl && \
  curl -sL https://deb.nodesource.com/setup_17.x | bash - && \
  apt-get -qq --no-install-recommends install \
    openssl \
    nodejs \
    linux-headers-generic \
    tzdata \
    libmysqlclient-dev

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 tentative
Solution 2 grosser