30

I'm trying to install this library called phantomjs.

The instructions include this line:

http://code.google.com/p/phantomjs/wiki/BuildInstructions

For convenience, copy the executable bin/phantomjs.app/Contents/MacOS/phantomjs to some directory in your PATH.

How do I do this?

4
  • I don't think you'd need to worry about putting that in your path as bin is already in it. But if you wanted to, you can find how to achieve that here: troubleshooters.com/linux/prepostpath.htm
    – user10355
    Feb 24, 2012 at 3:37
  • @cksum but this is a relative path; we don't know what comes before the "bin".
    – bneely
    Feb 24, 2012 at 21:07
  • @bneely Yes, but there is no ~/bin. The only locations are /bin, /usr/bin and /usr/local/bin. All of which are in your path already. If you run path you'll see the following by default: PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin I haven't look through the install script of the program in question, but it would be extremely unlikely to install into it's own 'bin' folder somewhere. And you can't add relative paths AFAIK. I'm inclined to think just a typo from a sloppy guide.
    – user10355
    Feb 25, 2012 at 1:01
  • I know relative paths can't be added; I'm stating that the path in the question is a relative path because it doesn't start with a / or a ~ .
    – bneely
    Feb 25, 2012 at 1:02

4 Answers 4

19

To find out what's in your PATH, open a Terminal window and run this command: echo $PATH . The output is a colon-separated list of directories, the contents of which you can run without specifying the full path.

Since /usr/bin is in my path, I can run the w command simply by typing w instead of the full /usr/bin/w.

Also, you don't have to move the executable into one of the listed PATH directories. Other options include

  • Leave the executable where it is, and symlink to it from one of the PATH directories
  • Add a directory to your PATH by explicitly setting it in a login script for your shell
2
  • Please, follow @Gerry's advice in the other answer, and symlink it. It is the cleanest way!
    – cregox
    Sep 26, 2014 at 18:03
  • If, by any chance, you need a full directory in the PATH, for there are many executables in it you need access to, there isn't any other option and that's what the PATH is for. Currently we use the ~/.bash_profile to add to path.
    – cregox
    Sep 26, 2014 at 20:19
28

The cleanest way to achieve this would be to symlink the binary in the /usr/local/bin directory (which is included in PATH by default). You might need to create this directory if it doesn't exist yet. You can check to see if these directories are already in your PATH by opening Terminal.app and typing:

echo $PATH

This will generate a colon delimited listing of all directories in your PATH.

If the directories /usr/local or /usr/local/bin do not exist yet, execute the following:

sudo mkdir -p /usr/local/bin

Symlink the binary, replace (/Applications) with the path to phantomjs.app if it differs:

sudo ln -s /Applications/phantomjs.app/Contents/MacOS/phantomjs /usr/local/bin

Now you should have no problems executing phantomjs from the command-line.

1
  • +1, ls /usr/local/bin/ || sudo mkdir /usr/local/bin/ ; sudo ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql Sep 7, 2016 at 16:57
5

While symlinking the file from another directoy works, I prefer to actually copy/move the file to usr/local/bin, to not have it "twice".

cp bin/phantomjs.app/Contents/MacOS/phantomjs /usr/local/bin
3

Going back to the root of your issue, if you are installing phantomjs you can use npm to make it easier.

If you do then you can just run:

npm install -g phantomjs-prebuilt

which will install the binary inside of the proper bin directory. (the -g flag indicates to npm that the package is to be installed globally)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .