How to Use Brew to Install Java on Mac
In this article we show how to install Java on Mac using Homebrew, and how to allow to switch between different versions such as Java8, Java11, Java13 and latest Java version.
Pre-requisites
Before we start, make sure you have Homebrew installed on your Mac. If not, you can install it via:
$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
If you already have brew
installed, make sure you have the latest version by running the following command in a terminal
$ brew update
Next, install Homebrew Cask
$ brew tap homebrew/cask-versions
$ brew update
$ brew tap caskroom/cask
Install Latest Version of Java Using Brew
To install the latest version of Java, all you need to do is:
$ brew install --cask adoptopenjdk
This command installs the AdoptOpenJDK package, which provides prebuilt OpenJDK binaries.
You will be prompted to enter your password. Provide your admin password to authorize the installation.
Brew will then download and install the Java package. The process might take a few minutes depending on your internet speed.
Once the installation is complete, you can verify if Java is installed correctly by running the following command:
$ java -version
Install Specific Versions of Java (Java8, Java11, Java13)
To install a specific version of Java using Brew, you can use the adoptopenjdk
formula with a version specifier. Here’s how you can do it:
1 - Open Terminal on your Mac.
2 - Update Brew to ensure you have the latest version by running the following command:
$ brew update
3 - Search for the available versions of AdoptOpenJDK by running the following command:
$ brew search adoptopenjdk
This will list all the available versions of AdoptOpenJDK that you can install.
4 - Choose the specific version you want to install. For example, if you want to install AdoptOpenJDK version 11, you can run the following command:
$ brew install --cask adoptopenjdk@11
Replace 11
with the desired version number.
5 - Brew will then download and install the specified version of Java. The installation process may take some time.
6 - Once the installation is complete, you can verify the installed Java version by running the following command:
$ java -version
That’s it! You have successfully installed a specific version of Java on your Mac using Brew. Now you can use that version for development or running Java-based applications.