Skip to content

Instantly share code, notes, and snippets.

@andreajparker
Created September 24, 2023 22:50
Show Gist options
  • Save andreajparker/ed3b15fd670caa6e4f7e4da18ce398ac to your computer and use it in GitHub Desktop.
Save andreajparker/ed3b15fd670caa6e4f7e4da18ce398ac to your computer and use it in GitHub Desktop.
2023-09-24 – Install `coreutils` using Homebrew on MacOS and setting up GNU utils to be used instead of the Mac-flavored utils

Here's a step-by-step guide for a brand new Mac user to install coreutils using Homebrew and then modify their PATH to use the GNU utilities without the "g" prefix:


Step-by-Step Guide to Installing coreutils on a Mac

  1. Open Terminal:

    • Open Spotlight
      • Method A to open Spotlight: Click on the magnifying glass icon, Spotlight, in the top right corner of your screen.
      • Method B to open Spotlight:Or press and hold the command key ⌘ and then press and release the spacebar to bring up Spotlight.)
    • Type "Terminal" and press Enter to open the Terminal application.
  2. Install Homebrew (if not already installed):

    • Homebrew is a package manager for macOS that allows you to easily install software. Homebrew's homepage, from which we take the bash-based command in the next step, is displayed on the Homebrew homepage: https://brew.sh/
    • ⚠️ Never run shell scripts or other flavors of scripts that you find on the internet without reading through what the scripts do before you run them.⚠️
    • To install Homebrew, paste the following command into the Terminal and press Enter:
      /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
    • Follow the on-screen instructions to complete the installation.
  3. Install coreutils using Homebrew:

    • Once Homebrew is installed, type the following command into the Terminal and press Enter:
      brew install coreutils
  4. Modify your PATH to use GNU utilities without the "g" prefix:

    • In the Terminal, type the following command and press Enter:
      echo 'export PATH="/usr/local/opt/coreutils/libexec/gnubin:$PATH"' >> ~/.zshrc
    • This command appends the new path to your .zshrc file, which is executed every time you open a new Terminal session. This ensures that the GNU utilities are prioritized over the macOS default utilities.
    • If you're wondering why we're exporting this path to our .zshrc file instead of our .bashrc file it is because Apple announced the switch from Bash to Zsh (Z shell) as the default shell in macOS in June 2019 during the Worldwide Developers Conference. This change was then implemented in macOS Catalina (version 10.15), which was released in October 2019. Prior to macOS Catalina, Bash had been the default shell since the introduction of Mac OS X. The switch to Zsh was primarily due to licensing concerns with newer versions of Bash, which is licensed under the GNU General Public License version 3 (GPLv3), while Zsh uses the more permissive MIT License.
  5. Apply the changes:

    • To apply the changes immediately without restarting the Terminal, type the following command and press Enter:
      source ~/.zshrc
  6. Test the changes:

    • Now, you can use the GNU utilities without the "g" prefix. For example, instead of using gcp, you can simply use cp.
    • As mentioned in your example, on macOS, you'd typically use cp -R to copy directories recursively. However, with the GNU utilities installed, you can now use the Linux-style cp -r.

That's it! You've successfully installed coreutils on your Mac and modified your PATH to use the GNU utilities without the "g" prefix. This should help you avoid the idiosyncrasies of the macOS versions of these utilities.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment