3

I noticed that in macOS, the /usr/local/Cellar folder contains binaries installed using Homebrew, and that /usr/local/opt has symbolic links to a bunch of those binaries. I haven't used symbolic links much, but as I understand it, the purpose of them is to create a link in a convenient location to a file/folder that acts as a copy of that file/folder, without having to move that file/folder from its original location.

So my questions are:

  1. Am I correct in my understanding of symbolic links there?

  2. If so, why would you create symbolic links in one folder (/usr/local/opt) to binaries in another folder (/usr/local/Cellar) that is at a similar location/depth in the file structure? Under what circumstances would you use these symbolic links where you couldn't just use the binaries?

3 Answers 3

1

Am I correct in my understanding of symbolic links there?

Yes. Symbolic link, as the name implies acts as a link to a file/folder located elsewhere. Accessing the link is generally equivalent to accessing the file/folder.

If so, why would you create symbolic links in one folder (/usr/local/opt) to binaries in another folder (/usr/local/Cellar) that is at a similar location/depth in the file structure?

If you'll carefully observer in this particular case, the binaries corresponding to all the various installed formulae are in different folders. There's a separate folder corresponding to each installed formula under /usr/local/Cellar. Within the folder for formula is a folder containing version number, followed by installed files for the formula.

Via symlink under /usr/local/opt, the installation location for all the formula are brought in one place.

For knowing the exact purpose of why /usr/local/opt folder is created by Homebrew, you can refer to this post on Stack Overflow:

1

I’m not that familiar with Homebrew, but the pattern you are describing is one I’ve seen other software installers use: it makes various binaries available under one directory, while making it easy to tell what software package they come from. The screenshot below is from an article about Homebrew, and shows a listing of the directory “/usr/local/opt”, with the various symbolic links pointing to subdirectories of “/usr/local/Cellar”. While most of the binaries point to a package with the same name, this is for example not the case for “npm”, where the symbolic link makes it easy to tell that it is part of the same package as “node” and “nodejs”. Symbolic links are used here for filesystem organization purposes: all of the files of a package are kept together in one place, while making them available from other directories as needed.

enter image description here

You may also be interested in an example of how symbolic links are sometimes used without even pointing to a different directory at all. Take a look at the output of ls -l /usr/bin | grep vim, which should show various links pointing to “vim” in the same directory, for example “ex” and “view”. As the man page for “vim” explains:

       Vim behaves differently, depending on the name of the command (the exe-
       cutable may still be the same file).

       vim       The "normal" way, everything is default.

       ex        Start in Ex mode.  Go to Normal mode with the ":vi"  command.
                 Can also be done with the "-e" argument.

       view      Start  in read-only mode.  You will be protected from writing
                 the files.  Can also be done with the "-R" argument.
0

The most common reason is "canonical-ize" the original file/binary. This could mean placing binaries in a canonical location — in this case, symlinks are placed in /usr/local/opt because your computer expects to find binaries there but not necessarily in /usr/local/Cellar (likely based on your shell $PATH variable). It could also be used to designate a single binary in a group of related binaries as "official" while keeping the other related binaries around but not immediately available — for instance, you might have installed many packages, each relying on a different version of Python, while you have specifically installed the latest version of Python for yourself. In this case, you might have binaries python34, python35, python36 etc in your Cellar folder, and a python binary in /usr/local/opt that links to the Python binary you requested be installed, python36.

You must log in to answer this question.

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