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

Last updated on December 13, 2017

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 https://raw.githubusercontent.com/Homebrew/install/master/install)"

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

(All the brew install commands below have been put into one script on GitHub for downloads.)
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.

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

  1. Pingback: How to use GNU sed on Mac OS 10.10+, 'brew install -default-names' no longer supported - The Citrus Report

  2. Pingback: sed in-place flag that works both on Mac (BSD) and Linux

  3. Pingback: 【Mac】Mac OS X 安装GNU命令行工具 - 算法网

  4. Pingback: Mac OS X : dircolor를 찾을 수 없습니까? 사용 .zshrc하면 - How IT

  5. Pingback: Mac OS X 유틸리티를 GNU 핵심 유틸리티로 바꾸는 방법은 무엇입니까? 차이점이 있음을 - How IT

  6. Pingback: How to use GNU sed on Mac OS 10.10+, 'brew install -default-names' no longer supported - iZZiSwift

  7. Pingback: 记录自己常用的一些 Linux Shell 脚本 | 隔叶黄莺 Yanbin Blog - 软件编程实践

  8. Pingback: Programe mais rápido automatizando comandos repetitivos

  9. Pingback: macos - OS X 10.9.2: la comprobación de si el compilador de C no funciona...

  10. Pingback: Automatically Conceal Sender's IP Address in Email Clients via SSH Tunneling - Top Bug Net

  11. Pingback: Using GNU utils on Macs for Makefile compatibility | Net Consonance

  12. Ankit Jain

    Could you possibly give the commands to revert all the changes? How to remove coreutils and switch back to macOS coreutils.

    Reply
  13. Pingback: OS X 10.9.2: vérifier si le compilateur C fonctionne ... non

  14. Rob C

    Thank you for putting this information together – is there any chance you could move update #2 up to the top of the post?

    It would have made life a little easier for me, I manually typed half the commands, then realised I should just make a script, then found there already was one 🙂

    Reply
  15. Pingback: macOS安装GNU命令行工具 – To Be A Linux Pro

  16. 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.

    Reply
    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.

      Reply
      1. Jeff

        Thanks for the post. Could you explain in a little more detail what `export PATH=”$(brew –prefix coreutils)/libexec/gnubin:/usr/local/bin:$PATH”` does? For example, by having this in the path, will typing `ls` without the `g` prefix access the GNU version, while if it is not in the path will the BSD version be accessed?

        Reply
          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.

          3. Manuel

            I can’t find .bashcr either and when I type the command you gave into the console I get
            -bash: touch~/.bashrc: No such file or directory

            Can you give a hint about what is wrong here?

  17. 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:
    /usr/local/bin/brew
    /usr/local/Library/…
    /usr/local/share/man/man1/brew.1

    ==> The following directories will be made group
    writable:
    /usr/local/.
    /usr/local/bin
    /usr/local/include
    /usr/local/lib
    /usr/local/lib/pkgconfig
    /usr/local/share
    /usr/local/share/man
    /usr/local/share/man/man1

    ==> The following directories will have their group set
    to admin:
    /usr/local/.
    /usr/local/bin
    /usr/local/include
    /usr/local/lib
    /usr/local/lib/pkgconfig
    /usr/local/share
    /usr/local/share/man
    /usr/local/share/man/man1

    Reply
    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.

      Reply
      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.

        Reply
        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.

          Reply
  18. 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).

    Reply
      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.

        Reply

Leave a Reply

Your email address will not be published. Required fields are marked *