M1 Mac で Docker のビルドエラー fatal error: vips/vips8 への対処方法

DockermacOS

M1 Mac ( Apple Silicon )で Node.js を使う Docker イメージのビルドで次のようなエラーが出るときの対処方法についてです。

サンプル 1:

../src/common.cc:24:22: fatal error: vips/vips8: そのようなファイルやディレクトリはありません
 #include <vips/vips8>
                      ^
compilation terminated.
sharp.target.mk:137: ターゲット 'Release/obj.target/sharp/src/common.o' のレシピで失敗しました
make: ディレクトリ '/node_modules/sharp/build' から出ます
make: *** [Release/obj.target/sharp/src/common.o] エラー 1

サンプル 2:

npm verb stack Error: command failed
npm verb stack     at ChildProcess.<anonymous> (/usr/local/lib/node_modules/npm/node_modules/@npmcli/promise-spawn/index.js:64:27)
npm verb stack     at ChildProcess.emit (node:events:390:28)
npm verb stack     at maybeClose (node:internal/child_process:1064:16)
npm verb stack     at Process.ChildProcess._handle.onexit (node:internal/child_process:301:5)
npm verb pkgid sharp@0.29.3
npm verb cwd /app
npm verb Linux 5.10.76-linuxkit
npm verb argv "/usr/local/bin/node" "/usr/local/bin/npm" "install" "--verbose"
npm verb node v16.13.1
npm verb npm  v8.2.0
npm ERR! code 1
npm ERR! path /app/node_modules/sharp
npm ERR! command failed
npm ERR! command sh -c (node install/libvips && node install/dll-copy && prebuild-install) || (node install/can-compile && node-gyp rebuild && node install/dll-copy)
npm ERR! sharp: Please see https://sharp.pixelplumbing.com/install for required dependencies
npm ERR! sharp: Installation error: Use with glibc 2.28 requires manual installation of libvips >= 8.11.3
npm verb exit 1

私は Node.js の NPM パッケージ sharp を使うプロジェクトでこのエラーに遭遇しました。 具体的にはベースイメージとして node:16node:14 を使用した場合にこの問題が発生しました。 記事執筆時点で node:16buster を、 node:14stretch をベースとしています。

docker run --rm node:16 cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 10 (buster)"
NAME="Debian GNU/Linux"
VERSION_ID="10"
VERSION="10 (buster)"
VERSION_CODENAME=buster
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"
docker run --rm node:14 cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 9 (stretch)"
NAME="Debian GNU/Linux"
VERSION_ID="9"
VERSION="9 (stretch)"
VERSION_CODENAME=stretch
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"

ちなみに、ホスト側の macOS の環境は次のとおりです:

❯ sw_vers
ProductName:    macOS
ProductVersion: 12.1
BuildVersion:   21C52

ウェブ上で検索するといくつかの解決策が提案されています。 私が目にした範囲では次の 3 つの方法が有効なようでした。

  • A) bullseye ベースのイメージを使う
  • B) イメージのプラットフォームを変更する
  • C) vipslibvips )をインストールする

A) bullseye ベースのイメージを使う

記事執筆時点では bullseye ベースのイメージだと問題ありませんでした。

FROM node:16-bullseye
FROM node:14-bullseye

参考: Cannot install sharp on M1 Mac with Debian Stretch based Docker container · Issue #2875 · lovell/sharp · GitHub

B) イメージのプラットフォームを変更する

FROM のオプション --platform でプラットフォームを変更します。

FROM --platform=linux/amd64 node:16
FROM --platform=linux/amd64 node:14

C) vipslibvips )をインストールする

Homebrew が利用できる場合は次のコマンドで vips をインストールできます。

RUN brew reinstall vips

apt-get が利用できる場合は次のコマンドが libvips をインストールできます。

RUN apt-get update -y && \
  apt-get upgrade -y libvips libvips-dev

参考: Can't compile under Apple Silicon M1 arm64 · Issue #2460 · lovell/sharp · GitHub

これらの中から状況に合った方法を選ぶとよいかと思います。 私は A) の方法を選びました。

参考


アバター
後藤隼人 ( ごとうはやと )

ソフトウェア開発やマーケティング支援などをしています。詳しくはこちら