Ubuntu apt Install Java: Choose and Set Up a JDK
In this article
To install Java on Ubuntu with APT, open a terminal and run sudo apt update, then install a Java Development Kit with sudo apt install default-jdk. This installs Ubuntu’s default JDK package from your enabled software repositories; verify it with java -version and javac -version.
What APT Installs
APT is Ubuntu’s package-management tool: it downloads packages from configured repositories, checks their dependencies, and installs updates in a coordinated way. The command default-jdk is a convenient choice when you want the Java runtime and development tools without selecting a specific Java release. The exact version it provides depends on your Ubuntu release and repository configuration.
A JDK, or Java Development Kit, includes the Java runtime plus tools such as javac, the compiler used to build Java source files. If you only need to run an application, a Java runtime may be sufficient, but installing a JDK is often the practical choice for development and also lets you run Java programs.
Use sudo because installing system packages requires administrator privileges. apt update refreshes the local list of package versions; it does not upgrade all installed software. The install command then retrieves the package and any dependencies. Review APT’s proposed changes before confirming, particularly on a machine where package changes need to be controlled.
Choose a Java Version
If a project does not specify a Java release, begin with default-jdk. If its documentation requires a particular release, install that version when it is available in your Ubuntu repositories. For example, a package may be named openjdk-21-jdk or openjdk-17-jdk; package availability varies by Ubuntu release, so check before relying on a name.
Search the repositories enabled on your system with:
apt search openjdkto find matching package names and descriptions.apt policy openjdk-21-jdkto inspect whether that package has a candidate version from your configured sources.
Then install the package that matches the project’s requirement, for example:
sudo apt install openjdk-21-jdk
Use the actual package name available for your Ubuntu release. A package not appearing in APT’s results may be absent from the enabled repositories, rather than evidence that Java itself is unavailable. Avoid adding an unfamiliar repository simply to obtain a newer version unless you understand its maintenance and security implications.
Install and Verify Java
- Open Terminal.
- Refresh package information:
sudo apt update. - Install a JDK:
sudo apt install default-jdk, or install a version-specific JDK available for your release. - Check the runtime:
java -version. - Check the compiler:
javac -version.
The version output should identify the Java release now selected by your system. If java works but javac is reported as missing, you may have installed only a runtime package; install a JDK package. Conversely, a successful javac -version confirms that the compiler is available, but it does not prove that a particular project will build: the project may require another Java release or additional build tools such as Maven or Gradle.
Manage Multiple Java Versions
It is possible to have more than one Java version installed. Ubuntu’s alternatives system selects which executable is used when you type java or javac. To see installed choices and select one interactively, use:
sudo update-alternatives --config javasudo update-alternatives --config javac
The runtime and compiler selections should generally correspond to the same JDK version. Check both commands afterward. If the alternatives command reports no choices, that command may not be registered for multiple installed versions; first confirm which JDK packages are installed.
Some development tools also look for JAVA_HOME, an environment variable pointing to a JDK installation directory. It is not always required for launching Java, and its value should match the selected JDK. Find the executable path with readlink -f "$(command -v java)"; the JDK home is normally the parent directory above bin/java. Configure the variable only when a tool or project asks for it, and follow the project’s instructions for making the setting persistent.
APT Compared with Other Installation Methods
For most Ubuntu systems, APT is the simplest starting point: packages are integrated with the operating system’s package database and can be updated or removed through the same package manager. APT installs from the repositories configured for the system, so the available releases and update schedule depend on the Ubuntu version and repository sources.
Other methods can suit particular needs. A project may distribute a container image or specify a vendor-supported JDK; a developer may use a version manager to switch Java versions per project. Those approaches can offer more control, but they also introduce separate update and configuration responsibilities. Don’t assume that an online command using curl or wget is equivalent to an Ubuntu package: check the download’s source, integrity instructions, and ongoing update process before running it.
Common Installation Problems
- “Unable to locate package”: Run
sudo apt update, check the package spelling, and confirm that the package exists for your Ubuntu release withapt search openjdk. Repository configuration can affect results. - “java: command not found”: Confirm that the install completed successfully, then try
java -versionagain in a new terminal. Check installed packages withdpkg -l '*openjdk*'. - The wrong version appears: Inspect installed alternatives using
sudo update-alternatives --config javaand select the required version. Checkjavacseparately if compiling. - A project rejects the installed version: Read its build instructions for the required Java release. A newer JDK is not automatically compatible with every project or plugin.
Keep Java patched through your normal Ubuntu package updates. If APT reports that packages are held back or proposes removing important software, read the proposed changes rather than forcing installation commands blindly.
Frequently Asked Questions
What is the command to install Java on Ubuntu?
Run sudo apt update, followed by sudo apt install default-jdk. For a project-specific release, install the corresponding versioned JDK package if it is available in your repositories.
Does sudo apt install java work?
Not reliably as a general instruction. Ubuntu package names are specific; use default-jdk for the default development kit or a package such as openjdk-21-jdk when that version is available and appropriate.
How do I check whether Java is installed?
Run java -version. To check whether development tools are installed, run javac -version as well.
Should I install a JRE or JDK?
Install a JDK if you will write, compile, or build Java software; it includes the compiler and runtime. If you only run an application, a runtime may be enough, but follow the application’s requirements.
How do I uninstall Java installed with APT?
Remove the specific package you installed, such as sudo apt remove openjdk-21-jdk. Substitute the actual package name; review APT’s proposed removals before confirming, especially if other applications depend on Java.