Upgrading grep in OS X

This Article Is Outdated

The information in this article is outdated and may contain information which is now incorrect. A newer version of this article is now available.

By default OS X (10.7 in this case) comes installed with grep version 2.5.1. Unfortunately if you want to use the --exclude-dir option (which you probably will if you are using any type of VCS) then you will have to upgrade it, since this is a feature of grep version >= 2.5.1a. If you are using macports or fink then the solution is simple, install via the command line.

Recently, I have been using homebrew. Today I found out that the use of homebrew to install already-bundled system applications (like grep) is discouraged and unsupported, except with certain special cases. Due to the way homebrew recipes work, however, this does not stop us from making our own. In this case, I found one on the web. To install the latest grep via homebrew:

brew install xz #required for installing grep through homebrew
brew install https://raw.github.com/adamv/homebrew-alt/master/duplicates/grep.rb

After grep is installed via homebrew do a grep --version. If you are still showing version 2.5.1 (which you should be) then you need to update your paths. For some reason in OS X, /usr/bin comes before /usr/local/bin, and for our version of grep to be found by default we need to rearrange this. From what I read on stack overflow this is pretty harmless.

There is a file called /etc/paths which lists paths one per line, if you have administrator rights and you don’t feel guilty about modifying system files you can rearrange this file to put /usr/local/bin in front of /usr/bin.

The other option is the more obvious per-user change in ~/.bashrc or ~/.profile.

For more information and recipes not included in homebrew by default, check out the homebrew-alt repository on github.

blog comments powered by Disqus