Freebsd – Role of the /usr/local directory in FreeBSD

directory-structurefreebsd

Contrary to the Linuxes I used, in FreeBSD the /usr/local directory is heavily populated by a normal installation, even without using any ports. In fact, non-basic shells (Bash and Z-Shell) are put there (in /usr/local/bin).

Under Linux it was nice to have custom-built scripts or software in the /usr/local tree for them to be clearly separated from the distribution software (e.g. to easily “deactivate” these “modifications”, by taking /usr/local out of the $PATH).

What is the reasoning behind this? And since I doubt that there is a way to make FreeBSD behave like Linux, what would be the best practices to install custom-build software and files accessible for all users?

Best Answer

Under Linux it was nice to have custom-built scripts or software in the /usr/local tree for them to be clearly separated from the distribution software

And that is exactly what you are getting on FreeBSD. Shells like the Z shell and the Bourne Again shell are not part of FreeBSD. They are third-party additions. The operating system is sometimes referred to by the slang name "base". In the BSD world in general, third party additions on top of "base" do not live in /usr. They live in /usr/local.

In the BSD world — and this is true of other BSD operating systems like OpenBSD — you get the operating itself in / and /usr, and the stuff that isn't the operating system in /usr/local. If one wants just the operating system functionality without the additions, one takes /usr/local out of consideration in what one is doing.

The slight twist to this is that FreeBSD derivatives like TrueOS Server and TrueOS Desktop modestly consider their additions on top of FreeBSD to be not part of the operating system. So there's a whole load of TrueOS out-of-the-box stuff that lives in /usr/local with the non-operating-system stuff. For example: It's where you'll find PCDM, the TrueOS display manager, living.

Conversely, /usr/local is where all custom-built softwares that are not parts of the operating system go.

To show how strong this division is:

  • The Mewburn rc scripts for non-operating-system stuff go in /usr/local/etc/rc.d/ and do not get added to /etc/rc.d/. It's where you'll find /usr/local/etc/rc.d/nginx.
  • Non-operating-system configuration files go in /usr/local/etc/ not /etc/. It's where you'll find /usr/local/etc/cups.
  • There's a distinction between /usr/share/man where the operating system manuals are and /usr/local/man where the non-operating-system manuals are.

Even the package manager itself is (currently) not part of the operating system proper. There's a "bootstrap" package manager, pkg-static. This installs pkg, the actual package manager that has configuration files in /usr/local/etc/pkg and that is itself an add-on.

The conceptual leap that you have to make in coming from the Linux "distributions" world is that you don't get an operating system built by selecting from a mish-mash of packages supplied by a "distributor". You get a full operating system as one coherent unit (installed by an installer, upgraded with freebsd-update, and maintained as a single "boot environment" using ZFS), and all of the third-party stuff as ports and packages separate from that. If you yourself are supplying third-party stuff, be you a developer or a system administrator, then you do ports and packages too, or you just put it directly in /usr/local somehow.

On the other hand, custom-built softwares that are parts of the operating system go in / and /usr where the operating system lives. The source and build system for the entire operating system come supplied in /usr/src as part of this one self-sufficient system. You make local modifications there, share them with other people using Subversion (FreeBSD) and git (TrueOS) if you want, and rebuild either the "userland" alone or the entire operating system (both "shell" and "kernel") from that.

Interesting side note

If you nonetheless make your own structure for your own machines, you are, according to the operating system manual itself, expected to provide a local hier manual page superseding the operating system one. ☺

Further reading