HostingN Academy

How To Install Java In Ubuntu 20.04 Using Terminal

Install Java In Ubuntu 20.04 Using Terminal

Java is a crucial programming language and computing platform that underpins many modern applications and websites. Whether you’re a developer looking to run Java applications or someone who needs it for software dependencies, knowing how to install Java on your system is essential. In this guide, we’ll walk you through the process of How to install Java in Ubuntu 20.04 using terminal, ensuring you have a smooth setup from start to finish.

Prerequisites

Before diving into the installation process, it’s important to prepare your system. First, ensure that your system is up-to-date by running the following commands:

sudo apt update 
sudo apt upgrade

You’ll need administrative privileges to install Java, so make sure you have sudo access. If you’re the only user on your system, you likely already have this access.

Checking for Existing Java Installation

Before installing Java, it’s a good idea to check if it’s already installed on your system. Open the terminal and type:

java -version

If Java is installed, you’ll see the version information. If not, you’ll get a message saying that the command is not found.

Installing Java on Ubuntu 20.04

You have two main options for installing Java: OpenJDK and Oracle JDK.

OpenJDK vs. Oracle JDK

OpenJDK is the open-source implementation of the Java Platform, Standard Edition. Oracle JDK, on the other hand, includes some additional commercial features. For most users, OpenJDK is sufficient and easier to install via the Ubuntu package manager.

Installing OpenJDK

To install OpenJDK, run the following commands:

Install OpenJDK 11 (LTS version):

sudo apt install openjdk-11-jdk

Install OpenJDK 17 (latest version as of 2024):

sudo apt install openjdk-17-jdk

You can install both versions if needed, as we’ll cover managing multiple versions later.

Installing Oracle JDK

Oracle JDK requires you to download the package from the Oracle website and install it manually. Follow these steps:

Download the .tar.gz file from Oracle’s website.

Extract the tarball:

tar -xvf jdk-17_linux-x64_bin.tar.gz

Move the JDK to /opt directory:

sudo mv jdk-17 /opt/

Set Oracle JDK as default:

sudo update-alternatives --install /usr/bin/java java /opt/jdk-17/bin/java 1
sudo update-alternatives --set java /opt/jdk-17/bin/java

Setting Up Java Environment Variables

To ensure Java runs properly, you need to set up environment variables.

Configuring JAVA_HOME

Find your Java installation path:

sudo update-alternatives --config java

Set JAVA_HOME in .bashrc or .profile:

echo "export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64" >> ~/.bashrc
source ~/.bashrc

Updating PATH Variable

Add Java to your PATH:

echo "export PATH=\$PATH:\$JAVA_HOME/bin" >> ~/.bashrc
source ~/.bashrc

Verifying the Installation. How To Install Java In Ubuntu 20.04 Using Terminal

After setting up Java, you should verify the installation.

Check the Java version:

java -version

Run a simple Java program:

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

Compile and run the program:

```bash
javac HelloWorld.java
java HelloWorld
```

Managing Multiple Java Versions

Sometimes you need multiple Java versions. Ubuntu provides a way to manage this.

Install update-alternatives:

sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1
sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/java-17-openjdk-amd64/bin/java 2

Switch between versions:

sudo update-alternatives --config java

Uninstalling Java

If you need to uninstall Java, follow these steps.

Removing OpenJDK

Uninstall OpenJDK:

sudo apt remove openjdk-11-jdk
sudo apt remove openjdk-17-jdk

Clean up residual files:

sudo apt autoremove
sudo apt clean

Removing Oracle JDK

Remove Oracle JDK:

sudo rm -rf /opt/jdk-17

Update alternatives:

sudo update-alternatives --remove java /opt/jdk-17/bin/java

Troubleshooting Common Issues

Installation Errors on How To Install Java In Ubuntu 20.04 Using Terminal

If you encounter installation errors, check your internet connection and package manager settings. Ensure you’re using sudo for administrative tasks.

Environment Variable Issues

Ensure your JAVA_HOME and PATH variables are correctly set. Typographical errors can lead to issues.

Common Fixes

If Java commands aren’t recognized, double-check your PATH settings and ensure Java is correctly installed and configured.

Conclusion

Installing Java on Ubuntu 20.04 using the terminal is straightforward when you know the steps. Whether you choose OpenJDK or Oracle JDK, following this guide will help you set up your environment for Java development. Now, you’re ready to dive into the world of Java programming and explore the endless possibilities it offers.

How do I update Java on Ubuntu 20.04?

You can update Java by running sudo apt update and sudo apt upgrade if you’re using OpenJDK. For Oracle JDK, download the latest version from the Oracle website and follow the installation steps again.

Can I have both OpenJDK and Oracle JDK installed?

Yes, you can have both installed and switch between them using the update-alternatives command.

Leave a Comment

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

Scroll to Top