66

When I download command line applications they are located in my ~/Downloads/ directory. But I feel this is wrong place to have a command line application located. Where in the Mac OS X filesystem should I store command line applications?

/Applications/ also feels wrong, it´s more for desktop applications. And /bin/ sounds more for applications that belongs to OS X.

3
  • I agree with your sentiment that putting things directly in /bin or /Applications is generally poor form. Patrix's answer is the most canonical location I've seen for general command line storage of programs and scripts.
    – bmike
    Commented Aug 11, 2013 at 17:49
  • Duplicate: apple.stackexchange.com/q/80902/20459
    – rm -rf
    Commented Aug 11, 2013 at 20:47
  • 3
    I don't see these as duplicate since the linked question seems more about juggling several third party choices and this is more a pure - where do my manually downloaded apps go.
    – bmike
    Commented Aug 11, 2013 at 22:30

2 Answers 2

65

If you don’t want to worry about updates, dependencies and paths, use Homebrew or MacPorts to install additional utilities. This will be the easiest approach by far.

If the binary is not available in Homebrew/MacPorts, or you want to manage things on your own, read on.

Because macOS comes from a Unix heritage, you may want to store system files in /usr/local/bin for command line applications and scripts that belong to the system locally and not to a specific user. Recent versions of macOS have the directory installed as part of the OS install, otherwise you can create it first by running:

sudo mkdir -p /usr/local/bin

You can move any command line application to that folder by running:

sudo mv my-binary /usr/local/bin/

/usr/local/bin already should be listed at the beginning of /etc/paths, but you can run the following to insert it as the first entry in case it's missing:

grep -w /usr/local/bin /etc/paths || \
    sudo sh -c 'echo -e "1i\n/usr/local/bin\n.\nw\nq" | ed /etc/paths'

If the software to be installed comes with a lot of support files, and you want to keep everything bundled together to make upgrades or removal easier, you can also choose to store the whole package in a sub-directory of /opt (-> /opt/foo-package). To make its binaries accessible via the shell, you can either

  • add the path of the binaries to /etc/paths.d/foo-package (i.e., as root, run echo /opt/foo-package/bin >> /etc/paths.d/foo-package) and restart your shell, or
  • symlink all binaries from /usr/local/bin (-> cd /usr/local/bin; sudo ln -s /opt/foo-package/bin/* .). This is basically what Homebrew does as well.

Some users make a second directory for user level scripts, but this is even more subject to personal preference.

mkdir -p ~/.local/bin

In this case, you'll want to have each user's path include this location by modifying the shell startup scripts (~/.bash_profile for bash, ~/.zshrc for zsh)

export PATH=~/.local/bin:$PATH
3
  • 2
    Starting with Yosemite, /usr/local/bin was moved to the beginning of PATH instead of the end of PATH. This allowed new commands, with the same name as old commands, to have precedence. Commented May 5 at 15:41
  • May be worth adding that this answer describes only the versions of Homebrew running on Intel.  (Not surprising since that's all there was when it was written!)  On ARM (Apple M1,…), Homebrew instead installs into /opt/homebrew, which must be added to the PATH as shown above.
    – gidds
    Commented May 7 at 10:34
  • 1
    @gidds The question and the answer is about manually installing command-line binaries in general. They can (and should) go into /usr/local/bin even on Apple Silicon, and for sure not into /opt/homebrew/bin.
    – nohillside
    Commented May 7 at 11:05
9

Consider creating an /opt directory, which is another location that custom Unix software would appear by convention.

3
  • 7
    /opt/ is very useful for complete applications whereas /usr/local/bin/ is more appropriate for standalone command line scripts. It is often preferable to use /opt/ in instances where a collection of related scripts act as a library (e.g., /opt/myscriptslib/bin/).
    – plasmid87
    Commented Aug 12, 2013 at 10:17
  • Thanx for your advices. I personally created the folder /opt/ to put my software and then I just made symbolic links to /usr/local/bin for all the executables... then no need to add the /opt/myspecific-soft in the path environment variable.
    – рüффп
    Commented Apr 5, 2015 at 18:42
  • Update: In macOS Sonoma 14, I did indeed find an existing /opt directory (empty). Commented Apr 27 at 7:26

You must log in to answer this question.

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