Create a gist now

Instantly share code, notes, and snippets.

How to install PhantomJS on Debian/Ubuntu

How to install PhantomJS on Ubuntu

Version: 1.9.8

Platform: x86_64

First, install or update to the latest system software.

sudo apt-get update
sudo apt-get install build-essential chrpath libssl-dev libxft-dev

Install these packages needed by PhantomJS to work correctly.

sudo apt-get install libfreetype6 libfreetype6-dev
sudo apt-get install libfontconfig1 libfontconfig1-dev

Get it from the PhantomJS website.

cd ~
export PHANTOM_JS="phantomjs-1.9.8-linux-x86_64"
wget https://bitbucket.org/ariya/phantomjs/downloads/$PHANTOM_JS.tar.bz2
sudo tar xvjf $PHANTOM_JS.tar.bz2

Once downloaded, move Phantomjs folder to /usr/local/share/ and create a symlink:

sudo mv $PHANTOM_JS /usr/local/share
sudo ln -sf /usr/local/share/$PHANTOM_JS/bin/phantomjs /usr/local/bin

Now, It should have PhantomJS properly on your system.

phantomjs --version
#!/usr/bin/env bash
# This script install PhantomJS in your Debian/Ubuntu System
#
# This script must be run as root:
# sudo sh install_phantomjs.sh
#
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root" 1>&2
exit 1
fi
PHANTOM_VERSION="phantomjs-1.9.8"
ARCH=$(uname -m)
if ! [ $ARCH = "x86_64" ]; then
$ARCH="i686"
fi
PHANTOM_JS="$PHANTOM_VERSION-linux-$ARCH"
sudo apt-get update
sudo apt-get install build-essential chrpath libssl-dev libxft-dev -y
sudo apt-get install libfreetype6 libfreetype6-dev -y
sudo apt-get install libfontconfig1 libfontconfig1-dev -y
cd ~
wget https://bitbucket.org/ariya/phantomjs/downloads/$PHANTOM_JS.tar.bz2
sudo tar xvjf $PHANTOM_JS.tar.bz2
sudo mv $PHANTOM_JS /usr/local/share
sudo ln -sf /usr/local/share/$PHANTOM_JS/bin/phantomjs /usr/local/bin
@SmurfyFR
SmurfyFR commented Sep 2, 2014

There is a mistake :

sudo mv $PHANTOM_JS.tar.bz2 /usr/local/share/
cd /usr/local/share/
sudo ln -sf /usr/local/share/$PHANTOM_JS/bin/phantomjs /usr/local/share/phantomjs
sudo ln -sf /usr/local/share/$PHANTOM_JS/bin/phantomjs /usr/local/bin/phantomjs
sudo ln -sf /usr/local/share/$PHANTOM_JS/bin/phantomjs /usr/bin/phantomjs

should be :

sudo mv $PHANTOM_JS.tar.bz2 /usr/local/share/
cd /usr/local/share/
sudo tar xvjf phantomjs-1.9.2-linux-x86_64.tar.bz2
sudo ln -sf /usr/local/share/$PHANTOM_JS/bin/phantomjs /usr/local/share/phantomjs
sudo ln -sf /usr/local/share/$PHANTOM_JS/bin/phantomjs /usr/local/bin/phantomjs
sudo ln -sf /usr/local/share/$PHANTOM_JS/bin/phantomjs /usr/bin/phantomjs
@julionc
Owner
julionc commented Sep 30, 2014

good catch!
I updated the gist. Thank you guys!

@karol-blaszczyk

Thank you, that was i was looking for !

@SizeSeeker

Great gist, thank you!

@shamabbas

Great! Thank you!

@iobee
iobee commented Nov 19, 2014

Great! Thank you!

@lucassch

Great!

@reminate
reminate commented Dec 9, 2014

Thank you!

@nikulkarni

exactly what I was looking for, thank you.

@georgehenze

Thanks a lot!

@idmontie

๐Ÿ‘
Thanks for this write up. Note that Phantom is now on 1.9.8 though.

@aug-riedinger

Why triple symbolic link? Just in case?

What about sudo apt-get install phantomjs? It did not work for me, but any idea why?

@Yakito
Yakito commented Feb 3, 2015

Tested on an EC2 with Ubuntu 14.04, Works fine.

@lethalbrains

Thanx

@tbuchboeck

JFYI:
We had some font issues recently (Debian GNU/Linux 7.8 (wheezy) pjs-1.9.7)
Installing sudo apt-get install msttcorefonts solved the problem.

@q0rban
q0rban commented Feb 5, 2015

Thanks for this! You shouldn't need to create that many symlinks, or export your $PHANTOM_JS variable. I would just symlink to /usr/local/bin.

@q0rban
q0rban commented Feb 5, 2015

Here's a shorter version of the above:

sudo aptitude update
sudo aptitude install build-essential chrpath libssl-dev libxft-dev \
  libfreetype6 libfreetype6-dev libfontconfig1 libfontconfig1-dev
PHANTOM_JS="phantomjs-1.9.8-linux-x86_64"
cd ~
wget https://bitbucket.org/ariya/phantomjs/downloads/$PHANTOM_JS.tar.bz2
tar -xvjf $PHANTOM_JS.tar.bz2
sudo mv $PHANTOM_JS /usr/local/share
sudo ln -s /usr/local/share/$PHANTOM_JS/bin/phantomjs /usr/local/bin
phantomjs --version
@tunnckoCore

After all

bash: /usr/local/bin/phantomjs: cannot execute binary file: Exec format error

Ubuntu 14.04

@tunnckoCore

Its okey. It was the arch :)

@ghost
ghost commented Feb 13, 2015

q0rban: worked perfectly, thanks!

@pankajclabs

phantomjs --version

getting this:

Fontconfig warning: ignoring UTF-8: not a valid region tag
1.9.8

please help me out, I really tried everything

@mgzrobles

ubuntu
phantomjs --version
-bash: /usr/local/bin/phantomjs: cannot execute binary file

@jasseral

same !! :(
ubuntu
phantomjs --version
-bash: /usr/local/bin/phantomjs: cannot execute binary file

@jasseral

HELP !

@azenkovets

Have the same issue:
phantomjs --version
-bash: /usr/local/bin/phantomjs: cannot execute binary file

@TammoHeeren

you may need to download the i686 version
PHANTOM_JS="phantomjs-1.9.8-linux-i686"

@prakash89

Ubuntu
phantomjs --version
-bash: /usr/local/bin/phantomjs: cannot execute binary file

@prakash89

Thanks,
This works for me
PHANTOM_JS="phantomjs-1.9.8-linux-i686"

@julionc
Owner
julionc commented Mar 20, 2015

I update this gist to v1.9.8.
For lazy ones like us. I attached install_phantomjs.sh script. Have fun!

@Rolos
Rolos commented Mar 29, 2015

Works perfectly, many thanks!

@id-den
id-den commented Apr 9, 2015

Thanks!

@donaldali

Awesome

@dominiquedutra

Great stuff, thanks.

@ericallard0

Thanks !

@nhattan
nhattan commented Apr 21, 2015

Thanks!

@IvRRimum

install_phantomjs.sh - Works like charm, Thanks!

@xcaliburs

Thank you. Works fine, tested on EC2 with Ubuntu 14.04.

@vdloo
vdloo commented May 6, 2015

thanks!

@kumarldh

Or you can build it on your machine as suggested on the site and then follow instructions from this page where the author starts moving the folder.

@shashank-saxena

Thanks a ton!!!

@Plazik
Plazik commented May 29, 2015

Thank you!

For fonts issue I have to install ttf-mscorefonts-installer too.

@carltondickson

Amazingly simple <3

@FurkanArslan

Thank you very much man! โ˜บ๏ธ

@ftcosta
ftcosta commented Jun 17, 2015

for Ubuntu14.04 and Mint

cd /usr/local/share
sudo wget https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-1.9.7-linux-x86_64.tar.bz2
sudo tar xjf phantomjs-1.9.7-linux-x86_64.tar.bz2
sudo ln -s /usr/local/share/phantomjs-1.9.7-linux-x86_64/bin/phantomjs /usr/local/share/phantomjs
sudo ln -s /usr/local/share/phantomjs-1.9.7-linux-x86_64/bin/phantomjs /usr/local/bin/phantomjs
## system wide
sudo ln -s /usr/local/share/phantomjs-1.9.7-linux-x86_64/bin/phantomjs /usr/bin/phantomjs

phantomjs -v

v//carlosdeoliveira.guru

change the version from 1.9.7 to 1.9.8

@alaabatayneh

Thanks ftcosta

@rbyelyy
rbyelyy commented Jun 26, 2015

Thanks. Works like a sharm ;-)

@n37r06u3

npm install phantomjs -g

@hebbian
hebbian commented Jul 23, 2015

pankajclabs, I know that feel buddy, when no one helps out

@rga-odoo

Thank you ๐Ÿ‘

@kafeltz
kafeltz commented Jul 28, 2015

The solution is so complex that I prefer using php -S localhost:80 for simple tests.

@lalitb
lalitb commented Aug 7, 2015

For all those who are getting error "Cannot execute binary file" , please note these instructions are only for 64 bit architecture. For 32 bit, you may have to build phantomjs from source.

@nroose
nroose commented Oct 27, 2015

Thanks for this!

@Deele
Deele commented Nov 10, 2015

Thank you! Just installed it in vagrant and it works.

@voodoorai2000

Thank you! It works ๐Ÿ‘

@lu-ko
lu-ko commented Nov 12, 2015

Problem: Fontconfig warning: ignoring UTF-8: not a valid region tag
Fix was in my case very simple, just add following variable to my bash profile: LC_ALL=en_US.UTF-8

@tonnydulo

Thanks for This

@adrianparr

Thanks for this. It worked for me.

@nando
nando commented Nov 24, 2015

๐Ÿ‘

Thanks, thanks, thanks,
-- nando

@mwillerich

on Ubuntu 14.04 apt-get install phantomjs installs version 1.9.0-1 successfully for me.

@avral
avral commented Dec 15, 2015

Thank you very much =)

@dimanyc
dimanyc commented Dec 24, 2015

works! Thanks!

@mkhusu
mkhusu commented Jan 4, 2016

amazing stuff!
Thanks so much! Have been trying to figure this out for quite some time now

@jerryjohnjacob

Awesome!! ๐Ÿ‘

@pette9
pette9 commented Jan 24, 2016

worked, Thank You ๐Ÿ‘

@makshark
makshark commented Feb 7, 2016

nice

@rotatopoti

thanks

@lucas1
lucas1 commented Mar 7, 2016

Thanks

@aidankmcl

Just completed successfully with version 2.1.1, thanks!

@ethagnawl

Ha - and here I was worried this would be out-of-date...

But, seriously, this is incredibly useful. Thanks for putting it together and keeping it up-to-date!

@onliners

Thanks a lot!

@stefnats

Thanks! :-)

@vivex
vivex commented Mar 28, 2016

Thanks

@MayankPratap

Thanks :)

@langri-sha

Thanks ๐Ÿ˜ป! Everyone, don't forget to look at the latest gist revision ๐Ÿ“

@sridharkalaibala

Thanks a lot friend :)

@rimidl
rimidl commented Apr 11, 2016

Thanks!

@natanaugusto

Thanks! Very helpful

@yanshiyason

Thanks!!!

@amihaiemil

Great! Thank you very much!

@andyxmas
andyxmas commented May 3, 2016

Worked like a charm on Ubuntu 14 getting version 2.1.1 of Phantom using https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-linux-x86_64.tar.bz2. Thanks!

@fawyna
fawyna commented May 19, 2016

Thanks for the simple guide!

@kennytilton

Brilliant.

@ghprod
ghprod commented May 30, 2016

Thanks :)

@achmadns

Thanks.

@ybaras
ybaras commented Jun 8, 2016 edited

wget from Bitbucket sometimes results in a "20 redirection exceeded" error.
I would wget from github instead ( and get a newer version ๐Ÿ˜„ )

export PHANTOM_JS="phantomjs-2.1.1-linux-x86_64"
wget https://github.com/Medium/phantomjs/releases/download/v2.1.1/$PHANTOM_JS.tar.bz2

and thanks for this Gist ๐Ÿ‘

@JorgeRdg

Thanks for the Gist!

@enfeizhan

My sincere thanks. Heaps of instructions on the web but only yours works to me perfectly.

@a1exlism

Thanks for sharing.

@DanDvoracek

Thanks!

@nisevi
nisevi commented Aug 1, 2016

๐Ÿ‘

@milad145
milad145 commented Aug 8, 2016

how can i uninstall phantomjs?

@adamwlev
adamwlev commented Aug 9, 2016

Thank you!

@mathieujobin

Great script...
but this working curl command that worked for me

sudo curl --output /usr/local/bin/phantomjs https://s3.amazonaws.com/circle-downloads/phantomjs-2.1.1

from: https://discuss.circleci.com/t/add-phantomjs-2-1-1-to-the-platform/1755/4

@loretoparisi

So this is the updated script for 2.1.1 and the new tarball url

#!/usr/bin/env bash
# This script install PhantomJS in your Debian/Ubuntu System
#
# This script must be run as root:
# sudo sh install_phantomjs.sh
#

if [[ $EUID -ne 0 ]]; then
    echo "This script must be run as root" 1>&2
    exit 1
fi

PHANTOM_VERSION="phantomjs-2.1.1"
ARCH=$(uname -m)

if ! [ $ARCH = "x86_64" ]; then
    $ARCH="i686"
fi

PHANTOM_JS="$PHANTOM_VERSION-linux-$ARCH"

sudo apt-get update
sudo apt-get install build-essential chrpath libssl-dev libxft-dev -y
sudo apt-get install libfreetype6 libfreetype6-dev -y
sudo apt-get install libfontconfig1 libfontconfig1-dev -y

cd ~
wget https://github.com/Medium/phantomjs/releases/download/v2.1.1/$PHANTOM_JS.tar.bz2
sudo tar xvjf $PHANTOM_JS.tar.bz2

sudo mv $PHANTOM_JS.tar.bz2 /usr/local/share/
sudo ln -sf /usr/local/share/$PHANTOM_JS/bin/phantomjs /usr/local/share/phantomjs
sudo ln -sf /usr/local/share/$PHANTOM_JS/bin/phantomjs /usr/local/bin/phantomjs
sudo ln -sf /usr/local/share/$PHANTOM_JS/bin/phantomjs /usr/bin/phantomjs
@capitolkrs

Thank you for the gist

@mikesten

@loretoparisi Thanks for the update! Small typo towards the end of your script...

sudo mv $PHANTOM_JS.tar.bz2 /usr/local/share/ should be sudo mv $PHANTOM_JS /usr/local/share/

@arturmalecki

Good stuff ๐Ÿ‘

@miked0004

Thanks for this. Since it runs as root, it does not need sudo.

I dumped this in a Dockerfile here: https://gist.github.com/miked0004/b4aa62b62afbb16c67528f7b5f520041

See the docker-compose.yml in that gist for an example of running the box to bundle and run rake.

@jondelmil

Thanks @mikesten and @loretoparisi -- the bitbucket URL for wget does work for me.

@tregenza

Second to @mikesten comment on @loretoparisi 's script

sudo mv $PHANTOM_JS.tar.bz2 /usr/local/share/ should be sudo mv $PHANTOM_JS /usr/local/share/

Other than than the script works perfectly.

@FredyKonig

what about :
sudo apt-get install phantomjs

@rista404
rista404 commented Dec 2, 2016

Thanks @loretoparisi and @mikesten for the updated script, works great! ๐ŸŽ‰

@ambikashri

I am getting the below error. Could somebody suggest what could be done. I am using ubuntu

customer@QA-Ubuntu-1:~/automation$ phantomjs --webdriver=8090 --webdriver-selenium-grid-hub=http://127.0.0.1:4444
QXcbConnection: Failed to initialize XRandr
Qt: XKEYBOARD extension not present on the X server.
[INFO - 2016-12-07T10:46:27.715Z] GhostDriver - Main - running on port 8090
[INFO - 2016-12-07T10:46:27.720Z] GhostDriver - Main - registering to Selenium HUB 'http://127.0.0.1:4444' version: using '127.0.0.1:8090' with org.openqa.grid.selenium.proxy.DefaultRemoteProxy as remote proxy. [ERROR - 2016-12-07T10:46:27.740Z] GhostDriver - main.fail - {"line":97,"stack":"register\nglobal code"}

@z
z commented Dec 18, 2016

The other update for 2.1.1 had issues, I modified the script below:

#!/usr/bin/env bash
# This script install PhantomJS in your Debian/Ubuntu System
#
# This script must be run as root:
# sudo sh install_phantomjs.sh
#

if [[ $EUID -ne 0 ]]; then
    echo "This script must be run as root" 1>&2
    exit 1
fi

PHANTOM_VERSION="phantomjs-2.1.1"
ARCH=$(uname -m)

if ! [ $ARCH = "x86_64" ]; then
    $ARCH="i686"
fi

PHANTOM_JS="$PHANTOM_VERSION-linux-$ARCH"

sudo apt-get update
sudo apt-get install build-essential chrpath libssl-dev libxft-dev -y
sudo apt-get install libfreetype6 libfreetype6-dev -y
sudo apt-get install libfontconfig1 libfontconfig1-dev -y

cd ~
wget https://bitbucket.org/ariya/phantomjs/downloads/$PHANTOM_JS.tar.bz2
sudo tar xvjf $PHANTOM_JS.tar.bz2

sudo mv $PHANTOM_JS /usr/local/share/
sudo ln -sf /usr/local/share/$PHANTOM_JS/bin/phantomjs /usr/local/share/phantomjs
sudo ln -sf /usr/local/share/$PHANTOM_JS/bin/phantomjs /usr/local/bin/phantomjs
sudo ln -sf /usr/local/share/$PHANTOM_JS/bin/phantomjs /usr/bin/phantomjs
@gautiermichelin

If this script has to be run through a sudo, why the sudo lines at the end ?
It's redundant...

Prefer :

#!/usr/bin/env bash
# This script install PhantomJS in your Debian/Ubuntu System
#
# This script must be run as root:
# sudo sh install_phantomjs.sh
#

if [[ $EUID -ne 0 ]]; then
    echo "This script must be run as root" 1>&2
    exit 1
fi

PHANTOM_VERSION="phantomjs-2.1.1"
ARCH=$(uname -m)

if ! [ $ARCH = "x86_64" ]; then
    $ARCH="i686"
fi

PHANTOM_JS="$PHANTOM_VERSION-linux-$ARCH"

apt-get update
apt-get -y install build-essential chrpath libssl-dev libxft-dev libfreetype6 libfreetype6-dev libfontconfig1 libfontconfig1-dev

cd ~
wget https://bitbucket.org/ariya/phantomjs/downloads/$PHANTOM_JS.tar.bz2
tar xvjf $PHANTOM_JS.tar.bz2
mv $PHANTOM_JS /usr/local/share/
ln -sf /usr/local/share/$PHANTOM_JS/bin/phantomjs /usr/local/share/phantomjs
ln -sf /usr/local/share/$PHANTOM_JS/bin/phantomjs /usr/local/bin/phantomjs
ln -sf /usr/local/share/$PHANTOM_JS/bin/phantomjs /usr/bin/phantomjs

@julionc nice and very useful gist :-) ๐Ÿ‘

@ssi-anik

๐Ÿ‘ for the post, and if anyone wants to download the latest one, go get the file name, and change the PHANTOM_JS variable.

@illia108

Thanks. Worked for me!

@bologer
bologer commented Jan 26, 2017 edited

ะ”ะปั ั‚ะตั…, ะบั‚ะพ ะผัƒั‡ะฐะตั‚ัั ัƒัั‚ะฐะฝะพะฒะบะพะน phantomjs 2.x.x ะฝะฐ Ubuntu 14.0 โ€” http://bologer.ru/kak-ustanovitobnovit-phantomjs-v-ubuntu-14-04/

@frankie-loves-jesus

Worked here as well, thanks for a wonderful thread!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment