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.
Pingback: How to use GNU sed on Mac OS 10.10+, 'brew install -default-names' no longer supported - The Citrus Report
Pingback: sed in-place flag that works both on Mac (BSD) and Linux
Pingback: 【Mac】Mac OS X 安装GNU命令行工具 - 算法网
Pingback: Mac OS X : dircolor를 찾을 수 없습니까? 사용 .zshrc하면 - How IT
Pingback: Mac OS X 유틸리티를 GNU 핵심 유틸리티로 바꾸는 방법은 무엇입니까? 차이점이 있음을 - How IT
it would be great to see one line comment on each packagge to see what it offers! Thanks
Pingback: How to use GNU sed on Mac OS 10.10+, 'brew install -default-names' no longer supported - iZZiSwift
–with-default-names does not work anymore
Pingback: 记录自己常用的一些 Linux Shell 脚本 | 隔叶黄莺 Yanbin Blog - 软件编程实践
Pingback: Programe mais rápido automatizando comandos repetitivos
Pingback: macos - OS X 10.9.2: la comprobación de si el compilador de C no funciona...
Pingback: Automatically Conceal Sender's IP Address in Email Clients via SSH Tunneling - Top Bug Net
Pingback: Using GNU utils on Macs for Makefile compatibility | Net Consonance
Could you possibly give the commands to revert all the changes? How to remove coreutils and switch back to macOS coreutils.
Just run
brew uninstall
for all these packages…Pingback: OS X 10.9.2: vérifier si le compilateur C fonctionne ... non
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 🙂
Done. Thanks!
Pingback: macOS安装GNU命令行工具 – To Be A Linux Pro
Thanks for collecting this all on one page. Very helpful!
Looks like now Homebrew add “g” prefix instead “gs”:
Homebrew link moved.
{code}ruby -e “$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)”
Whoops, the Homebrew installer has moved! Please instead run:
ruby -e “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)”
Also, please ask wherever you got this link from to update it to the above.
Thanks!
{code}
Updated, thanks!
Great post; that ‘g’ was killing me and – of course – I didn’t read the fine(?) print.
how to install valgrind
sudo yum install valgrind
My hero! Pity there are so many steps. It would be nice if there was a metapackage that just turned your Mac into Linux then broke an iPad over its knee for good measure.
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.
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.
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?
Yes, your example is correct.
Thank you so much for clean and nice writing!
I can’t find my .bashrc
Then you need to create it
How?
touch ~/.bashrc
I’ve already tried that, but it just creates a TextEdit file (Or at least opens the file in TextEdit)
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.
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?
You missed a space after “touch”.
thank you very much! Now I can use GNU Screen 4.2.1 instead of system screen 4.0.0.
Thank you! I started compiling from source until I stumbled across this post. +1
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
Could you post an issue here: https://github.com/Homebrew/homebrew/issues?state=open
This is actually a question on Homebrew.
A one-liner for your ~/.profile to get manpages working. https://gist.github.com/quickshiftin/9130153
I’ve put this at the end of update. Thanks!
@xuhdev:disqus
Hi, The command (first) to install brew fails, could you please update it to the correct one:
ruby -e “$(curl -fsSL https://raw.github.com/mxcl/homebrew/go/install)”
Many thanks!
Updated. Thanks!
Does this replace os x only command line options?
@xuhdev:disqus Do you have any idea why Apple isn’t bundling the GNU tools?
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.
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.
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.
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).
Thanks! I’ll add this info to this post.
Done!
how to install GNU hostname ?
Hi Fabricio, Actually I didn’t see any advantages of GNU hostname over the BSD one. Did you notice anything different?
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.
how to update command line ‘date’ ??
What do you mean by “update”?
`date’ is included in coreutils.
谢啦,用了–default-names选项之后有遇到什么副作用吗?
Dear Hong, thank you so much for this post! This is a lifesaver as I’m using your install method to update my mac’s command line tools. I’m using it to study for this course: https://www.coursera.org/course/startup
It’s pretty awesome class btw 🙂
@fenomalist You’re welcome. I would like to remind you that the link in your post actually requires a login…
Corrected!
it worked for me, for the installation itself i found: http://eldar.cz/kangaroo/binarni-sxizofrenie/howto-make-macosx-well-behaved-unix.html
@galadgriel:disqus I didn’t quite understand your point….