In this article we will show you how to use Homebrew, the popular package manager for MacOS or Linux.
Getting started
Installing Homebrew on Mac
In order to get Homebrew installed on your machine, you will have to run the following command:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Once installed, to check that everything is in order you can do so by running:
$ brew --version
Homebrew 3.6.12
Homebrew/homebrew-core (git revision cea59c6ec84; last commit 2022-11-22)
Homebrew/homebrew-cask (git revision c0c877a95b; last commit 2022-11-22)
If you get a similar output, you have now Homebrew installed on your machine.
Update brew
It is highly recommended that you update homebrew periodically. If you need to do so, it is as simple as this:
brew update
As you can see, it’s very simple and easy to guess. Let’s dive into packages now!
Installing packages (formulas)
Installing packages in Homebrew is very straightforward, you just need to know the name of the package (or “formula” as Homebrew calls them). Even if you are not sure about its name, most of the times Homebrew will suggest the right formula for you!
Let’s install Java using brew
, the command line executable that Homebrew provides.
brew install java
This command will download the latest Java version and automatically make it available on your path. If you check the Java version from your command line it should match the version downloaded by Homebrew.
$ java -version
openjdk version "19.0.1" 2022-10-18
OpenJDK Runtime Environment Zulu19.30+11-CA (build 19.0.1+10)
OpenJDK 64-Bit Server VM Zulu19.30+11-CA (build 19.0.1+10, mixed mode, sharing)
If the output you get is similar to what is shown above and the version matches with the version downloaded by brew
, everything worked as expected.
What if we need to install a specific version? Let’s see how to search for formulas first.
Searching package versions
Before installing a specific version of a formula, we will need to know what versions are available through brew
or brew [command] --cask
. Cask is a brew extension where you can find some formulas that are not available in the standard brew repository.
We will use openjdk
formula in this case, a specific JDK vendor, because its format will make it easier to understand how brew works. If we want to check the versions available, we can run the following:
$ brew search openjdk
==> Formulae
openjdk ✔ openjdk@11 ✔ openjdk@17 openjdk@8 openj9 openvdb
==> Casks
adoptopenjdk adoptopenjdk8 microsoft-openjdk microsoft-openjdk11 openkey
As you can see, it will show you the versions available for that formula. As simple as that!
You will be able to notice that there are some suggestions under Casks
section too. You can find similar packages in cask. Let’s show another example for a different vendor, Zulu:
$ brew search zulu
==> Formulae
zurl
==> Casks
shopify/shopify/zulu-jdk11 shopify/shopify/zulu-jdk14 shopify/shopify/zulu-jdk17 zulu zulu15 zulu8
shopify/shopify/zulu-jdk12 shopify/shopify/zulu-jdk15 shopify/shopify/zulu-jdk7 zulu11 zulu17 zulufx
shopify/shopify/zulu-jdk13 shopify/shopify/zulu-jdk16 shopify/shopify/zulu-jdk8 zulu13 zulu7
In this case, all the existing zulu packages are available as cask packages. It’d be clearer then if we search for them in this way:
brew search zulu --cask
==> Casks
shopify/shopify/zulu-jdk11 shopify/shopify/zulu-jdk14 shopify/shopify/zulu-jdk17 zulu zulu15 zulu8
shopify/shopify/zulu-jdk12 shopify/shopify/zulu-jdk15 shopify/shopify/zulu-jdk7 zulu11 zulu17 zulufx
shopify/shopify/zulu-jdk13 shopify/shopify/zulu-jdk16 shopify/shopify/zulu-jdk8 zulu13 zulu7
To search for cask packages you just have to append --cask
to your command.
Installing a specific version
If for any reason you don’t want to install the latest version, for example if your current project uses a previous Java version, then it’d make sense that you download the specific version used in your project. How can we do that?
We saw in the previous section how to list the available versions for a formula. Assuming we have the same list of openjdk
versions listed above, in order to install a specific version we just have to run the following then:
brew install openjdk@11
In a similar way to what we saw earlier, to install a zulu cask package we would have to run the following:
brew install zulu17
We can also run:
brew install zulu17 --cask
You now know how to install any package in Homebrew!
Upgrading packages
It’s also highly recommended to upgrade your packages to avoid using old versions of packages that could have security vulnerabilities. In order to do so, you just need to run the following:
brew upgrade
If you haven’t upgraded your packages in a while, this could take a few minutes to complete.
Conclusion
In this article we have learned how to use Homebrew as a package manager for OSX, this is a very good tool to manage your packages. However, in the case we want to manage multiple versions of the jdk, we would highly recommend that you use SDKMan instead. SDKMan allows a much simpler handling of multiple versions of any sdk. You can find a short tutorial about SDKMan in our article “How to Use SDKMan”.
We have arrived to the end of our journey together today. We really hope you’ve liked our article and enjoyed your time reading it.
Please follow us if you like our articles. You can do so by introducing your email in the section at the top of this page at the right-hand side.
Thanks for reading us! See you soon!
You must log in to post a comment.