Skip to content

Install OpenJDK

Zhamri Che Ani edited this page Nov 19, 2025 · 11 revisions

Install OpenJDK 21 using Homebrew

1. Update Homebrew

brew update

2. Install OpenJDK 21

brew install openjdk@21

Link OpenJDK 21 to your system

macOS does not automatically link newer JDKs, so you must link it manually:

brew link --force --overwrite openjdk@21

If Homebrew refuses to link, use this alternative:

sudo ln -sfn /usr/local/opt/openjdk@21/libexec/openjdk.jdk \
/Library/Java/JavaVirtualMachines/openjdk-21.jdk

(Use /opt/homebrew instead of /usr/local if you're on Apple Silicon.)

Set JAVA_HOME

Add this to your shell config.

For zsh (~/.zshrc):

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

For bash (~/.bash_profile):

echo 'export JAVA_HOME="$(/usr/libexec/java_home -v 21)"' >> ~/.bash_profile
source ~/.bash_profile

Verify

java -version

Expected output:

openjdk version "21" 2023-09-19

Clone this wiki locally