Packaging Github Projects using Homebrew
While it’s easy to package code in GitHub repos by setting up CI pipelines (like Travis CI, Github packages etc) and get the packages hosted on the specific package managers (like pip, npm etc), it’s a little harder to package repositories with code written in multiple languages. Sometimes you need to ship all the code into user machines and be able to work with it during or post the installation. I cracked one such problem for a project of mine using Homebrew formulas 🍺 .
Homebrew — Quick Intro
Homebrew is a package-manager that helps you install software on macOS and Linux. It’s just like apt-get or pip utilities. You’ll find most mac users choosing Homebrew for their installations mainly because it WORKS!!
Listing down a few pros of Homebrew.
- Very active project. Makes extensive use of GitHub to expand the support of several packages through user contributions.
- Installs into
/usr/localso does not need you to modifyPATHanywhere - Every installed package is cleanly sandboxed into its own cellar so you don’t have stray files all over your system, just symlinks from bin, man, etc.
- Super easy to package your own code and make it installable via brew 💛
Now the last point is what made me write this post. So let’s get started on packaging Github repos using Homebrew.
Step 1: Package your repo
First things first, you need to have a packaged version of your code. It could be anything ranging from a pip package, docker image or even just a zip. For my project I used a zip file as the packaging format. It has code in multiple languages and different builds, so zip was my only way to go. But if you have your code in one specific language, you should consider packaging it in the specific packaging format and then use that in the further steps. The reason why is, you want your package files to be as small as possible (not have tonnes of code just zipped up lazily) to make download quicker on machines. You have higher chance of people actually wanting/being able to use your package that way!
I used Github releases to get the tarball of my code. You should also be able to make releases for your repo easily from the releases tab. If you have a language specific code consider integrating with some CI service like Travis to have your packages created.
Now what happens with Github releases is that Github creates tagged versions of your project with links to the compressed file of code for those particular releases.
Now the zip or tar.gz are of importance to us here. We’ll use this in writing our homebrew formula — which are nothing but package descriptions that tell what to do with these tarballs on the installing machines.
Step 2: Get started with Brew formula
For a brew formula you’ll write a .rb (ruby) file. Refer to my formula.
You could start fresh and write it all by yourselves or make use of brew itself to create the file for you. I would suggest the latter. Refer to Homebrew documentation.
brew create https://example.com/foo-0.1.tar.gzHere for the tar.gz link you will use your GitHub release tar link.
Doing this will create a formula for you in
/usr/local/Homebrew/Library/Taps/homebrew/homebrew-core/Formula/foo.rbThe .rb is named foo matching the tar name.
This formula is directly placed in the homebrew-core formulas. Homebrew core is where all cool projects want their formulas to be. If your formula is in Homebrew core then it will get listed like this. But you’ll need to get it committed into the Homebrew repo. For which you have to have a website, good documentation for your project — basically the maintainers of Homebrew should be satisfied enough to make it a part of their core formuale! Once you make it to the core formulas, all brew users can directly install your package without having to tap to get your formula separately.
What are taps??
Detour. I just said taps. What are taps?
Homebrew has this amazing support wherein it helps people who want to quickly package their formula without going through the whole process of reviewing to make it to the core formulas.
And I used tap to make my project Homebrew installable.
Now everytime brew updates (which is pretty much everytime you run an install) it pulls in all new core formulas. Once a formula is in the user machine, then all they do is run “brew install <package-name>” to install the package. Now to get Third-party formulas into user machines we make use of tap.
Users can tap into a third-party formula and then run install for that package.
Get Sowmiya Nagarajan’s stories in your inbox
Join Medium for free to get updates from this writer.
Now how do you create taps, to write and put in your non-core formula?
Again, Github!
Step 3: Setup a tap
We have a sample formula we created in Step 2. We’ll create a tap and move that formula to the tap.
- To create a tap, all we need is a GitHub repo with “homebrew-” as prefix in the repo’s name — like this one.
- Now move the sample .rb created earlier into this repo and commit it.
- Now try
brew tap <username-or-orgname>/<tap-name>Here tap name is the repo’s name without the homebrew prefix. For example, if your tap repo is named as “homebrew-randomtap” then you should use “randomtap” as tap name.
I guess the homebrew prefix in repo names is for homebrew to keep track of third-party formulas.
Now as a result of this tap command you will have the formula in your local homebrew taps. Path like below.
/usr/local/Homebrew/Library/Taps/<username-or-orgname>/<tap-name>From here you can build changes on your formula and test it locally until you’re satisfied and then commit it to the tap repo for others to install using it.
Step 4: Write Brew Formula
Now let’s start editing the formula from our tap.
class Stackbox < Formula
desc "Create app stacks loaded with all your favourite clients,
services and infra along with code boilerplates in under
5 mins."
url "https://github.com/Stack-Box/StackBox/archive/v1.2.0.tar.gz"
sha256
"ea9bcc6905169868ac4f5ee54b7ace6ad8ed8416d482accbcf3fb7211027436b"depends_on "docker"
depends_on "docker-compose"
depends_on "python"
depends_on "shc"def install
system "mkdir -p /usr/local/Cellar/stackbox/1.2.0/bin"
system "cp -a . /usr/local/Cellar/stackbox/1.2.0/"
system "shc -f ./brew/stackbox-brew.sh -o stackbox"
system "cp ./stackbox /usr/local/Cellar/stackbox/1.2.0/bin/"
end
end
This is how my formula looks. Running over some key points.
- Put the url to your tarball in the url field.
- Find out the sha for your tar file and put that in the sha256 field (I usually just run an install and check the expected sha and replace it in the formula 😛)
- Figure out if your package needs any dependencies to install/run and define them using “depends_on”
- Then define the install block. You can use a make command, a configure script or just some cp move to get what you want to be done based on your use case.
For my install I’m simply creating an executable binary from a bash script using shc and copying all installation/run files (in my case everything in repo) to the /usr/local/Cellar/<package-name> path.
Anything within /bin in the cellar is an exec and in the executable path. So you can put your main executable in bin and refer to any files you need from there relatively.
Now it might take multiple runs to figure out if all the commands work and modify them if needed.
To debug and test, you can simply run
brew install <package-name>In my case the package name is same as the formula name “stackbox”. You will be able to debug your installation commands when you run this install.
Step 5: Commit formula changes to tap
So far you debugged using the local formula file. Just make sure to commit it back to the tap repo. Now you can ask your project fam to do just a tap and install to get your project up and running!
brew tap <username-or-orgname>/<tap-name>
brew install <package-name>That’s the end. Find some links to useful references below. Byee 👋
— Authored by Sowmiya Nagarajan








