35

I followed these instructions (except for copying the executable to my PATH because I cannot seem to find it and it does not seem necessary). Then I made a file called image_render.js in my public javascripts directory with

console.log('Hello, world!');
phantom.exit();

inside it, saved it, and ran phantomjs render_image.js in my terminal. However, my terminal does not recognize the command:

-bash: phantomjs: command not found

What have I done wrong?

Justin Liu
  • 581
  • 6
  • 21
user730569
  • 3,940
  • 9
  • 42
  • 68

5 Answers5

71

The PATH really is the important part. You can skip it, however, if you specify the absolute path. Try something like this:

/path/to/phantomjs render_image.js

...but that can get tiring. For a quick way to add it to your PATH, from the directory of the phantomjs executable, symbolically link it into /usr/local/bin:

sudo ln -s /path/to/phantomjs /usr/local/bin/

/usr/local/bin is likely on your PATH.

icktoofay
  • 126,289
  • 21
  • 250
  • 231
  • where do I find the executable? where exactly do I put it? in my root? – user730569 Jun 23 '11 at 01:47
  • 3
    @user730569: Check the `bin` folder. If you're on Linux, it should be right in there. If you're on Mac OS X, it should be in `phantomjs.app/Contents/MacOS`, which should be in there. – icktoofay Jun 23 '11 at 01:49
  • I'm not sure where this `bin` folder is. I'm on Mac OSX – user730569 Jun 23 '11 at 01:58
  • @user730569: Assuming you just opened up a terminal and copied the code from the build instructions web page into there, the `bin` directory should be in the `phantomjs` directory in your home directory. – icktoofay Jun 23 '11 at 02:03
  • @user730569: If you couldn't access it by typing `phantomjs`, it *isn't* on your `PATH`. That's why you need to link it to somewhere that is, as I showed in my answer. Alternatively, you could add something to your `PATH`, but that's a little more complex. – icktoofay Jun 23 '11 at 02:11
  • I put it into `/usr/local/bin` and `phantomjs` does not work so it must not be on my `PATH`. How do I find out my `PATH`? – user730569 Jun 23 '11 at 02:16
  • this is what I get: `/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/git/bin:/usr/X11/bin:/usr/local/mysql/bin`... the executable is currently on my desktop ` – user730569 Jun 23 '11 at 02:22
  • 2
    @user730569: If that's your `PATH` and the `phantomjs` executable is in `/usr/local/bin`, it should work. Did you use an absolute path when linking it? – icktoofay Jun 23 '11 at 02:24
  • I'm not sure. I typed `sudo ln -s /path/to/phantomjs /usr/local/bin/` but maybe I didn't get the path to the executable correct? what would the path be now that it's on my desktop? – user730569 Jun 23 '11 at 02:27
  • @user730569: The `/path/to/phantomjs` was a placeholder. Replace `/path/to/phantomjs` with the absolute path to the PhantomJS executable. If your username (the UNIX one, not the full name) was `johndoe`, the absolute path to your executable might be `/Users/johndoe/phantomjs/bin/phantomjs.app/Contents/MacOS/phantomjs`. – icktoofay Jun 23 '11 at 02:31
  • @user730569: If it's on your desktop it might be `/Users/johndoe/Desktop/phantomjs` (if it's the executable and *not* the `.app`), but you'll have to keep it on your desktop unless it's copied, so that's no good. – icktoofay Jun 23 '11 at 02:32
  • my UNIX username? sorry I'm really bad at this – user730569 Jun 23 '11 at 02:33
  • @user730569: It may be displayed at your `bash` prompt as `username@host`. Take the `username` part. Aside: if you're going to be programming using PhantomJS or other things, you'll probably want to get acquainted with the shell. – icktoofay Jun 23 '11 at 02:35
  • this is displayed at the prompt: `new-computers-computer:~ newcomputer$` – user730569 Jun 23 '11 at 02:36
  • @user730569: In that case, it's the word just before the `$`. – icktoofay Jun 23 '11 at 02:37
  • so I ran `sudo ln -s /Users/newcomputer/phantomjs/bin/phantomjs.app/Contents/MacOS/phantomjs /usr/local/bin/` but still no luck :/ – user730569 Jun 23 '11 at 02:40
  • my shell is telling me `/Users/newcomputer/phantomjs/bin/phantomjs.app/Contents/MacOS/phantomjs` doesn't exist – user730569 Jun 23 '11 at 02:49
  • ok so `/Users/newcomputer/phantomjs` doesn't exist but `/Users/newcomputer` does – user730569 Jun 23 '11 at 02:52
  • @user730569: Where did you execute the build instructions from? – icktoofay Jun 23 '11 at 02:52
  • ahh ok, this is the path to phantomjs `/rubyprograms/search/phantomjs/bin/phantomjs.app/Contents/MacOS/phantomjs`... exactly, I executed the instructions from inside my app – user730569 Jun 23 '11 at 02:55
  • ok I've figured it out. Thanks!!!! God help me when I have to do this on linux haha, this was just for local development – user730569 Jun 23 '11 at 03:04
5

add this line to this file /home/optiman2/.bashrc

PATH=/home/optiman2/phantomjs/bin:$PATH

this worked for me.

and remember to use this command, before test phantomjs: source .bashrc

kleopatra
  • 51,061
  • 28
  • 99
  • 211
Brian Sanchez
  • 832
  • 1
  • 13
  • 11
1

FYI to Windows users upgrading to version 2.0.0 - the executable has moved. In addition to changing your PATH environment variable to 2.0.0, you'll need to add \bin to the end of the PATH that you had for the 1.x.x.

Jack Collins
  • 1,145
  • 10
  • 21
1

Mac PATH suggested setup:

  1. Open Terminal.
  2. Type vi ~/.bash_profile and hit enter (this opens or creates your bash_profile where you can customize Terminal commands).
  3. Press i to enter insert/edit mode then type alias phantomjs='~/PATH/TO/phantomjs' and be sure to replace ~/PATH/TO/phantomjs with something like ~/Documents/PhantomJS/bin/phantomsj or wherever the file exists on your machine.
  4. Press esc to exit insert/edit mode then type :x and hit enter to save the file.
  5. Quit and re-open Terminal.
  6. Type phantomjs and hit enter. If you see the prompt phantomjs> then you're all set.
  7. Type phantom.exit() to end the PhantomJS program.
  8. Now you can navigate to any folder and run PhantomJS by simply typing phantomjs. This is helpful when saving screenshots because the images will be saved inside the folder that's active in your Terminal.
Trev14
  • 3,626
  • 2
  • 31
  • 40
0

Whatever command is just a executable file. To be accessible by type its name directly, you have to put it into a path that system will look for that file directly.
For linux/OSX, it's /usr/bin or /usr/local/bin. Which really works? Well, it depends...
So what worked for me is extract the 'phantomjs.tar.bz2' source file to somewhere and copy the executable file to /usr/local/bin like this:

cp path-of-extracted-phantomjs/bin/phantomjs /usr/local/bin

Jennie Ji
  • 769
  • 5
  • 13