Headless Firefox on Amazon Linux 2015.xx

Amazon Linux does not offer a graphical user interface so in order to run Firefox we need to compile the gimp tool kit and install Firefox from the mozilla provided source. Joe has been kind enough to provide a shell script that resolves dependencies and installs Firefox.

The steps outlined below presume that you have some familiarity with unix like systems, know how to launch an instance, and have the appropriate group permissions and private keys in order to access the machine.

Start out by launching a t1 micro instance of the Amazon Linux Machine Image. I find it best to tinker with the smallest t1 tier and scale up to larger instances when demand warrants. After the instance is up and running, ssh into the box.

From inside the machine instance update Amazon’s base image with the latest packages by running

$ sudo yum update

Next, run

$ wget https://gist.githubusercontent.com/joekiller/4144838/raw/ffd5fabce7083eca65c61033ea90e51b4be6ba55/gtk-firefox.sh

to get that awesome install script.

Modify the script to be executable with $ chmod 755 gtk-firefox.sh and then run the script

$ ./gtk-firefox.sh

On a t1 micro instance this process takes anywhere from 15 to 20 minutes.

Now we have Firefox but nowhere for it to run. To remedy this we want to install Xvfb

X virtual framebuffer is a display server implementing the X11 display server protocol. Xvfb performs all graphical operations in memory without showing any screen output.

Do that by running $ sudo yum install xvfb

Next we need to set up the machine so that when it starts it also starts the X11 server so that Firefox has a place to live.

Add the following lines to /etc/rc.local:

export DISPLAY=:10
Xvfb :10 -ac &
firefox &

The & at the end of the last two commands tells the system to run those processes in the background.

Now install the selenium web browser automation tool so that we can take this puppy for a test drive. Install this by running

$ easy_install selenium

Now that we are done setting up our box, let’s clean up our mess and and make a copy of the machine we just created.

Clean up the artifacts in the ec2-user’s home directory. This step is optional. After that, delete the private key info from the /home/ec2-user/.ssh/authorized_user directory. Next, create the file /firstboot so that the instance knows to download SSH public keys for new logins. Then, stop the instance but do not terminate it or you will lose all of that hard work. After the instance stops use the ec2-create-image tool or the “Create Image” command in the EC2 Management Console to create an AMI from the stopped instance. When that finishes you can terminate the instance.

Let’s take the newly created image for a test drive. Launch the new instance and login. The script below is a sample selenium test script sourced from the selenium python documentation. Create a file called test.py and paste in the contents of the script.

from selenium import webdriver

driver = webdriver.Firefox()
print driver.title
assert "Python" in driver.title

when you run the script - $ python test.py - you should see the word Python and then True in the output. If you can see that then you are good to go.

At the end of this you should have an Amazon Linux 2015.xx machine image configured with the selenium browser automation suite and the latest Firefox. If you have any questions or run into any problems you can reach me by using the form below or on twitter. Happy hacking.