{"id":8745,"date":"2018-03-26T19:12:16","date_gmt":"2018-03-26T19:12:16","guid":{"rendered":"https:\/\/www.hostinger.com\/tutorials\/?p=8745"},"modified":"2026-03-10T10:13:16","modified_gmt":"2026-03-10T10:13:16","slug":"install-java-ubuntu","status":"publish","type":"post","link":"\/uk\/tutorials\/install-java-ubuntu","title":{"rendered":"How to install Java on Ubuntu in 2026"},"content":{"rendered":"<?xml encoding=\"utf-8\" ?><p>Java is a versatile and popular object-oriented programming language known for its platform-independent nature. Installing Java on Ubuntu offers various development possibilities, from running web servers to building Android applications.<\/p><p>In this article, you&rsquo;ll learn how to install Java on an Ubuntu server. Whether you&rsquo;re new to Java programming or seeking to refine your existing setup, our guide provides you with all the necessary tools and knowledge for your forthcoming Java projects.<\/p><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"https:\/\/assets.hostinger.com\/content\/tutorials\/pdf\/Linux-Commands-Cheat-Sheet.pdf\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" width=\"1024\" height=\"283\" src=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2022\/11\/Linux-cheat-sheet-1024x283.png\" alt=\"\" class=\"wp-image-69262\" srcset=\"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2022\/11\/Linux-cheat-sheet-1536x425.png 1024w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2022\/11\/Linux-cheat-sheet-300x83.png 300w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2022\/11\/Linux-cheat-sheet-150x41.png 150w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2022\/11\/Linux-cheat-sheet-768x212.png 768w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2022\/11\/Linux-cheat-sheet.png 2048w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><\/div><p>\n\n\n\n<\/p><h2 class=\"wp-block-heading\" id=\"h-prerequisites-for-installing-java-on-ubuntu\">Prerequisites for Installing Java on Ubuntu<\/h2><p>Before you start installing Java on Ubuntu, ensure you meet the following prerequisites:<\/p><ul class=\"wp-block-list\">\n<li><strong>Familiarity with the command line<\/strong>. It&rsquo;s crucial to have a foundational knowledge of <a href=\"\/uk\/tutorials\/linux-commands\">Linux commands<\/a> and Ubuntu terminal navigation. This will facilitate the Java installation process and subsequent management tasks.<\/li>\n\n\n\n<li><strong>Access to an Ubuntu server<\/strong>. If you&rsquo;re using a <a href=\"\/uk\/tutorials\/what-is-vps-hosting\">virtual private server (VPS)<\/a>, ensure you have access with <strong>sudo <\/strong>privileges to execute administrative commands within the server. For enhanced stability and extended support, we recommend opting for recent long-term support (LTS) versions of Linux distributions, such as <strong>Ubuntu 22.04 LTS<\/strong>.<\/li>\n\n\n\n<li><strong>Java code understanding<\/strong>. While not necessary for installation, having a basic knowledge of Java coding is beneficial for those looking to develop Java-based applications. As one of the most popular programming languages, Java offers extensive documentation and community support to enhance your programming skills.<\/li>\n<\/ul><h2 class=\"wp-block-heading\" id=\"h-how-to-install-java-on-ubuntu\">How to Install Java on Ubuntu<\/h2><p>This section will explain how to install and configure Java for Ubuntu using a <a href=\"\/uk\/vps-hosting\">Hostinger VPS plan<\/a>.<\/p><?xml encoding=\"utf-8\" ?><figure class=\"wp-block-image size-large\"><a href=\"\/uk\/vps-hosting\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" width=\"1024\" height=\"300\" src=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/02\/VPS-hosting-banner-1024x300.png\" alt=\"\" class=\"wp-image-77934\" srcset=\"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2023\/02\/VPS-hosting-banner.png 1024w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2023\/02\/VPS-hosting-banner-300x88.png 300w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2023\/02\/VPS-hosting-banner-150x44.png 150w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2023\/02\/VPS-hosting-banner-768x225.png 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><h3 class=\"wp-block-heading\" id=\"h-1-update-the-package-index\">1. Update the Package Index<\/h3><p>For a smooth Java installation, ensure your Ubuntu system is up-to-date. Start by securely logging into your server using an <a href=\"\/uk\/tutorials\/how-to-use-putty-ssh\">SSH client like PuTTY<\/a> and entering your credentials to access the system.<\/p><p>Refresh your system&rsquo;s connection to the APT repository by updating the package indexes:<\/p><pre class=\"wp-block-preformatted\">sudo apt update\nsudo apt upgrade<\/pre><p>The <strong>sudo apt update<\/strong> command contacts the package repository to retrieve the latest list of available packages and their versions without installing or upgrading any software.<\/p><p>Following this, <strong>sudo apt upgrade <\/strong>installs the available updates, which may include security patches and improved versions of packages, ensuring your system&rsquo;s security and compatibility.<\/p><h3 class=\"wp-block-heading\" id=\"h-2-install-the-default-jre\">2. Install the Default JRE<\/h3><p>If you&rsquo;re primarily focused on running Java programs, you&rsquo;ll only need to install the Java Runtime Environment (JRE) on Ubuntu. Here&rsquo;s the command you should execute:<\/p><pre class=\"wp-block-preformatted\">sudo apt install default-jre<\/pre><p>Facilitated by the APT package manager, this command installs the default JRE package. This default version is chosen for its stability and comprehensive support within the Ubuntu distribution.<\/p><p>It includes the Java Virtual Machine (JVM), core classes, and supporting libraries required to run Java applications.<\/p><h3 class=\"wp-block-heading\" id=\"h-3-install-a-specific-java-jdk-version\">3. Install a Specific Java (JDK) Version<\/h3><p>For software development tasks, installing the Java Development Kit (JDK) on Ubuntu is essential. The JDK includes the JRE and key development tools like the Java compiler (<strong>javac<\/strong>) and debugger, which are necessary for building Java applications.<\/p><p><strong>Installing the Default JDK<\/strong><\/p><p>The OpenJDK package is recommended for most development needs due to its ease of installation and open-source licensing. To install the default version of JDK, use:<\/p><pre class=\"wp-block-preformatted\">sudo apt install default-jdk<\/pre><p><strong>Installing Specific Versions of OpenJDK<\/strong><\/p><p>You may need a specific Java version for compatibility purposes. For a particular OpenJDK installation on Ubuntu, first, search for the available versions with the following command:<\/p><pre class=\"wp-block-preformatted\">apt search openjdk<\/pre><p>Then, install your desired OpenJDK version, such as OpenJDK 11, using:<\/p><pre class=\"wp-block-preformatted\">sudo apt install openjdk-11-jdk<\/pre><p><strong>Installing Oracle JDK<\/strong><\/p><p>If your preference leans towards Oracle Java on Ubuntu due to its specific features or application compatibility, be aware that it requires manual installation, as Oracle JDK cannot be installed through package managers.<\/p><p>Oracle&rsquo;s JDK 21 is the latest long-term support release of the Java SE Platform as of this writing. To install Oracle JDK, follow these steps:<\/p><ol class=\"wp-block-list\">\n<li>Visit the <a href=\"https:\/\/www.oracle.com\/java\/technologies\/downloads\" target=\"_blank\" rel=\"noopener\">Oracle download page<\/a> to find the JDK version suitable for Ubuntu (<strong>x64 Compressed Archive<\/strong>). You can also obtain the official JDK Debian package here.<\/li>\n<\/ol><div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><a href=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2024\/04\/oracle-java-downloads-page-x64-compressed-archive-highlighted.png\"><img decoding=\"async\" width=\"1024\" height=\"579\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2024\/04\/oracle-java-downloads-page-x64-compressed-archive-highlighted-1024x579.png\" alt=\"Choosing the suitable JDK version for Ubuntu on the Oracle's official download page\" class=\"wp-image-107045\" srcset=\"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2024\/04\/oracle-java-downloads-page-x64-compressed-archive-highlighted-1024x579.png 1024w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2024\/04\/oracle-java-downloads-page-x64-compressed-archive-highlighted-300x170.png 300w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2024\/04\/oracle-java-downloads-page-x64-compressed-archive-highlighted-150x85.png 150w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2024\/04\/oracle-java-downloads-page-x64-compressed-archive-highlighted-768x434.png 768w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2024\/04\/oracle-java-downloads-page-x64-compressed-archive-highlighted.png 1400w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><\/div><ol start=\"2\" class=\"wp-block-list\">\n<li>After accepting the license agreement, download the <strong>.tar.gz<\/strong> file for your desired version.<\/li>\n\n\n\n<li>Use an <a href=\"\/uk\/tutorials\/ftp\/filezilla-ftp-configuration\">FTP client like FileZilla<\/a> or the <a href=\"\/uk\/tutorials\/using-scp-command-to-transfer-files\">scp command<\/a> to upload Oracle&rsquo;s JDK to your Ubuntu server. Choose a directory like <strong>\/tmp<\/strong> or<strong> \/opt<\/strong> for the upload. If opting for <strong>scp<\/strong>, use this command, replacing <strong>username <\/strong>and <strong>server_ip <\/strong>with your actual credentials:<\/li>\n<\/ol><pre class=\"wp-block-preformatted\">scp \/path\/to\/jdk-archive.tar.gz username@server_ip:\/tmp<\/pre><ol start=\"4\" class=\"wp-block-list\">\n<li>Navigate to the directory where you uploaded the file and extract it using:<\/li>\n<\/ol><pre class=\"wp-block-preformatted\">tar -xzf &lt;filename&gt;.tar.gz<\/pre><ol start=\"5\" class=\"wp-block-list\">\n<li>Move the extracted folder to a more permanent location, such as <strong>\/usr\/lib\/jvm\/<\/strong>, with:<\/li>\n<\/ol><pre class=\"wp-block-preformatted\">sudo mv &lt;extracted-folder-name&gt; \/usr\/lib\/jvm\/oracle-jdk-&lt;version&gt;<\/pre><h3 class=\"wp-block-heading\" id=\"h-4-verify-the-installation\">4. Verify the Installation<\/h3><p>After installing Java, it&rsquo;s crucial to confirm that the installation was successful and that your system is using the correct version.<\/p><p>To check the installed version of the JRE, run the following command:<\/p><pre class=\"wp-block-preformatted\">java -version<\/pre><p>To verify the Java Development Kit version, including the compiler, enter:<\/p><pre class=\"wp-block-preformatted\">javac -version<\/pre><p><strong>Output and Interpretation<\/strong><\/p><p>The output should display the version number and build information. For example:<\/p><pre class=\"wp-block-preformatted\">java version \"21.0.2\" 2024-01-15\nJava(TM) SE Runtime Environment (build 21.0.2+10-LTS)\nJava HotSpot(TM) 64-Bit Server VM (build 21.0.2+10-LTS, mixed mode)<\/pre><p>This confirms <strong>Java 21.0.2<\/strong> is the installed Java version, with <strong>mixed mode<\/strong> indicating the JVM&rsquo;s ability to run both just-in-time compiled and interpreted code, optimizing performance.<\/p><p><strong>Configuring the Default Java Version<\/strong><\/p><p>Ubuntu allows for multiple Java installations. If you have several installed, set the default version with the <strong>update-alternatives <\/strong>command:<\/p><pre class=\"wp-block-preformatted\">sudo update-alternatives --config java<\/pre><p>This command lists all installed Java versions, allowing you to perform the Ubuntu Java version switch by entering its selection number.<\/p><p><strong>Updating Java<\/strong><\/p><p>To update Java on Ubuntu, run the <strong>update <\/strong>and <strong>upgrade <\/strong>commands with <strong>apt<\/strong>:<\/p><pre class=\"wp-block-preformatted\">sudo apt update &amp;&amp; sudo apt upgrade<\/pre><p><strong>Removing\/Uninstalling Java<\/strong><\/p><p>To remove a particular version of Java, first identify its package name with the <a href=\"\/uk\/tutorials\/grep-command-in-linux-useful-examples\">grep command<\/a>:<\/p><pre class=\"wp-block-preformatted\">apt list --installed | grep java<\/pre><p>Then, use the <strong>apt remove<\/strong> command followed by the package name:<\/p><pre class=\"wp-block-preformatted\">sudo apt remove openjdk-21-jdk<\/pre><p>This removes the specified Java version but keeps configuration files. To completely remove Java and its configurations, use <strong>apt purge <\/strong>instead:<\/p><pre class=\"wp-block-preformatted\">sudo apt remove openjdk-21-jdk<\/pre><h3 class=\"wp-block-heading\" id=\"h-5-configure-the-java-home-environment-variable\">5. Configure the JAVA_HOME Environment Variable<\/h3><p>The <strong>JAVA_HOME<\/strong> environment variable is a crucial system setting that indicates the Java installation directory. This variable is used by Java-based applications and development tools to ensure their proper operation.<\/p><p>To configure <strong>JAVA_HOME<\/strong>, follow these steps:<\/p><ol class=\"wp-block-list\">\n<li>If Java was installed using <strong>apt<\/strong>, it is likely located in <strong>\/usr\/lib\/jvm\/<\/strong>. Use the following command to list the contents of this directory and identify the exact path to your Java version:<\/li>\n<\/ol><pre class=\"wp-block-preformatted\">ls \/usr\/lib\/jvm\/<\/pre><p>Write down the Java installation location that you intend to use as <strong>JAVA_HOME<\/strong>.<\/p><ol start=\"2\" class=\"wp-block-list\">\n<li>Edit your shell profile file to include the <strong>JAVA_HOME<\/strong> variable. This file is typically <strong>.bashrc<\/strong> for bash shell users or <strong>.profile <\/strong>for system-wide settings. Use the following command to edit <strong>.bashrc<\/strong> for a single user:<\/li>\n<\/ol><pre class=\"wp-block-preformatted\">nano ~\/.bashrc<\/pre><p>For system-wide changes, use this command instead:<\/p><pre class=\"wp-block-preformatted\">sudo nano \/etc\/profile<\/pre><ol start=\"3\" class=\"wp-block-list\">\n<li>At the end of the file, define <strong>JAVA_HOME<\/strong>, substituting <strong>\/path\/to\/java<\/strong> with the actual path you noted earlier. For example, if your installation path is <strong>\/usr\/lib\/jvm\/java-21-openjdk-amd64<\/strong>, add:<\/li>\n<\/ol><pre class=\"wp-block-preformatted\">export JAVA_HOME=\/usr\/lib\/jvm\/java-21-openjdk-amd64<\/pre><ol start=\"4\" class=\"wp-block-list\">\n<li>Save the file and apply the changes. If you edited <strong>.bashrc<\/strong>, refresh the environment variables with:<\/li>\n<\/ol><pre class=\"wp-block-preformatted\">source ~\/.bashrc<\/pre><p>Otherwise, log out and back in to apply system-wide changes.<\/p><h2 class=\"wp-block-heading\" id=\"h-conclusion\">Conclusion<\/h2><p>In this guide, you&rsquo;ve successfully installed Java on your Ubuntu server. Whether you chose the JRE to execute Java programs like Apache Tomcat or the JDK to develop Java apps for Android devices, this tutorial has provided you with the essential tools to get started.<\/p><p>Remember, understanding and managing your Java installation is crucial for a seamless development experience on Ubuntu. Best wishes on your upcoming Java-based projects!<\/p><h2 class=\"wp-block-heading\" id=\"h-install-java-on-ubuntu-faq\">Install Java on Ubuntu FAQ<\/h2><p>This section will answer the most common questions about how to install Java on Ubuntu.<\/p><div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1712107082249\"><h3 class=\"schema-faq-question\">What Version of Java Is Compatible With Ubuntu?<\/h3> <p class=\"schema-faq-answer\">Ubuntu is compatible with various Java versions, including OpenJDK and Oracle JDK. The most recent Ubuntu LTS version typically supports the latest version of Java. However, you can install other versions on your system if needed.<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1712107091612\"><h3 class=\"schema-faq-question\">Where Is JDK Installed in Ubuntu?<\/h3> <p class=\"schema-faq-answer\">In Ubuntu, the JDK is typically installed in the <strong>\/usr\/lib\/jvm\/<\/strong> directory. This location can contain multiple JDK installations, each within its subdirectory. To view the specific installation paths and identify the JDK versions installed on your system, run <strong>ls \/usr\/lib\/jvm\/<\/strong>.<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1712107102130\"><h3 class=\"schema-faq-question\">Can I Have Multiple Versions of Java Installed on Ubuntu?<\/h3> <p class=\"schema-faq-answer\">Yes, you can have multiple Java versions installed on Ubuntu simultaneously. To manage and switch between installed versions, use the <strong>update-alternatives<\/strong> system, which configures the default Java version for use in the terminal and applications requiring Java.<\/p> <\/div> <\/div><p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java is a versatile and popular object-oriented programming language known for its platform-independent nature. Installing Java on Ubuntu offers various development possibilities, from running web servers to building Android applications. In this article, you&rsquo;ll learn how to install Java on an Ubuntu server. Whether you&rsquo;re new to Java programming or seeking to refine your existing [&#8230;]<\/p>\n<p><a class=\"btn btn-secondary understrap-read-more-link\" href=\"\/uk\/tutorials\/install-java-ubuntu\">Read More&#8230;<\/a><\/p>\n","protected":false},"author":411,"featured_media":128796,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_title":"How to Install Java on Ubuntu","rank_math_description":"Check this article to learn how to install Java on Ubuntu: 1. Update the package index 2. Install JRE 3. Install a specific Java version and more.","rank_math_focus_keyword":"install java on ubuntu","footnotes":""},"categories":[22640],"tags":[],"class_list":["post-8745","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-vps"],"hreflangs":[{"locale":"en-US","link":"https:\/\/www.hostinger.com\/tutorials\/install-java-ubuntu","default":0},{"locale":"pt-BR","link":"https:\/\/www.hostinger.com\/br\/tutoriais\/como-instalar-java-no-ubuntu","default":0},{"locale":"es-ES","link":"https:\/\/www.hostinger.com\/es\/tutoriales\/como-instalar-java-en-ubuntu","default":0},{"locale":"id-ID","link":"https:\/\/www.hostinger.com\/id\/tutorial\/install-java-ubuntu","default":0},{"locale":"en-UK","link":"https:\/\/www.hostinger.com\/uk\/tutorials\/install-java-ubuntu","default":0},{"locale":"en-MY","link":"https:\/\/www.hostinger.com\/my\/tutorials\/install-java-ubuntu","default":0},{"locale":"en-PH","link":"https:\/\/www.hostinger.com\/ph\/tutorials\/install-java-ubuntu","default":0},{"locale":"es-MX","link":"https:\/\/www.hostinger.com\/mx\/tutoriales\/agregar-meta-descripcion-wordpress-7","default":0},{"locale":"es-CO","link":"https:\/\/www.hostinger.com\/co\/tutoriales\/agregar-meta-descripcion-wordpress-7","default":0},{"locale":"es-AR","link":"https:\/\/www.hostinger.com\/ar\/tutoriales\/como-instalar-java-en-ubuntu","default":0},{"locale":"pt-PT","link":"https:\/\/www.hostinger.com\/pt\/tutoriais\/como-instalar-java-no-ubuntu","default":0},{"locale":"en-IN","link":"https:\/\/www.hostinger.com\/in\/tutorials\/install-java-ubuntu","default":0},{"locale":"en-CA","link":"https:\/\/www.hostinger.com\/ca\/tutorials\/install-java-ubuntu","default":0},{"locale":"en-AU","link":"https:\/\/www.hostinger.com\/au\/tutorials\/install-java-ubuntu","default":0},{"locale":"en-NG","link":"https:\/\/www.hostinger.com\/ng\/tutorials\/install-java-ubuntu","default":0}],"_links":{"self":[{"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/posts\/8745","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/users\/411"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/comments?post=8745"}],"version-history":[{"count":49,"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/posts\/8745\/revisions"}],"predecessor-version":[{"id":128795,"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/posts\/8745\/revisions\/128795"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/media\/128796"}],"wp:attachment":[{"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/media?parent=8745"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/categories?post=8745"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/tags?post=8745"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}