Install and Use GNU Command Line Tools on macOS/OS X

If you are moving onto macOS/OS X from GNU/Linux, you would probably find out that the command line tools shipped with OS X are not as powerful and easy to use as the tools in Linux. The reason is that macOS/Mac OS X uses the BSD version command line tools, which are different from the Linux version, while they are both compliant with POSIX standards. But we can easily install the GNU command line tools by using Homebrew in Mac OS X and set them as default.

Note: you need to notice that you may have some compatibility issues with shell scripts written specifically for OS X after you have replaced your OS X commands with the GNU version. Although the very vast majority of shell scripts have no problem, you just need to be aware that when there comes a problem, this may be the spot to check on.

Install Homebrew

First, visit Homebrew homepage and follow the installation instructions to install Homebrew.

Shortcut: install the latest XCode and then run the following command to install:

/usr/bin/ruby -e "$(curl -fsSL"

Then add the following line to your .bashrc or .zshrc:

export PATH="$(brew --prefix coreutils)/libexec/gnubin:/usr/local/bin:$PATH"

Install the GNU Command Line Tools

First comes the most important one — GNU Coreutils:

brew install coreutils

GNU Coreutils contains the most essential UNIX commands, such as ls, cat.

Then you may probably want to install the following ones (For some of the packages, you need to run brew tap homebrew/dupes first, but only once for your system):

brew install binutils
brew install diffutils
brew install ed --with-default-names
brew install findutils --with-default-names
brew install gawk
brew install gnu-indent --with-default-names
brew install gnu-sed --with-default-names
brew install gnu-tar --with-default-names
brew install gnu-which --with-default-names
brew install gnutls
brew install grep --with-default-names
brew install gzip
brew install screen
brew install watch
brew install wdiff --with-gettext
brew install wget

The --default-names option will prevent Homebrew from prepending a g to each of the newly installed commands, thus we could use these commands as default commands over the ones shipped by OS X.

In addition, some GNU command line tools already exist by default on OS X, but you may want a newer version:

brew install bash
brew install emacs
brew install gdb  # gdb requires further actions to make it work. See `brew info gdb`.
brew install gpatch
brew install less
brew install m4
brew install make
brew install nano

As a complementary set of packages, the following ones are not from GNU, but you can install and use a newer version instead of the version shipped by OS X:

brew install file-formula
brew install git
brew install openssh
brew install perl
brew install python
brew install rsync
brew install svn
brew install unzip
brew install vim --override-system-vi
brew install macvim --override-system-vim --custom-system-icons
brew install zsh

Now you should have an easier command line system in your OS X. Have fun with them!

Update 1: You may also want to add $HOMEBREW_PREFIX/opt/coreutils/libexec/gnuman to the MANPATH environmental variable, where $HOMEBREW_PREFIX is the prefix of Homebrew, which is /usr/local by default. (Thanks Matthew Walker!) Alternatively, there is also a one-line setup which you could put in your shell configuration files here by quickshiftin.

Update 2 All the brew install commands above have been put into one script on GitHub for downloads.

44 thoughts on “Install and Use GNU Command Line Tools on macOS/OS X

  1. Marvin R.

    I really like the idea of replacing BSD commands with GNU ones… just one question: Does this break some programs? Are there any regular programs which rely on BSD commands on OS X? This guide could also break shell scripts which are written specific for OS X. You should at least warn about this.

    1. Hong Xu

      If you don’t add `$(brew –prefix coreutils)/libexec/gnubin` into your PATH variable, there should be no problem, as all the commands are compiled with ‘g’ prefix.

      There aren’t compatibility issues I met for the coreutils though. Generally GNU commands provides more than BSD versions, so if your script is written for BSD systems, it is very likely to be compatible with GNU. But you are right, adding a compatibility warning doesn’t hurt.

          1. Neelu S

            I’ve already tried that, but it just creates a TextEdit file (Or at least opens the file in TextEdit)

          2. alex

            A .bashrc file is a text file. That’s why the article tells you to add some text to it. D’oh. 🙂 You can open it with wathever text editor you want.

  2. Kosta

    When I run the first command to install homebrew it shows the messages below. The security changes that the script wants to implement make me nervous. The group owner for /usr/local is wheel which is not the same as admin. root is the only member of wheel.

    ==> This script will install:

    ==> The following directories will be made group

    ==> The following directories will have their group set
    to admin:

    1. Hong Xu

      Because OS X is mainly BSD based — the same reason why FreeBSD/OpenBSD/NetBSD does not use GNU tools by default. Another reason that Apple bundles many outdated GNU software (bash, gdb, etc.) is that the new GPLv3 doesn’t allow Apple to do so, while GPLv2 is fine with this behavior. After many GNU projects upgraded to GPL v3, Apple won’t be able to bundle them any more.

      1. Bospaddenstoel

        Which is a bit of a pathetic move from GPL. Blocking every OS X user from using a few great tools out of spite for Apple not being open source.

        1. alex

          This is absurd. Apple doesn’t want to ship GPL3 software because the “tivoization clauses”, not because is not open source. Get your facts straight before insulting others.

          Bonus: Apple blocks EVERYONE of using their OS if you don’t buy their hardware and don’t adhere to draconian rules, but the bad guys are the FSF for trying to give EVERYONE the right to use software. Nice twist of the reality, man.

  3. Matthew Walker

    Oh, this information was much appreciated at my end!

    Just to add a little to your post, homebrew also recommended I add a directory to the MANPATH to get the appropriate man pages (the path, in my case, was /usr/local/opt/coreutils/libexec/gnuman).

      1. Fabricio Figueiredo

        Yeap! I was needed to use “hostname -i” so I could retrieve local IP address but I found a workaround and changed my script to use ‘ifconfig’ instead.


Leave a Reply

Your email address will not be published.