First, install Homebrew. Homebrew allows us to install and compile necessary software packages easily from the source.
It comes with a handy installation script:
Run (paste) the command above in your terminal. When it asks you to install XCode CommandLine Tools, say yes.
Once the installation is done:
$ brew update
$ brew doctor
You'll get Your system is ready to brew message on the console.