How to Install Java on Mac (Homebrew + pkg)

The fastest way to install Java on a Mac is with Homebrew. If you do not use Homebrew, the Adoptium Temurin .pkg installer is equally straightforward. Both are free and set up JAVA_HOME automatically.

Method 1: Homebrew (recommended)

# Install Homebrew first if you haven't: brew.sh
brew install openjdk@21

After install, Homebrew prints a symlink command β€” run it so macOS's java_home utility detects the JDK:

sudo ln -sfn $(brew --prefix)/opt/openjdk@21/libexec/openjdk.jdk \
    /Library/Java/JavaVirtualMachines/openjdk-21.jdk

Set JAVA_HOME in your shell config (~/.zshrc for zsh, the default on macOS Catalina+):

echo 'export JAVA_HOME=$(/usr/libexec/java_home -v 21)' >> ~/.zshrc
source ~/.zshrc
java -version

Method 2: Adoptium Temurin .pkg

  1. Go to adoptium.net > Latest LTS.
  2. Select macOS and pick the right architecture:
    • aarch64 β€” Apple Silicon (M1, M2, M3, M4)
    • x64 β€” Intel Mac
  3. Download the .pkg file and double-click it.
  4. Follow the installer. It installs into /Library/Java/JavaVirtualMachines/.
  5. Verify: open a new Terminal and run java -version.

Apple Silicon vs Intel

Apple Silicon Macs (M1 and later) need the aarch64 build for native performance. The x64 build runs under Rosetta 2 emulation and is noticeably slower for compilation-heavy workloads. Homebrew on Apple Silicon installs at /opt/homebrew (not /usr/local) β€” the $(brew --prefix) expansion in the commands above handles this automatically.

Check your architecture:

uname -m
# arm64 = Apple Silicon, x86_64 = Intel

Verify the installation

java -version
javac -version
echo $JAVA_HOME

All three should print sensible output. If javac -version fails, you installed a JRE instead of a JDK β€” rerun with the JDK option on adoptium.net or brew install openjdk@21 (Homebrew installs JDK by default).

Multiple Java versions on Mac

Install additional versions with brew install openjdk@17 etc., symlink each one, then switch with:

export JAVA_HOME=$(/usr/libexec/java_home -v 17)   # switch to 17
export JAVA_HOME=$(/usr/libexec/java_home -v 21)   # switch to 21

List all installed JDKs:

/usr/libexec/java_home -V

Tools like jenv or SDKMAN! automate per-directory switching if you juggle many versions.