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
-
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.
- Open Spotlight
-
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.
-
Install
coreutils
using Homebrew:- Once Homebrew is installed, type the following command into the Terminal and press
Enter
:brew install coreutils
- Once Homebrew is installed, type the following command into the Terminal and press
-
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
export
ing 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.
- In the Terminal, type the following command and press
-
Apply the changes:
- To apply the changes immediately without restarting the Terminal, type the following command and press
Enter
:source ~/.zshrc
- To apply the changes immediately without restarting the Terminal, type the following command and press
-
Test the changes:
- Now, you can use the GNU utilities without the "g" prefix. For example, instead of using
gcp
, you can simply usecp
. - 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-stylecp -r
.
- Now, you can use the GNU utilities without the "g" prefix. For example, instead of using
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.
Today you must do
echo 'export PATH="/opt/homebrew/opt/coreutils/libexec/gnubin:$PATH"' >> ~/.zshrc
instead of the path written above.