Backward
Yuanyi

Using NightmareJS/Electron on Amazon Linux

Sharing
We recently changed from PhantomJS to NightmareJS to improve quality of PNG files generated on MockingBot, and here's how we get NightmareJS/Electron run on Amazon Linux.

0. Install nightmare

$ npm install --save nightmare

1. Check dependencies

As Amazon Linux don't provide desktop environment, we need to resolve the dependencies of prebuilt electron manually.

$ cd node_modules/nightmare/node_modules/electron-prebuilt/dist
$ ldd electron | grep ‘not found'
    libgtk-x11-2.0.so.0 => not found
    libgdk-x11-2.0.so.0 => not found
    libatk-1.0.so.0 => not found
    libpangocairo-1.0.so.0 => not found
    libpango-1.0.so.0 => not found
    libgdk_pixbuf-2.0.so.0 => not found
    libXrandr.so.2 => not found
    libgconf-2.so.4 => not found
    libXcursor.so.1 => not found
    libXcomposite.so.1 => not found
    libgdk_pixbuf-2.0.so.0 => not found

Use ldd, we can see there're 11 libraries we miss. Pango, Xrandr, Xcursor and Xcomposite are included in Amazon Linux, let's install those with yum:

$ sudo yum install pango pango-devel libXrandr libXrandr-devel libXcursor libXcursor-devel libXcomposite libXcomposite-devel

and we can find atk from CentOS 6:

$ sudo rpm -ivh http://mirror.centos.org/centos/6/os/x86_64/Packages/atk-1.30.0-1.el6.x86_64.rpm
$ sudo rpm -ivh http://mirror.centos.org/centos/6/os/x86_64/Packages/atk-devel-1.30.0-1.el6.x86_64.rpm

Now comes the hard part.

2. GConf

GConf is depended by GTK+, so we need to compile it first:

# Install GConf dependencies
$ sudo yum install libIDL libIDL-devel gtk-doc dbus dbus-devel dbus-glib dbus-glib-devel intltool indent
$ sudo rpm -ivh http://mirror.centos.org/centos/6/os/x86_64/Packages/ORBit2-2.14.17-5.el6.x86_64.rpm
$ sudo rpm -ivh http://mirror.centos.org/centos/6/os/x86_64/Packages/ORBit2-devel-2.14.17-5.el6.x86_64.rpm

# Compile GConf
$ cd /tmp
$ wget ftp://ftp.gnome.org/pub/GNOME/sources/GConf/2.32/GConf-2.32.4.tar.bz2
$ tar -jxvf GConf-2.32.4.tar.bz2
$ cd GConf-2.32.4
$ ./configure && make
$ sudo make install

3. GTK+

It took me some time to find a capable version of GTK+, and finally I found 2.24.0 work well will current release of Amazon Linux.

# Compile gdk-pixbuf
$ cd /tmp
$ wget http://ftp.acc.umu.se/pub/gnome/sources/gdk-pixbuf/2.24/gdk-pixbuf-2.24.0.tar.bz2
$ tar -jxvf gdk-pixbuf-2.24.0.tar.bz2
$ cd gdk-pixbuf-2.24.0
$ ./configure
$ make
$ sudo make install

# Compile GTK+
$ wget http://ftp.gnome.org/pub/gnome/sources/gtk+/2.24/gtk+-2.24.0.tar.bz2
$ tar -jxvf gtk+-2.24.0.tar.bz2
$ cd gtk+-2.24.0
$ PKG_CONFIG_PATH=/usr/local/lib/pkgconfig ./configure
$ make
$ sudo make install

4. Linking

Now we've got all libraries we needed, let's link them to prebuilt electron:

$ cd ~/node_modules/electron-prebuilt/dist
$ ln -PL /usr/local/lib/libgconf-2.so.4
$ ln -PL /usr/local/lib/libgtk-x11-2.0.so.0
$ ln -PL /usr/local/lib/libgdk-x11-2.0.so.0
$ ln -PL /usr/local/lib/libgdk_pixbuf-2.0.so.0

Now run ldd again, and you should get empty output:

$ ldd electron | grep 'not found'

5. Run

To run Electron on Amazon Linux, here's the last step we needed: setup X.

# Install X
$ sudo yum install xorg-x11-server-Xorg xterm
# Install X drivers
$ sudo yum install xorg-x11-drv-vesa xorg-x11-drv-evdev xorg-x11-drv-evdev-devel
# Install xvfb
$ sudo yum install Xvfb

Okay, finally, let's check our work with an example from NightmareJS homepage:

$ xvfb-run -a --server-args="-screen 0 1024x768x24" node cnn.js
Breaking News, Daily News and Videos - CNN.com

# cnn.js
var Nightmare = require('nightmare');
var vo = require('vo');

vo(run)(function(err, result) {
  if (err) throw err;
});

function *run() {
  var nightmare = Nightmare();
  var title = yield nightmare
    .goto('http://cnn.com')
    .evaluate(function() {
      return document.title;
    });
  console.log(title);
  yield nightmare.end();
}

Cool, it works!
Comments 1