Overview

One of the first things I do when using a new Mac is to set up development tools. Rather than jumping all over the internet and downloading all these separately, I’ve come to know a nice command line utility called Homebrew.
Now I know this isn’t exactly breaking news–a lot of people have messed around with Homebrew.  However, I wanted to give a nice quick guide to get set up with a couple of the more common development tools needed for Java development for that first time user.

Homebrew and Dev Tools Installation

Make sure you fire up the Mac OS X Terminal in the /Applications/Utilities directory to run all the commands in this listing.  During certain steps you may be asked to agree to terms or use your administrator password.

Install Prerequisites

Before installing Homebrew, you must have XCode Command Line Tools installed.  If you already have XCode installed, you can skip this step.  To note, the terminal command will also show you a dialog to install the complete XCode if desired.

# install xcode command utilities
xcode-select --install

Install Homebrew

Here’s the install command for Homebrew (be sure to scroll right if necessary).  In case you’re late to this blog, you can double check it at the Hombrew site listed in the Resources section.

# install command - do NOT sudo this command
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Next run Homebrew doctor to ensure everything is installed correctly.

brew doctor

If it says ‘Your system is ready to brew’, then you’re good to go.

Install Cask

Cask is an extension built onto Homebrew which allows for installing traditional Mac installable applications like Google Chrome, TextWrangler, etc.  You can check the Cask website in the Resources section for more information and to search for applications available to install.

brew install caskroom/cask/brew-cask

I listed some of the more useful commands for Homebrew and Homebrew Cask at the bottom of this blog, for now we’ll continue to install the rest of the packages.

Set up Bash Profile

Setting up your bash profile will allow you to set up environment variables and set up auto-completion when the terminal is opened.  We’ll perform this in one step for all the packages we’ll install after.
Either fire up your favorite text editor or you can use the simple command line accessible editor ‘pico’ to edit. The key commands to save and quit the editor will be shown at the bottom of the screen.

pico ~/.bash_profile

This will either create a new Bash profile or create a new one.
Now, add the following contents.

#!/bin/bash
# setup Java home directory
export JAVA_HOME=`/usr/libexec/java_home`
# OPTIONAL: install Cask apps in standard Applications directory
export HOMEBREW_CASK_OPTS="--appdir=/Applications"
# for loading homebrew bash completion scripts
if [ -f $(brew --prefix)/etc/bash_completion ]; then
. $(brew --prefix)/etc/bash_completion
fi

NOTE: make sure that the top line declaration “#!/bin/bash” is only added once to this file. If your .bash_profile file already exists, then it may already be there.
The location of JAVA_HOME will ensure that you point to your latest installed Java JDK version.  PLEASE NOTE that your terminal on restart or on opening tab will complain about Java missing until it is fully installed.
The optional HOMEBREW_CASK_OPTS variable is for installing Cask applications in the standard Applications folder.  Normally Cask will install these separately in the user’s own Applications directory.  If you find yourself using Spotlight or another command line launcher utility then it really doesn’t matter if you use this.  However, some users like the Applications installed in only one location.  In that case, use this optional variable.
After completion, you’ll end up with the following file (substituting YOUR_NAME_HERE with your name):

/Users/YOUR_NAME_HERE/.bash_profile

We will test autocompletion after installing Git and Spring CLI.

Install Dev Packages

Now, let’s install all the development packages and applications using Homebrew, Cask and a little bit of editing.

Bash Completion

I love autocomplete in the terminal.  Bash completion allows for a number of different applications to participate in auto-completion.  Rather than installing this along with those applications, we’ll install it ahead of time.

brew install bash-completion

After installing, the ‘Caveats’ note section explains that additional commands should be added to your bash profile which we have already done.

Git

First, check and note your current version of Git that is executed on command line which will be the version that Mac OS X uses by default.

git --version

Next, install Git.

brew install git

To check that Git has been updated and auto-completion is working, open a new tab or restart the terminal so that it’ll be reloaded with Git completions.  When you type ‘git’ followed by a space and click tab twice, you should see list of Git commands.

# recheck the installed version of Git
git --version
# check for autocompletion
git [hit tab key twice]

Java

The procedure for installing Java on Mac has changed over the years.  This will give you a simple way to install the Java Development Kit (JDK) using Cask.

brew cask install java

And that’s it!  We have already set up the JAVA_HOME variable necessary to properly use your Java environment and Maven earlier.
The location of JAVA_HOME will ensure that you point to your latest installed Java JDK version.
Again, restart your terminal — the terminal will no longer complain about missing Java, the Java version command should work and you’ll see a full path for JAVA_HOME.

# show version of Java
java -version
# show location of JAVA_HOME variable
echo $JAVA_HOME

Maven

Next, install the Maven build package.

brew install maven

If everything is set up correctly, you’ll see output and locations for ‘Maven home’, ‘Java version’ and ‘Java home’ after version command.

mvn --version

Spring Boot CLI

If you use Spring for Java and Groovy development, then you’ll want to install the Spring Boot CLI which will allow you to easily create Spring Boot projects on the command line.

# set up the location of where to install - the Pivotal brew tap
brew tap pivotal/tap
# install springboot
brew install springboot
# check version after installation
spring --version

To check autocompletion, open a new tab or restart the terminal.  When you type ‘spring’ followed by a space and click tab twice, you should see list of spring commands.

spring [hit tab key twice]

Assuming autocomplete is set up correctly, you should see a list of the git commands to choose from.
That covers the dev tools to install, next we’ll cover some maintenance for brew.

Updating HomeBrew

I run through four commands for the brew updating process.  I always run through these before installing new programs through Brew and Cask.

# Update Homebrew formulae:
brew update
# To see list of outdated packages you have installed
brew outdated
# Assuming there are any outdated packages, go ahead and upgrade them:
brew upgrade --all
# Lastly, to check if there are any problems, re-run the doctor:
brew doctor

There are a lot more things that can be done with Brew like cleaning out the old versions of files, cleaning caches, etc that are beyond the scope of this blog (thank goodness for the scope excuse).

Some Useful Cask Commands

As promised, here’s some additional useful Cask commands.  Google Chrome is used in the following examples.

# search for applications - all matches will show
brew cask search google
# info about the application
brew cask info google-chrome
# typical application install
brew cask install google-chrome
# delete application:
brew cask uninstall google-chrome

Next Steps

In my next blog post I will show how to use the development environment you just set up.

Resources

Homebrew: http://brew.sh
Cask: http://caskroom.io