165

I set up PhantomJS and recorded it to video: https://www.dailymotion.com/video/xnizmh_1_webcam

Build instructions: http://phantomjs.org/build.html

Is there anything wrong in my setup?

After I set it up I read the quick start tutorial and tried to write this code

phantomjs hello.js 

It gives me "command not found" error. How can I solve this problem?

rmtheis
  • 5,992
  • 12
  • 61
  • 78
FURKAN ILGIN
  • 2,290
  • 3
  • 18
  • 21

25 Answers25

361

Guidouil's answer put me on the right track. I had to add one additional symlink to /usr/bin/, and I did direct symlinks for all 3 - see below.

I'm installing on Ubuntu server Natty Narwhal.

This is exactly what I did.

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
sudo ln -s /usr/local/share/phantomjs-1.9.7-linux-x86_64/bin/phantomjs /usr/bin/phantomjs

And finally when I do

phantomjs -v

I get 1.9.7

If anyone sees any problems with what I've done, please let me know.

Pierre.Vriens
  • 2,117
  • 75
  • 29
  • 42
shawn
  • 3,922
  • 1
  • 17
  • 9
  • 7
    if you need that final sym link it is because you don't have /usr/local/bin in your PATH – DrewB Jan 11 '13 at 17:46
  • I am getting error on this command sudo ln -s /usr/local/share/phantomjs-1.8.1-linux-x86_64/bin/phantomjs /usr/local/bin/phantomjs ln: creating symbolic link `/usr/local/bin/phantomjs': No such file or directory – Amar Banerjee Jun 10 '13 at 15:39
  • Please check before downloading the tar file via terminal: uname -i – Shakil Ahmed Jul 10 '13 at 17:53
  • @AmarBanerjee you need to use sudo in front of the previous and while unwrapping the tar ball. – Shane Aug 03 '13 at 07:57
  • 2
    This works, but can anyone explain to me why shouldn't we just copy paste the whole thing into `usr/bin` instead of creating a link to `/usr/local`? – Heisenberg Nov 02 '13 at 05:36
  • 6
    Anyone get permission denied too? – User Mar 01 '14 at 01:31
  • Thank you for this prebuild link, as i coulnt get it to build on Ubuntu – tristanbailey Mar 24 '14 at 09:37
  • 1
    For phantomjs to be available from any directory, it is enough if you add a symlink to one of the directories listed in your $PATH variable. (run "echo $PATH" to see which they are). – Juampy NR Mar 24 '14 at 10:23
  • 5
    I ran into this issue: bin/phantomjs: error while loading shared libraries: libfontconfig.so.1: cannot open shared object file: No such file or directory. I found a fix [here](https://github.com/ariya/phantomjs/issues/10904). Run the command: `sudo apt-get install libfontconfig` – Neil Hoff Apr 14 '14 at 15:54
  • Nice answer mate. I knew that but I was not dare to do that. Your answer encourages me and it is on progress. – kta Jun 01 '16 at 06:33
  • I put these commands in my CI build steps. They worked fine…until the CI server started getting 403 errors from bitbucket.org. (I'm guessing it's rate-limiting.) So I found [this SO post](http://stackoverflow.com/questions/39533707/bitbucket-rate-limiting-phantomjs#39533708) and changed the URL to `https://cnpmjs.org/mirrors/phantomjs/phantomjs-2.1.1-linux-x86_64.tar.bz2`. – Jeff Terrell Ph.D. Mar 21 '17 at 00:08
  • could you please chek this too https://stackoverflow.com/questions/44624964/phantom-js-on-web-project – Manik Jun 19 '17 at 08:04
114

PhantomJS is on npm. You can run this command to install it globally:

npm install -g phantomjs-prebuilt  

phantomjs -v should return 2.1.1

Francisco Presencia
  • 8,732
  • 6
  • 46
  • 90
Arnel Bucio
  • 1,255
  • 1
  • 9
  • 12
58

download from phantomjs website the prebuilt package : http://phantomjs.org/download.html then open a terminal and go to the Downloads folder

sudo mv phantomjs-1.8.1-linux-x86_64.tar.bz2 /usr/local/share/.
cd /usr/local/share/
sudo tar xjf phantomjs-1.8.1-linux-x86_64.tar.bz2
sudo ln -s /usr/local/share/phantomjs-1.8.1-linux-x86_64 /usr/local/share/phantomjs
sudo ln -s /usr/local/share/phantomjs/bin/phantomjs /usr/local/bin/phantomjs

then to check install phantomjs -v should return 1.8.1

Ponkadoodle
  • 5,777
  • 5
  • 38
  • 62
Guidouil
  • 1,694
  • 2
  • 18
  • 18
  • Why do you create a link instead of copy paste directly? – Heisenberg Nov 02 '13 at 05:29
  • 2
    Why would you copy paste directly when you can symlink? =/ – jmar Apr 23 '14 at 17:39
  • 1
    on a fresh ubuntu also apt-get install libfontconfig – Ryan Detzel Sep 05 '14 at 15:51
  • I had phantomjs installed in my user directory. Selenium didn't work with that. I then moved phantomjs to `/usr/local/share` and created these symbolic links. Then it worked (even though file permissions were the same). Can anyone explain to this non-linux-expert why this made a difference? – Peter May 17 '17 at 09:44
38

Install from package manager:

sudo apt-get install phantomjs
  • 2
    Sadly, this now installs 1.4, which is so old many things that use phantom.js just won't work. – Julian Birch Jul 29 '13 at 12:38
  • @JulianBirch Yes, I think if you have in Ubuntu Saucy Salamander(13.10) you can get the 1.9.0 version according to this [link] https://launchpad.net/ubuntu/+source/phantomjs, I just installed from Raring Ringtail(13.04) and I get 1.6.0 version,like it also say in the link –  Jul 30 '13 at 01:02
  • 3
    Installed 1.9.0 on Ubuntu 14.04 – Ionică Bizău Jun 19 '14 at 16:06
  • 6
    But I recommend to install it via [`npm install -g phantomjs`](http://stackoverflow.com/a/22524611/1420197). – Ionică Bizău Jun 19 '14 at 16:18
  • 1
    Just installed. It's 1.9.0 now. – Strabek Nov 15 '15 at 18:15
14

Here are the build steps I used (note these instructions are for version 1.3. See comments to this answer for the installation instructions of the latest PhantomJS):

sudo apt-get update
sudo apt-get install git-core
sudo apt-get install build-essential
sudo apt-get install libqt4-dev libqtwebkit-dev qt4-qmake
git clone git://github.com/ariya/phantomjs.git && cd phantomjs
git checkout 1.3
qmake-qt4 && make

Now install Xvfb

sudo apt-get install xvfb xfonts-100dpi xfonts-75dpi xfonts-scalable xfonts-cyrillic

Launch Xvfb:
Xvfb :23 -screen 0 1024x768x24 &

Now run phantom:
DISPLAY=:23 ./phantomjs hello.js

Rao
  • 20,781
  • 11
  • 57
  • 77
Vijay Boyapati
  • 7,632
  • 7
  • 31
  • 48
  • 16
    These instructions are now obsolete, in the current version (1.5) there is no need to install either qt or xfvb. See http://code.google.com/p/phantomjs/wiki/Installation – michelpm Jun 04 '12 at 21:21
  • 2
    Michel is correct, the latest version of phantom should be used because it doesn't require a virtual display (Xvfb) – Vijay Boyapati Sep 28 '12 at 18:27
  • Answer from Andre was quicker and easier for me. – fooMonster Apr 23 '13 at 18:04
12

For PhantomJS version above 1.5, consider this (verbatim copy of the build instructions on the phantom website):

For Ubuntu Linux (tested on a barebone install of Ubuntu 10.04 Lucid Lynx and Ubuntu 11.04 Natty Narwhal):

sudo apt-get install build-essential chrpath git-core libssl-dev libfontconfig1-dev
git clone git://github.com/ariya/phantomjs.git
cd phantomjs
git checkout 1.7
./build.sh
iwein
  • 25,788
  • 10
  • 70
  • 111
nbk
  • 1,992
  • 2
  • 19
  • 34
  • 3
    I think it should be noted (as it is on PhantomJS website) that building from source can take a **very long** time (hours on a small machine). – Whymarrh Feb 20 '14 at 07:05
  • 1
    It should also be noted that this answer resolved the issue I had with phantomjs when using the prebuilt binaries failed. It took around 1 hour to compile on 2gb ubuntu 10.04 dual core virtual. – Keith John Hutchison May 01 '15 at 22:43
7

For Ubuntu you can use the prebuilt versions downloadable from the PhantomJS site.

If you have some serious time on your hands you can also build it yourself. (This is exactly the procedure from Nikhil's answer).

The guys over at PhantomJS recommend using the binaries to save time:

Warning: Compiling PhantomJS from source takes a long time, mainly due to thousands of files in the WebKit module. With 4 parallel compile jobs on a modern machine, the entire process takes roughly 30 minutes. It is highly recommended to download and install the ready-made binary package if it is available.

With a modern machine they mean > 4 cores, > 8gb mem I think. I tried it on a micro AWS instance and gave up after 2 hours.

In short: install the prebuilt packages from the PhantomJS site per their instructions.

iwein
  • 25,788
  • 10
  • 70
  • 111
4

in my vagrant bootstrap:

apt-get install -y build-essential chrpath git-core libssl-dev libfontconfig1-dev
git clone git://github.com/ariya/phantomjs.git
cd phantomjs
git checkout 1.9
echo y | ./build.sh
ln -s /home/vagrant/phantomjs/bin/phantomjs /usr/local/bin/phantomjs
cd ..
rev
  • 1,681
  • 2
  • 13
  • 21
  • @artjom b: this would be better as a comment than an edit. "You don't have to forget to put the resulting executable from ./build.sh into a directory that is in the PATH environment variable. This can be done via symlink as in this example." – rev Dec 29 '14 at 21:23
  • You don't have to forget to put the resulting executable from `./build.sh` into a directory that is in the PATH environment variable. This can be done via symlink as in this example. – Artjom B. Dec 29 '14 at 21:27
  • That's what all the other answers forget when using `build.sh`. I only wanted to improve your answer by making that fact more visible. If you don't want it there, fine. – Artjom B. Dec 29 '14 at 21:32
4

Personaly I prefer using npm (see Arnel Bucio answer)

sudo npm install -g phantomjs

but! I noticed that some of npm module still can't see it as global executable.

enter image description here

so!

  • Create new /usr/share/phantomjs/ directory link

    cd /usr/share
    sudo ln -s ../lib/node_modules/phantomjs/lib/phantom phantomjs
    
  • Remove old /usr/bin/phantomjs executable link and create the new one

    cd /usr/bin
    sudo mv phantomjs phantomjs.old
    sudo ln -s ../share/phantomjs .
    
Community
  • 1
  • 1
Fery W
  • 1,402
  • 1
  • 15
  • 28
  • 1
    Doing the commands after broke it for me on Ubuntu 14.04. Works with just the `npm` install, and `sudo apt-get install libfontconfig` after. – Tarang Oct 19 '14 at 07:17
2

I have done with this.

sudo apt-get update
sudo apt-get install build-essential chrpath git-core libssl-dev libfontconfig1-dev
git clone git://github.com/ariya/phantomjs.git
cd phantomjs
git checkout 1.9
./build.sh
Amar Banerjee
  • 4,992
  • 5
  • 34
  • 51
2

See link Installation guide is in ...

https://gist.github.com/julionc/7476620

And run in terminal with this command

phantomjs --webdriver=4444

Yasar Arafath
  • 605
  • 1
  • 9
  • 30
1

From the official site: phantomjs site

sudo apt-get install build-essential chrpath git-core libssl-dev libfontconfig1-dev
git clone git://github.com/ariya/phantomjs.git
cd phantomjs
git checkout 1.8
./build.sh
Ihor Shubin
  • 10,628
  • 3
  • 25
  • 33
1

For Ubuntu, download the suitable file from http://phantomjs.org/download.html. CD to the downloaded folder. Then:

sudo tar xvf phantomjs-1.9.0-linux-x86_64.tar.bz2
sudo mv phantomjs-1.9.0-linux-x86_64 /usr/local/share/phantomjs
sudo ln -s /usr/local/share/phantomjs/bin/phantomjs /usr/bin/phantomjs

Make sure to replace the file name in these commands with the file you have downloaded.

Humming
  • 453
  • 4
  • 15
1

Be aware this is definitely one way to do it:

$ sudo apt-get install phantomjs
$ phantomjs -v
1.6.0

Sadly, it installs 1.6 and not the latest one, but this works for my purposes.

Starkers
  • 10,273
  • 21
  • 95
  • 158
1

I know this is too old, but, just i case someone gets to this question from Google now, you can install it by typing apt-get install phantomjs

Nico Savini
  • 458
  • 3
  • 10
1

On Ubuntu for Windows, I found neither apt-get nor npm versions worked for me. What worked was the script from this comment.

For ease of use, I pasted the whole thing into a script file called install_phantomjs.sh, made it executable (chmod u+x install_phantomjs.sh), and then ran it (./install_phantomjs.sh)

Nick F
  • 9,781
  • 7
  • 75
  • 90
0

Or the latest - 32bit version Linux

sudo wget http://phantomjs.googlecode.com/files/phantomjs-1.9.2-linux-i686.tar.bz2

sudo ln -s /usr/local/share/phantomjs-1.9.2-linux-i686/bin/phantomjs /usr/local/share/phantomjs

sudo ln -s /usr/local/share/phantomjs-1.9.2-linux-i686/bin/phantomjs /usr/local/bin/phantomjs

sudo ln -s /usr/local/share/phantomjs-1.9.2-linux-i686/bin/phantomjs /usr/bin/phantomjs
Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
Nesha Zoric
  • 6,218
  • 42
  • 34
0

Installation and Calling Phantomjs

Follow the steps doesn't work, but cloned from others built. (ver2.0)

Community
  • 1
  • 1
0

Bellow the installation procedure by Julio Napurí https://gist.github.com/julionc

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
PYK
  • 3,674
  • 29
  • 17
0

I have found this simpler way - Phantom dependencies + Npm

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

and npm

[sudo] npm install -g phantomjs

Done.

Nitin Jadhav
  • 6,495
  • 1
  • 46
  • 48
0

On linux to run hello.js don't forget to add the path of hello.js:

phantomjs YourPathToPhantomjsFolder/examples/hello.js

Mr Rubix
  • 1,492
  • 14
  • 27
0

If you want to use phantomjs easily, you can use it at phantomjscloud.com You can get the result just by http request.

Shin Kim
  • 4,911
  • 3
  • 29
  • 31
0

This is how I place a specific version of phantomjs in /usr/local/bin on my docker containers.

curl -Ls https://github.com/Medium/phantomjs/releases/download/v1.9.19/phantomjs-1.9.8-linux-x86_64.tar.bz2 \
    | tar jxvf - --strip-components=2 -C /usr/local/bin/ ./phantomjs-1.9.8-linux-x86_64/bin/phantomjs

or with out ./ depending on OS.

curl -Ls https://github.com/Medium/phantomjs/releases/download/v1.9.19/phantomjs-1.9.8-linux-x86_64.tar.bz2 \
    | tar jxvf - --strip-components=2 -C /usr/local/bin/ phantomjs-1.9.8-linux-x86_64/bin/phantomjs
MrAirikr
  • 21
  • 3
0

You can get up and running without sudo or npm. Simply download, extract, and add to path.

This has the added advantage of easy backup if you are in the habit of backing up your entire home folder which I highly recommend. This also works with any version of Linux.

➤  cd ~
➤  wget https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-linux-x86_64.tar.bz2
➤  mkdir phantomjs
➤  tar xjf phantomjs-2.1.1-linux-x86_64.tar.bz2 -C phantomjs
➤  echo 'export PATH="$PATH:$HOME/phantomjs/bin"' >> .profile
➤  source .profile
➤  phantomjs -v
2.1.1

The disadvantages are:

  1. You will need to manually upgrade
  2. Other users will not have access to this.

You could use a very simple shell script for installing/upgrading

#!/bin/sh
# install_phantomjs.sh $VERSION

$VERSION = $1
printf "Downloading PhantomJS $VERSION...\n"
wget "https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-$VERSION-linux-x86_64.tar.bz2"

printf "Extracting PhantomJS $VERSION to ~/phantomjs...\n"
mkdir ~/phantomjs
tar xjf phantomjs-$VERSION-linux-x86_64.tar.bz2 -C ~/phantomjs

printf "Done! Make sure $HOME/phantomjs/bin is in your path.\n"

Or in a Dockerfile

# Download and setup PhantomJS
ENV PHANTOMJS_VERSION 2.1.1
RUN curl -fSL "https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-$PHANTOMJS_VERSION-linux-x86_64.tar.bz2" -o /tmp/phantomjs.tar.bz2 && \
  mkdir ~/phantomjs && \
  tar xjf /tmp/phantomjs.tar.bz2 -C ~/phantomjs && \
  rm /tmp/phantomjs.tar.bz2
ENV PATH /home/$USERNAME/phantomjs/bin:$PATH
HarlemSquirrel
  • 8,966
  • 5
  • 34
  • 34
0

Here is what I did on my ubuntu 16.04 machine

sudo apt-get update
sudo wget https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-linux-x86_64.tar.bz2
sudo mv path/where/you/downloaded/phantomjs  /usr/bin

and finally when I do

phantomjs -v

I get 2.1.1

After going through every answer of this thread. I think this is the best solution for installing and running phantomjs in ubuntu.

aakrshak_
  • 1
  • 1