Skip to content

Instantly share code, notes, and snippets.

@indiesquidge
Last active December 14, 2023 14:45
Show Gist options
  • Save indiesquidge/ec010eca3ffa254788c2 to your computer and use it in GitHub Desktop.
Save indiesquidge/ec010eca3ffa254788c2 to your computer and use it in GitHub Desktop.
How to and Best of Homebrew

Homebrew

How To

Homebrew is a package management system for OS X. You can read more about it here, or simply run

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

to install it.

First things first, you need to familiarize yourself with some of the basic homebrew commands.

All commands start with the binary brew.

brew search

If you are looking for something in particular, you can first search for it to see if homebrew has a formulae for what you're looking for. For example, running brew search tree will return

mvptree   pstree    tree      treecc    treeline
brew install

Once you find the formulae you're looking for, you can run brew install tree.

other important commands
  • brew list - List all installed formulae
  • brew update – Fetch the newest version of Homebrew and all formulae form GitHub using git.
  • brew upgrade [formulae] – Upgrade outdated formulae. This can take an optional argument if you only want to update one formulae
  • brew uninstall <formulae> – uninstall a specific formulae

There are a myriad of other commands and tasks you can run that we won't discuss in detail here. Running man brew or reading the documentation linked on Homebrew's main page.

Best Of

Now that you know how Homebrew functions, let's get into so of the best packages to install.

Right off the bat, brew is useful for things like git, vim, zsh, tmux, and many more base level tools that a lot of developers normally already have. We're going to look at some lesser known packages, but just know that Homebrew is getting larger, and almost anything that you would want that runs in the terminal is available as a homebrew package. If it isn't, it's incredibly easy to make it yourself. More on this later.

brew install hub

This is easily one of my favorites. You can think of hub as all the same functionality of git, but with add-ons that make it compatible with GitHub.

When you're deep in the codebase and realize that you haven't commited any of your hard work to GitHub, it's annoying to have to jump over to your browser, surf to https://github.com, create a new repo, etc. That context switch just pulled you away from your important work and forced you to interact with – gulp – a GUI interface. How unfitting.

This is where hub comes into play. Important tasks that you were once only able to do through GitHub are now available via the terminal. Want to create a new remote repo and automatically set GitHub as the origin? hub create. Want to open a pull request? hub pull-request. Want to view your remote repo on GitHub from any sub-directory? hub browse. Of course this is not an exhaustive list of commands for hub.

Use hub --help or man hub for more information, customizing flags, and other goodies.

brew install the_silver_searcher

A quick and dirty way to search through any file structure for a key word or phrase.

You're in your Rails app and need to find all instances of current_user? ag current_user. ag is used for two reasons: 1) it would be awful to type the_silver_seracher before everything you want to search, 2) AG is the symbol for the chemical element silver. A common flag includes running the search with --context, which will print out the lines before and after the key phrase.

Some of you may be thinking, yeah, but I already have grep (or ack) which serves the same purpose. Try benchmarking ag vs. grep, you will find that ag is insurmountable when it comes to performance; grep may not even be able to run a search if the scope is too large.

Use ag --help or man ag for more information.

brew install tree

Lists the contents of directories in a tree-like format.

This one you kind of just have to try for yourself. It's not as crazy of a tool as some of the others, but if you want to see all files within a certain set of directories, tree does a great job of parsing it into a beautiful, readable format.

More info with tree --help or man tree (I'm going to stop referencing these "more infos" now since they are the same for nearly all packages).

brew install cask

Provides a friendly homebrew-style CLI workflow for the administration of Mac applications distributed as binaries.

Okay, this might be the coolest one I have found. This extends brew past the scope of the terminal and into your GUI applications. For example, you want to install Spotify. Instead of going to your browser to https://www.spotify.com, clicking "Download Spotify", opening the zipped pacakge, etc., all you have to run is cask install spotify, and boom, it will be downloaded to your computer without any hassle. Not only is this amazing because it takes away having to search for all the apps you want, but it also keeps those apps in one location (opt/homebrew-cask/Caskroom/), making it easier to update, remove, and be aware of what you have installed on your machine.

Obviously this list is just a start, and I am constantly finding new and amazing packages to increase my productivity, but this gives a glimpse as just how amazing and powerful Homebrew is, and why it should be part of every developer's arsenal of tools.

Keep It Clean

With all this awesomeness, it's easy to forget that these tools need to be polished and take care of to perform correctly. I'll make it easy for you with a few aliases I created:

alias brewup='brew update; brew upgrade; brew prune; brew cleanup; brew doctor'

brewup will make it easy to keep homebrew neat. The first two commands I explained earlier. The third command, brew prune, will remove dead symlinks from the Homebrew prefix. This may sound confusing, and generally isn't needed, but it's never a bad idea to run. brew cleanup will remove any older versions of packages that you have, keeping things up to date. And last, brew doctor will check your system for potential problems. If no problems exist, you should see Your system is ready to brew after the doctor command runs.

Brew It Yourself

Homebrew formulae are simple Ruby scripts. Homebrew's GitHub page is very well documented on how to contribute your own formulae in the rare case that you can't find what you're looking for. Read more on this here

@testacode
Copy link

Thanks!

@SheehanTX
Copy link

brew prune is deprecated, so you can remove it from your alias.

Otherwise, thanks for this!

@dlwalter
Copy link

brew upgrade can wreck your entire dev environment. I would not recommend running that command without specifying the formula.

@manroygood
Copy link

I've been running the suggested brewup alias, but it has stopped working. During the brew upgrade stage, it upgrades the first formula, then gives the message brew cleanup` has not been run in 30 days, running now... After this it spits out a bunch of error messages and quits. Then I have to manually upgrade each formula.

@4rc0s
Copy link

4rc0s commented May 5, 2019

@manroygood brew prune isn't a command any longer. I'd recommend just calling

alias brewup='brew update; brew upgrade; brew cleanup'

@dannybs1980
Copy link

Good article.. thanks

@Ilana-Polonsky
Copy link

Thank you!

@B1-Bloom
Copy link

@manroygood brew prune isn't a command any longer. I'd recommend just calling

alias brewup='brew update; brew upgrade; brew cleanup'

You can still access prune with brew cleanup --prune-prefix or brew cleanup --prune=all

@rileytwo
Copy link

I've been running the suggested brewup alias, but it has stopped working. During the brew upgrade stage, it upgrades the first formula, then gives the message brew cleanup` has not been run in 30 days, running now... After this it spits out a bunch of error messages and quits. Then I have to manually upgrade each formula.

@manroygood have you ran brew doctor? Running it will show this

Please note that these warnings are just used to help the Homebrew maintainers
with debugging if you file an issue. If everything you use Homebrew for is
working fine: please don't worry or file an issue; just ignore this. Thanks!

and then some warnings and errors if there's anything wrong. I like to run brew cleanup -s every few days, just to keep things clean.

brew cleanup is automatically ran with brew upgrade if a formulae is upgraded, or every 30 days--unless the environment variable HOMEBREW_NO_INSTALL_CLEANUP is set, e.g. export HOMEBREW_NO_INSTALL_CLEANUP=1 is in one of your shell profiles. I'd check the shell profiles (if any) in your home directory and see if any of them are exporting HOMEBREW_NO_INSTALL_CLEANUP=1!

When I installed rvm, it put a script in each one of my shell profiles (and created the profiles that didn't exist) that sourced rvm's path so it was loaded in my shell. I don't know of anything that sets HOMEBREW_NO_INSTALL_CLEANUP for you, but it's worth checking out, just in case :)

@manroygood
Copy link

I grepped "grep HOMEBREW_NO_INSTALL_CLEANUP .*" and it returned nothing.

The problem remains. Brew upgrade will tell me "==> Upgrading 5 outdated packages:" and then will upgrade one or two packages before quitting with an error message.

@Alyetama
Copy link

I like the "brewup" alias. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment