Homebrew Gives You the Best of Linux on Mac


Summary

  • macOS commands often differ from their Linux versions due to BSD vs. GNU inconsistency.
  • Homebrew simplifies the installation of missing command-line software on macOS.
  • Homebrew helps manage the PATH environment for installed commands, avoiding duplicates and ensuring correct priority.

Open up Terminal on macOS, and it’s easy to imagine you’re running in a full Linux environment. Those commands aren’t exactly what you’re used to, though. Fortunately, there’s an easy fix.

What’s the Problem With macOS?

Since the launch of OSX in 2001, macOS has shipped with a suite of command-line utilities that will be familiar to anyone who has used Linux: grep, find, vim, and many more. These are usually the BSD versions of each program, instead of the GNU versions that most Linux distributions ship with, but they are largely compatible.

However, Apple’s use of BSD software rather than GNU causes two issues. First, some of the BSD versions work slightly differently, with alternative options, unexpected behavior, or unfamiliar extensions. You can usually resolve these issues by reading the appropriate man pages or simply adapting to BSD command behavior. But these inconsistencies can be awkward to manage, especially if you’re dual-booting or otherwise using a combination of Mac and Linux systems.

Related


BSD: What Is It, and How Is It Different From Linux?

The ABCs of BSD.

Sometimes, these differences are due to the age of the tools; the Mac BSD versions tend to be older than their GNU equivalents. Apple doesn’t always update these commands very often, either, so they can get further out-of-date between OS releases.

A more significant issue is that macOS does not ship with an equivalent version of every command you may be used to. This won’t always be a problem, but there are some surprising omissions, like the tree tool for example.

The solution to this problem used to be to build from source: download the actual program code, run a configuration script, then use the make command to compile everything. Oh, and pray that it all works correctly in your environment.

Alternatively, you could try to find a pre-compiled binary for your architecture, but this process can be tedious and compiled software may be incompatible with your environment. However, nowadays, there are nicer ways of getting your macOS commands up to scratch.

How to Get Hold of Missing Commands

Since 2016, Homebrew has offered the best way of installing command-line software on your Mac. Homebrew is a package manager that lets you install software without having to compile it from source. Like any package manager, Homebrew takes care of compilation and installation. It also handles the network of dependencies that a program might require to run, so it’s a lot easier than building from source.

You can use Homebrew to install a wide range of programs, not just those limited to the Mac App Store. This has opened up the Mac ecosystem to many of the open-source apps that Linux users are used to. Many free software developers are discouraged by the closed nature of Apple’s store, either for practical or ideological reasons. Homebrew has opened up access to more quality, free software than ever before.

The current version—4.4—supports mac OS releases Ventura (13), Sonoma (14), and Sequoia (15).

Once you’ve installed Homebrew, adding missing commands is a breeze. To install the wget program, for example, use this command:

        brew install wget
A Mac terminal running the brew command to install wget.

If that’s still too complicated, you can use a Homebrew GUI like Cork instead. But, let’s be realistic: if you’re looking to install and use command-line software, you’ll be more than comfortable using the brew command.

On a Silicon Mac, Homebrew installs programs into /opt/homebrew/bin. It should also add that location to your PATH, which will therefore look something like:

        ...:/opt/homebrew/bin:.../usr/local/bin:...:/bin:...

In other words, Homebrew-installed commands will take priority, running instead of any duplicates provided by the system in /bin, for example.

Related


What Is the Unix PATH, and How Do You Add Programs To it?

The Unix PATH is an environment variable that tells the shell which directories it should look for when you issue commands.

You can use the which command to check the location of any program, e.g.

        $ which find
/usr/bin/find

The -a option will show you all locations of a command in your PATH, so you can use it to check for duplicates and verify that you’re using the correct version:

        $ which -a [...]

Remember that most commands accept a –version or –help option that can tell you which version you’re using. Another important detail is that the GNU tools are often installed with a different name to avoid clashes. For example, the Homebrew package for sed is called gnu-sed, and it installs as gsed:

Installing gsed using Homebrew and confirming the gsed binary is installed.

The BSD version of sed doesn’t really have a version number, but its man page in Sequoia dates it to June 2020.

        brew install coreutils

Related


How to Install Homebrew on a Mac

Homebrew is a package manager that makes installing open-source utilities and full-blown apps as easy as typing a few commands into Terminal.

Missing Software to Look Out For

The first time I realized something was a bit “off” with macOS’s tooling was when I reached for the trusty file utility, tree. Now, tree isn’t essential, but it’s incredibly useful. The command shows the full hierarchy of a directory, printing all files and folders beneath it, like a supercharged ls. Except that it won’t do that on a vanilla macOS install:

A terminal showing a "command not found" error when trying to run the tree command.

I used to solve this with a rudimentary shell script to perform the same function. But that script lacked all of tree’s options, which are surprisingly plentiful:

The man page for the tree command showing it supports tens of options.

Homebrew lets you install tree, providing the full command in all its glory. This is the latest version of tree: 2.2.1 which was released in November 2024.

Another very common program that is conspicuous in its absence is Emacs. True, Emacs is often missing from modern Linux distros too, but that doesn’t make its omission from macOS any more defensible.

The GNU Emacs text editor open in a terminal.

Emacs is a popular text editor and a powerful alternative to vim, so you may want to use it or try it out if you haven’t previously. Fortunately, Emacs is just the other side of a quick brew install emacs.

The find command is one of many that, while available on macOS, acts a little bit differently in its BSD incarnation. The two versions support slightly different options and behavior, making it difficult to switch between them.

Homebrew’s find command is part of the findutils package, so you can install it like this:

        brew install findutils

You should be aware that Homebrew installs the command as gfind. Homebrew often uses this convention of prefixing GNU-equivalents of built-in commands with a “g” so both versions can coexist:

mac-which-find-gfind

This is to ensure the default BSD find takes priority, but it can be confusing and awkward to remember. If you want to use GNU find by default, you can add this directory to your PATH:

        /opt/homebrew/opt/findutils/libexec/gnubin

That directory should have a symbolic link named “find” which points to the “gfind” executable in /opt/homebrew/bin. Remember to add it before /usr/bin in your path to ensure your shell finds the GNU version first.


Homebrew lets you install vital commands, including GNU equivalents you may be more used to, that offer enhanced or alternative behavior.



Source link

Previous articleQuordle hints and answers for Sunday, April 20 (game #1182)