How to install phantomjs on RHEL 8 / CentOS 8

Phantomjs is a scriptable, headless browser. It can be essential help in automated testing of web services, it can generate screenshots of the page it is visiting, effectively print the traffic while browsing a page, just to name a few of it’s features. Phantomjs can be scripted in JavaScript, so we don’t need to learn yet another language is we are familiar with it. Sadly, it’s development is suspended at the moment, but the last stable release is available for download.

In this tutorial we will install phantomjs on RHEL 8 / CentOS 8, and run one of the examples shipped with the package to see our tool working.

In this tutorial you will learn:

  • How to download phantomjs package
  • How to extract and place the binary on the path
  • How to run an example script with phantomjs

Running an example script with phantomjs.

Running an example script with phantomjs.

Software Requirements and Conventions Used

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System RHEL 8 / CentOS 8
Software Phantomjs 2.1.1
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

How to install phantomjs on RHEL 8 / CentOS 8 step by step instructions

Phantomjs is really self-contained. If we have the few packages it requires, we wouldn’t need root access to install the tool for ourselves (that is, accessible to our user only). To ensure every user of the system can use this tool, we’ll place it on a system-wide path, and this requires a privileged user.

  1. First we should check we have the packages required:
    $ rpm -q glibc
    glibc-2.28-18.el8.x86_64
    $ rpm -q fontconfig
    fontconfig-2.13.1-2.el8.x86_64

    If not, we can install the above packages with dnf:

    # dnf install glibc fontconfig
  2. We can visit the official download site to find the URL we need. In our case it is address of the package for Linux x86_64. We enter the /opt directory, where we will store the binary, examples and readmes:
    # cd /opt

    And download the package with wget:

    # wget https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-linux-x86_64.tar.bz2


  3. Next we extract the archive:
    # tar -xvf phantomjs-2.1.1-linux-x86_64.tar.bz2
  4. A common place for user-installed (that is, not originating from the distribution’s repositories) binaries and tools is /usr/local/bin. On RHEL it is also on everyone’s $PATH by default. We create a symlink into this directory, and by doing so any old or new user will find it with simple bash autocompletion:
    # ln -s /opt/phantomjs-2.1.1-linux-x86_64/bin/phantomjs /usr/local/bin/phantomjs
  5. To test we have phantomjs on our path, we can type the command without any path as any user of the system:
    $ phantomjs --version
    2.1.1
  6. While the binary itself is now on our path, the examples shipped with the distributed package are not. We can try our tool by running these examples, which can give us a start on writing our own scripts for phantomjs. The below example, netlog.js will print all the traffic generated while phantomjs visits a website, including header information and binary data. In our case we run it against the famous info.php (php‘s phpinfo(); called) served by a webserver running locally.
    $ phantomjs /opt/phantomjs-2.1.1-linux-x86_64/examples/netlog.js http://localhost/info.php

    We are provided with all the requested and received data, so a simple call as this will produce a lengthy output.

    requested: {
        "headers": [
            {
                "name": "Accept",
                "value": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
            },
            {
                "name": "User-Agent",
                "value": "Mozilla/5.0 (Unknown; Linux x86_64) AppleWebKit/538.1 (KHTML, like Gecko) PhantomJS/2.1.1 Safari/538.1"
            }
        ],
        "id": 1,
        "method": "GET",
        "time": "2019-01-15T17:48:57.393Z",
        "url": "http://localhost/info.php"
    }
    received: {
        "body": "",
        "bodySize": 60718,
        "contentType": "text/html; charset=UTF-8",
        "headers": [
            {
                "name": "Date",
                "value": "Tue, 15 Jan 2019 17:48:57 GMT"
            },
            {
                "name": "Server",
                "value": "Apache/2.4.35 (Red Hat Enterprise Linux) mod_perl/2.0.10 Perl/v5.26.2"
            },
            {
                "name": "X-Powered-By",
                "value": "PHP/7.2.11"
            },
            {
                "name": "Keep-Alive",
                "value": "timeout=5, max=100"
            },
          [...]