36

Getting the following error when trying to use phantomjs from ruby on Ubuntu:

 Failure/Error: visit root_path
 Cliver::Dependency::NotFound:
   Could not find an executable ["phantomjs"] on your path.
 # ./spec/features/search_spec.rb:17:in `block (2 levels) in <top (required)>'
 # ./spec/support/vcr.rb:23:in `block (3 levels) in <top (required)>'
 # ./spec/support/vcr.rb:23:in `block (2 levels) in <top (required)>'

phantomjs was built locally and added to PATH. How do I make ruby find phantomjs?

Sergey Evstifeev
  • 4,941
  • 2
  • 24
  • 28

6 Answers6

34

You can also do

$ sudo apt-get install phantomjs                               

That should automatically add phantomjs to your path, and do everything else necessary for it to run correctly. This worked for me.

Sal-laS
  • 11,016
  • 25
  • 99
  • 169
Matthew
  • 668
  • 5
  • 12
33

Instead of building locally, use homebrew on your mac with brew install phantomjs and all the paths will link after. I had this error myself, and you'll get the links for free and have the ability to update easily.

pjammer
  • 9,489
  • 5
  • 46
  • 56
  • 1
    As stated in the question, this is regarding phantomjs on Ubuntu, not Mac. – Sergey Evstifeev Sep 07 '15 at 08:36
  • 5
    As google has proven, this is the top result for anything to do with phantomjs and that error. 3 upvotes also prove that you should fix the question as it's not OS specific. – pjammer Feb 08 '16 at 12:26
  • 2
    You can't use just `brew install` anymore, it's now a cask, so do `brew cask install phantomjs` – Mint Aug 19 '19 at 00:55
16

For Mac Os El Capitan use following command:

npm install -g phantomjs

Above command only works if you have installed npm, for installing npm:

 brew install npm
Aamir
  • 16,329
  • 10
  • 59
  • 65
12

add to Gemfile

gem 'phantomjs', :require => 'phantomjs/poltergeist'

or put code below to spec_helper.rb

require 'phantomjs' 
Capybara.register_driver :poltergeist do |app|
    Capybara::Poltergeist::Driver.new(app, :phantomjs => Phantomjs.path)
end

https://github.com/colszowka/phantomjs-gem

gayavat
  • 18,910
  • 11
  • 45
  • 55
  • 1
    This should be the accepted answer. The point of the phantomjs gem is so you don't have to install the executable manually. But without the executable installed by brew or apt, you have to point the driver to the bin path. – evanbikes Jun 30 '21 at 19:01
  • Agreed. This should be the accepted answer. This will save you a ton of headaches if you run your tests in a different CI environment. – Jason L. Mar 09 '23 at 04:23
3

Apparently, the solution was to add phantomjs not only to the PATH, but also create links:

sudo ln -s /home/myuser/phantomjs/bin/phantomjs /usr/bin/phantomjs
sudo ln -s /home/myuser/phantomjs/bin/phantomjs /usr/local/bin/phantomjs
sudo ln -s /home/myuser/phantomjs/bin/phantomjs /usr/local/share/phantomjs

Adjust the /home/myuser/phantomjs/bin/phantomjs paths to match the path to phantomjs binary on your machine.

Sergey Evstifeev
  • 4,941
  • 2
  • 24
  • 28
-1

Other possible solution is to add executable rights to file:

# download phantomjs
$ curl --output /home/user/.rvm/bin/phantomjs https://s3.amazonaws.com/circle-downloads/phantomjs-2.1.1
# set rights 
$ chmod +x /home/user/.rvm/bin/phantomjs
# check
$ which phantomjs
/home/user/.rvm/bin/phantomjs

And also it is not recommended by poltergeist to use phantomjs from official Ubuntu repos:

DO NOT use phantomjs from the official Ubuntu repositories, since it doesn't work well with poltergeist.

Artem P
  • 5,198
  • 5
  • 40
  • 44