45

I'm trying to install PhantomJS on my Mac (Yosemite).

I did the following:

npm install phantomjs

Then I did:

npm install phantomjs-prebuilt

Both of which appear in my node_modules. But when I try to run phantomjs --versionI get

   -bash: phantomjs: command not found

Also tried installing the binary from the downloads website, but nothing. What am I missing? End goal is to use casperjs but currently casper is asking

Fatal: [Errno 2] No such file or directory; did you install phantomjs?

I thought I did....?

jblakeley
  • 816
  • 1
  • 10
  • 20

7 Answers7

71

If you are using Homebrew, you can type:

brew tap homebrew/cask
brew cask install phantomjs
villecoder
  • 13,323
  • 2
  • 33
  • 52
Vladtn
  • 2,506
  • 3
  • 27
  • 23
  • 11
    It was migrated from homebrew/core to homebrew/cask. You can access it again by running: brew tap homebrew/cask And then you can install it by running: brew cask install phantomjs – ran632 Oct 04 '18 at 14:41
  • 7
    It reminded me to use `brew install --cask phantomjs`. – SparkAndShine Jan 27 '21 at 09:49
40
  1. Download phantomjs latest version (ex: phantomjs-2.1.1-macosx.zip) from http://phantomjs.org/download.html
  2. Extract it to some path(ex: ~/Desktop/phantomjs-2.1.1-macosx)
  3. Run this command on terminal - sudo ln -n ~/Desktop/phantomjs-2.1.1-macosx/bin/phantomjs /usr/local/bin/
  4. Launch phantomjs from the terminal by command: phantomjs
  5. Check phantomjs version by command: phantomjs -v
  6. Check the phantomjs path by command: which phantomjs
user2625094
  • 606
  • 6
  • 12
26

The accepted answer is outdated. On Mac, using Homebrew, type:

brew tap homebrew/cask
brew cask install phantomjs

The first step may not be necessary for some MacOS versions.

For newer versions of cask (2021 and later), use

brew install --cask phantomjs
cgnorthcutt
  • 3,890
  • 34
  • 41
18

You need to add phantomjs binary to your PATH. You can do this either by modifying the environment variable or by symlinking the binary to for example /usr/local/bin

For example, if you want to symlink the binary:

cd $HOME
curl -O https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-macosx.zip
ln -s $HOME/phantomjs-2.1.1-macosx/bin/phantomjs /usr/bin/phantomjs

or if you want to modify the PATH environment variable:

cd $HOME
curl -O https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-macosx.zip

and add this line to this file: ~/.bash_profile:

export PATH=$HOME/phantomjs-2.1.1-macosx/bin:$PATH

and then run:

source ~/.bash_profile
Godraude
  • 620
  • 1
  • 8
  • 17
3

Shouldn't you be installing phantomjs globally?

npm install -g phantomjs

Since you are installing it locally, the command -

phantomjs -v

would not work everywhere.

You can check the difference between local install and global install here: https://docs.npmjs.com/cli/install

Rito
  • 3,092
  • 2
  • 27
  • 40
0

After a lot of digging I found that on on High-Sierra old versions of phantomjs (<2.0) would not work.

Also, the zip file via the phantomjs download page is a faulty one.

If you need a new phantom, just go to this page and download phantomjs 2.1.1 (or later).

zevij
  • 2,416
  • 1
  • 23
  • 32
-1

for Mac BigSur

brew tap homebrew/cask
brew install --cask phantomjs
Jamil Bashir
  • 307
  • 3
  • 10