DEV Community

Cover image for node-gyp support in alpine linux
Grigor Khachatryan
Grigor Khachatryan

Posted on

node-gyp support in alpine linux

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
Enter fullscreen mode Exit fullscreen mode

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 .
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

or

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

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!

Latest comments (9)

Collapse
 
ssianik profile image
Syed Sirajul Islam Anik
/app # apk add --no-cache --virtual .gyp python make g++
fetch https://dl-cdn.alpinelinux.org/alpine/v3.15/main/x86_64/APKINDEX.tar.gz
fetch https://dl-cdn.alpinelinux.org/alpine/v3.15/community/x86_64/APKINDEX.tar.gz
ERROR: unable to select packages:
  python (no such package):
    required by: .gyp-20220105.064855[python]
Enter fullscreen mode Exit fullscreen mode

If this is your error, I used python2 instead of python. Thus the command became
apk add --no-cache --virtual .gyp python2 make g++. Someone suggested to use python3. I'm a frontend guy, so you can just try his suggestion.

Collapse
 
bennycode profile image
Benny Code

When executing "RUN apk add --no-cache python make g++" then I am running into error "unable to select packages: python (no such package): required by: world[python]". I am using Docker image "node:16-alpine".

Collapse
 
magoacademico profile image
Mago Acadêmico

Have you found the solution?

Collapse
 
magoacademico profile image
Mago Acadêmico

I found the solution. If you want exactly python2, you need to use the alpine version 3.15. The latest versions only supports python3. So basically change "node:16-alpine" to "node:16-alpine3.15"

Collapse
 
ssianik profile image
Syed Sirajul Islam Anik

I used python2 as I saw it was explicitly looking for python2

Collapse
 
ikeapawel profile image
ikeapawel

Use python3 instead.

Collapse
 
kdalkafoukis profile image
Konstantinos Dalkafoukis

I had to install the npm package canvas and in order to be able to do it I had to change RUN apk add --no-cache --virtual .gyp python make g++ to RUN apk add --no-cache --virtual .gyp python make g++ pkgconfig pixman-dev cairo-dev pango-dev

Collapse
 
engineercoding profile image
Wesley Ameling

Seems like multistage docker images could be used for recompiling
docs.docker.com/develop/develop-im...

Collapse
 
yogeswaran79 profile image
Yogeswaran

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