Solvednode canvas npm ERR! Failed at the canvas@1.4.0 install script 'node-gyp rebuild'

I am trying to deploy my app to digitalocean with dokku. Even if I have ssh'ed to digitalocean machine and ran a command to install all dependencies as is stated in wiki (sudo apt-get install libcairo2-dev libjpeg8-dev libpango1.0-dev libgif-dev build-essential g++), I still receive the following error:

-----> Building dependencies
       Pruning any extraneous modules
       Installing node modules (package.json)

       > canvas@1.4.0 install /tmp/build/node_modules/canvas
       > node-gyp rebuild

       make: Entering directory `/tmp/build/node_modules/canvas/build'
       SOLINK_MODULE(target) Release/obj.target/canvas-postbuild.node
       COPY Release/canvas-postbuild.node
       CXX(target) Release/obj.target/canvas/src/Canvas.o
       CXX(target) Release/obj.target/canvas/src/CanvasGradient.o
       CXX(target) Release/obj.target/canvas/src/CanvasPattern.o
       In file included from ../src/CanvasPattern.cc:9:0:
       ../src/Image.h:19:21: fatal error: gif_lib.h: No such file or directory
       #include <gif_lib.h>
       ^
       compilation terminated.
       make: *** [Release/obj.target/canvas/src/CanvasPattern.o] Error 1
       make: Leaving directory `/tmp/build/node_modules/canvas/build'
       gyp ERR! build error
       gyp ERR! stack Error: `make` failed with exit code: 2
       gyp ERR! stack     at ChildProcess.onExit (/tmp/build/.heroku/node/lib/node_modules/npm/node_modules/node-gyp/lib/build.js:276:23)
       gyp ERR! stack     at emitTwo (events.js:87:13)
       gyp ERR! stack     at ChildProcess.emit (events.js:172:7)
       gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:200:12)
       gyp ERR! System Linux 3.13.0-85-generic
       gyp ERR! command "/tmp/build/.heroku/node/bin/node" "/tmp/build/.heroku/node/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
       gyp ERR! cwd /tmp/build/node_modules/canvas
       gyp ERR! node -v v4.4.5
       gyp ERR! node-gyp -v v3.3.1
       gyp ERR! not ok

Specs: Ubuntu 14.04, dokku 0.6.3, node 4.4.5, npm 2.15.5

29 Answers

✔️Accepted Answer

For OSX I had to do this:

xcode-select --install # I thought this was dumb, but was key to one of my issues 
npm install node-gyp -g
brew install giflib cairo libjpeg giflib pixman
export PKG_CONFIG_PATH=/opt/X11/lib/pkgconfig
OTHER_CFLAGS=-I/usr/local/include npm install canvas

Other Answers:

Simular issue using AWS Red Hat 7 (Centos)...

root@presenter-app-1a-247-1 npm]# sudo npm install canvas

> canvas@1.4.0 install /srv/www/presenter_app/releases/20160823093039/bundle/programs/server/npm/node_modules/canvas
> node-gyp rebuild

Package cairo was not found in the pkg-config search path.
Perhaps you should add the directory containing `cairo.pc'
to the PKG_CONFIG_PATH environment variable
No package 'cairo' found
gyp: Call to './util/has_lib.sh freetype' returned exit status 0 while in binding.gyp. while trying to load binding.gyp
gyp ERR! configure error 
gyp ERR! stack Error: `gyp` failed with exit code: 1
gyp ERR! stack     at ChildProcess.onCpExit (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/configure.js:305:16)
gyp ERR! stack     at emitTwo (events.js:106:13)
gyp ERR! stack     at ChildProcess.emit (events.js:191:7)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:204:12)
gyp ERR! System Linux 4.4.14-24.50.amzn1.x86_64
gyp ERR! command "/usr/local/bin/node" "/usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
gyp ERR! cwd /srv/www/presenter_app/releases/20160823093039/bundle/programs/server/npm/node_modules/canvas
gyp ERR! node -v v6.4.0
gyp ERR! node-gyp -v v3.3.1
gyp ERR! not ok 
npm WARN enoent ENOENT: no such file or directory, open '/srv/www/presenter_app/releases/20160823093039/bundle/programs/server/npm/package.json'
npm WARN enoent ENOENT: no such file or directory, open '/srv/www/presenter_app/releases/20160823093039/bundle/programs/server/npm/node_modules/meteor/package.json'
npm WARN npm No description
npm WARN npm No repository field.
npm WARN npm No README data
npm WARN npm No license field.
npm ERR! Linux 4.4.14-24.50.amzn1.x86_64
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "install" "canvas"
npm ERR! node v6.4.0
npm ERR! npm  v3.10.3
npm ERR! code ELIFECYCLE

npm ERR! canvas@1.4.0 install: `node-gyp rebuild`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the canvas@1.4.0 install script 'node-gyp rebuild'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the canvas package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     node-gyp rebuild
npm ERR! You can get information on how to open an issue for this project with:
npm ERR!     npm bugs canvas
npm ERR! Or if that isn't available, you can get their info via:
npm ERR!     npm owner ls canvas
npm ERR! There is likely additional logging output above.

Update

Installed dependencies to fix..

sudo yum install cairo cairo-devel cairomm-devel libjpeg-turbo-devel pango pango-devel pangomm pangomm-devel giflib-devel -y

Have you tried install the dependancies located on the packages NPM page https://www.npmjs.com/package/canvas?

retry running:
sudo apt-get install libgif-dev
should do it

What libraries have you tried installing on Ubuntu/Xubuntu? Have you tried sudo apt-get install libcairo2-dev libjpeg8-dev libpango1.0-dev libgif-dev build-essential g++?