Installing npm canvas on elasticbeanstalk - npm install error

I am trying to deploy a node.js server running on EC2 via Elasticbeanstalk. The problem I am having is during the rebuild of the canvas npm module. The command 'canvas@1.1.6 install: node-gyp rebuild' fails as follows:

> canvas@1.1.6 install /tmp/deployment/application/node_modules/canvas
> node-gyp rebuild

gyp ERR! clean error 
gyp ERR! stack Error: EACCES, unlink 'build'
gyp ERR! System Linux 3.10.35-43.137.amzn1.x86_64
gyp ERR! command "node" "/opt/elasticbeanstalk/node-install/node-v0.10.26-linux-x64/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
gyp ERR! cwd /tmp/deployment/application/node_modules/canvas
gyp ERR! node -v v0.10.26
gyp ERR! node-gyp -v v0.12.2
gyp ERR! not ok 

npm ERR! canvas@1.1.6 install: `node-gyp rebuild`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the canvas@1.1.6 install script.
npm ERR! 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 their info via:
npm ERR!     npm owner ls canvas
npm ERR! There is likely additional logging output above.

The problem appears to be that the npm install canvas command is not being run with appropriate privileges. If I manually ssh into the server and run 'sudo npm install canvas', everything works fine. This tells me that all the appropriate dependencies are installed fine (I followed these instructions: https://github.com/Automattic/node-canvas/wiki/Installation---Amazon-Linux-AMI-(EC2)). It's just the elasticbeanstalk deployment which is causing the grief.

I attempted to enable root access during deployment by adding commands to a .config file inside .ebextensions as follows:

option_settings:
  - namespace: aws:elasticbeanstalk:container:nodejs:staticfiles
    option_name: /public
    value: /public
  - option_name: NODE_ENV
    value: production

commands:
  01_enable_rootaccess:
    command: echo Defaults:root \!requiretty >> /etc/sudoers
  02_no-cert:
    command: sudo /opt/elasticbeanstalk/node-install/node-v0.10.10-linux-x64/bin/npm config set ca ""

but it doesn't appear to make any difference.

What am I doing wrong, and how can I get canvas to install nicely during eb deployment?

Do you have all the dependencies for canvas? In order for it to work for me, I had to install the following: cairo, cairo-devel, libjpeg-devel, giflib-devel, libpng-devel (should already be installed).

You can ensure all of these are installed by adding the following in one of your .ebextensions:

packages:
    yum:
        cairo: []
        cairo-devel: []
        libjpeg-devel: []
        giflib-devel: []
        libpng-devel: []

I think canvas might be trying to install things it doesn't have which it doesn't have permissions for.

Try that out and let me know how it works.