Using Custom Homebrew Formula

If you use homebrew to manage packages in macOS, you might notice that some formulas come with several versions. Take elasticsearch for example, $ brew search elasticsearch outputs two versions:

elasticsearch  [email protected]

The one without version suffix tag is the current version (5.3.1 as of this writing). But what if you want to install 2.3.3 for example? as you might be working on a project with such requirement.

The good thing is, you can write your own formula, and install it like:

$ brew install /path-to-formula/[email protected]

Better, you can just copy the original @2.3.3 formula from homebrew core git repository. See the current elasticsearch formula, which looks something like:

1
2
3
4
5
6
7
class Elasticsearch < Formula
  desc "Distributed search & analytics engine"
  homepage "https://www.elastic.co/products/elasticsearch"
  url "https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.3.1.tar.gz"
  sha256 "1c277102bedf58d8e0f029b5eecc415260a4ad49442cf8265d6ed7adc0021269"
  # ...
end

Then you can browse the git commit history, and look for version 2.3.3 commit. Download that into /path-to-formula/[email protected].

You need to modify the formula a little bit, to change its class name to add AT233 (version) suffix:

1
class ElasticsearchAT233 < Formula

Then you can do:

$ brew install /path-to-formula/[email protected]

Services

If a formula also comes with a service, brew services list will look like:

elasticsearch       stopped      
[email protected] started zul  /usr/local/opt/[email protected]/[email protected]

You can start, stop and run the service by appending the version suffix:

$ brew services start [email protected]

See [email protected] formula for another example.