This error occurs when installing node-canvas without the required system dependencies. Install Cairo and related graphics libraries for your Linux distribution to fix it.
Fixes npm ERR! [email protected] install: pkg-config: command not found
sudo apt-get update
sudo apt-get install -y \
build-essential \
libcairo2-dev \
libpango1.0-dev \
libjpeg-dev \
libgif-dev \
librsvg2-dev \
pkg-configsudo apt-get updatesudo apt-get install -y \build-essential \libcairo2-dev \libpango1.0-dev \libjpeg-dev \libgif-dev \librsvg2-dev \pkg-confignpm ERR! [email protected] install: pkg-config: command not found
The node-canvas package is a Cairo-backed Canvas implementation for Node.js. It requires several native graphics libraries to be installed on your system before npm can build it. pkg-config is a helper tool that helps the build process find installed libraries. When it's missing, the compilation can't locate the required dependencies. This error commonly occurs on minimal server installations, Docker containers, or CI environments that don't include development libraries by default.
Install all required packages:
sudo apt-get update
sudo apt-get install -y \
build-essential \
libcairo2-dev \
libpango1.0-dev \
libjpeg-dev \
libgif-dev \
librsvg2-dev \
pkg-configFor Red Hat-based systems:
sudo yum install -y \
gcc-c++ \
cairo-devel \
pango-devel \
libjpeg-turbo-devel \
giflib-devel \
librsvg2-devel \
pkgconfigOn Fedora, use dnf instead of yum.
For Alpine (common in Docker):
apk add --no-cache \
build-base \
g++ \
cairo-dev \
pango-dev \
jpeg-dev \
giflib-dev \
librsvg-dev \
pkgconfigAfter installing dependencies, install canvas:
npm install canvasOr rebuild if already in package.json:
npm rebuild canvasFor Docker, add dependencies in your Dockerfile:
FROM node:20
# Install canvas dependencies
RUN apt-get update && apt-get install -y \
libcairo2-dev \
libpango1.0-dev \
libjpeg-dev \
libgif-dev \
librsvg2-dev \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /app
COPY package*.json ./
RUN npm ciFor multi-stage builds, remember that build dependencies are only needed in the build stage. The runtime stage needs the non-dev packages:
# Runtime stage only needs:
RUN apt-get update && apt-get install -y \
libcairo2 libpango-1.0-0 libjpeg62-turbo libgif7 librsvg2-2Consider using the canvas package's prebuilt binaries where available by checking their platform support documentation.