BackgroundThe image may or may not be related. If it's not related then I just want to share my shots with you! ehe

Enable HEIC Support in Sharp

September 29, 2025

Quoting from its official docs, Sharp is a Node.js API module "to convert large images in common formats to smaller, web-friendly JPEG, PNG, WebP, GIF and AVIF images of varying dimensions." If you've worked with or used web development, chances are you already have experience with this library. At the time of writing, it has been downloaded over 17 million times on NPM.

Under the hood, Sharp uses libvips, resulting in efficient processing and broad compatibility with most popular image encodings available. According to its GitHub repository, it supports JPEG, JPEG 2000, JPEG XL, TIFF, PNG, WebP, HEIC, and more. HEIC support is important for my use case, as it allows me to upload photos directly from my iPhone to this site and have them converted to WebP.

The problem is that while libvips supports HEIC, due to patent restrictions, the default libvips packages in major Linux repositories don't enable this support. You have to either find a PPA that includes it or build it from source. Being pragmatic, I chose to skip searching for PPAs and just build it from source. After working on this for two days, here's the final Dockerfile you can use

dockerfile
FROM node:lts-alpine RUN apk update RUN apk add \ libheif \ libheif-dev \ libpng-dev \ libjpeg-turbo-dev \ libde265-dev \ libwebp-dev \ build-base \ python3 \ pkgconfig \ wget \ xz \ meson \ ninja \ glib-dev \ expat-dev # Build libvips from source for version 8.17.2 RUN wget https://github.com/libvips/libvips/releases/download/v8.17.2/vips-8.17.2.tar.xz \ && tar xf vips-8.17.2.tar.xz \ && cd vips-8.17.2 \ # && meson setup build --prefix /usr/local -Dheif=enabled \ && meson setup build --prefix /usr/local \ && cd build \ && meson compile \ && meson test \ && meson install \ && cd ../.. \ && rm -rf vips-8.17.2 vips-8.17.2.tar.xz RUN npm cache clean --force # Install Sharp globally with build-from-source for HEIC support RUN npm install -g node-addon-api node-gyp RUN npm install -g --no-package-lock --build-from-source sharp # Cleanup build dependencies to reduce image size RUN apk del build-base