How-To

How to Install Java on Ubuntu

How to Install Java on Ubuntu - Hero

If you want to use Java applications on Ubuntu (or code them yourself), you’ll need to install it. Here’s how to install Java on your Ubuntu PC.

There are a lot of reasons why Java remains one of the most commonly learned programming languages for new coders. If you’re trying to understand why code works, then a high-level language like Java is perfect for it. It has a huge amount of support, and you’ll find Java apps running on everything from a TV to your toaster.

If you’re running Ubuntu, you’ll find it an easy process to install Java on your Ubuntu PC, but there are a few steps you’ll need to remember to follow first. I’ll explain how to install Java below.

Steps to Take Before Installing Java on Ubuntu

Before you install Java on Ubuntu, you’ll need to make sure you’ve updated your package sources. Your package sources are the list of packages available for you to install.

You’ll need to update these before you install Java so that your Ubuntu system has the most up-to-date list of packages available for installation. If you don’t, Java might not install, or it might not work properly after installation.

To do this, open a terminal and type sudo apt update, then press Enter. If you’re prompted to do so, enter your password to complete the process. You can then update your system, ensuring all packages are up-to-date, by typing sudo apt upgrade and pressing Enter.

Updating Ubuntu

How to Install Java on Ubuntu

There are a few ways you can install Java on Ubuntu, depending on your requirements.

Choosing Between OpenJDK and Oracle JDK

Before installation, you should decide whether to use OpenJDK or Oracle JDK. OpenJDK is an open-source implementation of Java and is the default JDK (Java Development Kit) in Ubuntu. It’s widely used and available for free.

On the other hand, Oracle JDK is a commercial implementation provided by Oracle with some additional commercial features and support.

Installing Default JDK Packages

If you want the standard version of Java that Ubuntu supports, you can easily install it via the terminal. To install the default JDK on Ubuntu, follow these steps.

  1. Open a terminal window and type sudo apt update && sudo apt upgrade (if you’ve not already done so).
  2. To install the OpenJDK, type sudo apt install default-jdk and press Enter. This will install several necessary Java packages, including those for OpenJDK.
    Install OpenJDK for Java on Ubuntu
  3. Wait for the installation to complete and follow any additional on-screen instructions.
  4. Verify the installation by typing java -version and pressing Enter. You should see the installed Java version displayed.
    Checking Java version in Ubuntu

This will install the Java Runtime Environment (JRE) along with the JDK.

Installing Specific JDK Versions

Sometimes your applications may require a specific version of Java. Ubuntu repositories contain multiple versions, like OpenJDK 8 and OpenJDK 11, which are the most commonly used.

To install a specific version of OpenJDK on Ubuntu, follow these steps.

  1. Open the terminal and update your package list using the sudo apt update command.
  2. Next, you can install a specific version of OpenJDK (such as OpenJDK 11) by typing sudo apt install openjdk-XX-jdk and pressing Enter, replacing XX with the number. For example, sudo apt install openjdk-11-jdk.
    Installing a custom version of OpenJDK on Ubuntu
  3. Next, type sudo update-alternatives --config java and press Enter.
  4. Select your preferred Java version by typing in a value that matches the version from the list, then press Enter.
    Choosing between different Java versions on Ubuntu
  5. Confirm the version installed by typing java -version and press Enter.
    Confirming a custom Java version on Ubuntu

If you want to use the Oracle JDK, you’ll need to download this from the Oracle website and install it directly using the latest instructions.

How to Configure Your Java Environment on Ubuntu

Once you’ve installed your preferred Java version, you’ll need to set environment variables and choose the appropriate Java version to use (if you haven’t already).

Setting the JAVA_HOME Environment Variable

The JAVA_HOME environment variable tells your Ubuntu system where the Java Runtime Environment (JRE) and Java Development Kit (JDK) are located. To set the JAVA_HOME environment variable, follow these steps.

  1. Open a new terminal window.
  2. Determine the path to the installed JDK by running the sudo update-alternatives --config java command, making sure to copy the path for your preferred version.
    Confirming a Java installation path
  3. Next, type sudo nano /etc/environment and press Enter.
  4. At the end of the file, add JAVA_HOME="/path/to/java/installation" while replacing the example path with the path you copied.
  5. Press Ctrl+O, then Ctrl+X to save and exit the file.
    Set a custom Java installation path on Ubuntu

Using Java on Ubuntu

Java is just one of the many different programming languages that coders using Ubuntu can take advantage of. You shouldn’t find it hard to compile and run your code, but make sure to keep your Java installation up-to-date to avoid any bugs or problems further down the line.

Don’t be afraid to experiment. If you’re a brand new coder, Java is a great language to learn, but if you’ve got the concepts down, you can always try alternatives like Python or Ruby instead.

Click to comment

Leave a Reply

Your email address will not be published. Required fields are marked *

 

To Top