Cover image for node-gyp support in alpine linux

node-gyp support in alpine linux

grigorkh profile image Grigor Khachatryan ・2 min read

Originally published on Medium

When using alpine, you need to install build dependencies for some node module to be able to be built natively.
Here is an example of how you would install dependencies for packages that require node-gyp support on the alpine variant:

FROM node:alpine

RUN apk add --no-cache --virtual .gyp python make g++ \
    && npm install [ your npm dependencies here ] \
    && apk del .gyp

And Here’s a multistage build example:

FROM node:alpine as builder

## Install build toolchain, install node deps and compile native add-ons
RUN apk add --no-cache python make g++
RUN npm install [ your npm dependencies here ]

FROM node:alpine as app

## Copy built node modules and binaries without including the toolchain
COPY --from=builder node_modules .

Pro Tip:

As Alpine Linux uses musl, you may run into some issues with environments expecting glibc-like behavior — especially if you try to use binaries compiled with glibc. You should recompile these binaries to use musl (compiling on Alpine is probably the easiest way to do this).
If you get an error similar to error loading shared library ld-linux-x86-64.so.2, it may be that you have dependencies relying on libc – you can try to fix this by adding:

RUN apk add --no-cache libc6-compat


RUN ln -s /lib/libc.musl-x86_64.so.1 /lib/ld-linux-x86-64.so.2

to your Dockerfile.

Like to learn?

Follow me on twitter where I post all about the latest and greatest JavaScript, AI, DevOps, VR/AR, Technology, and Science! Connect with me on LinkedIn too!

Posted on by:

grigorkh profile

Grigor Khachatryan


Chief Architect at Mappr, Vectuel and RodinVR (#Techstars'17), #VR/#AR enginner, open source enthusiast. Thoughts on #IoT, #BigData, #DataScience, #AI & #Docker.Conducted 2500 job interviews.


markdown guide

Seems like multistage docker images could be used for recompiling


Hey there! I shared your article here t.me/theprogrammersclub and check out the group if you haven't already!