Menu Octopus Deploy

Java with Linux: installing Java on 6 distros and best practices

Java with Linux: the basics

Java is one of the world’s most popular programming languages. It follows the philosophy of WORA (Write once, run anywhere) allowing applications written in Java to run on any device or operating system equipped with a Java virtual machine (JVM). Linux is a Unix-like operating system kernel that is the foundation for a wide variety of operating system distributions called “distros.” It is distributed freely under the GNU General Public License (GPL).

Java and Linux are frequently used together, as Linux is a popular operating system for deploying and running Java applications. This combination is common in various environments, from development workstations to enterprise servers and cloud deployments.

Key aspects of using Java with Linux include:

Installation: Java can be installed on Linux using various methods, including:

  • Package managers: Most Linux distributions provide OpenJDK (the open-source implementation of Java) through their default package managers (e.g., apt on Debian/Ubuntu, dnf on Fedora/RHEL, pacman on Arch Linux). This is generally the simplest and recommended method for most users.
  • Manual installation: Oracle JDK or specific OpenJDK versions can be manually downloaded as .tar.gz archives and extracted to a desired location. This requires setting environment variables like JAVA_HOME and updating the system’s PATH.

Running Java programs:

  • Java programs are typically compiled into bytecode (.class files) using the java compiler and then executed on the Java Virtual Machine (JVM) using the java command.
  • Java applications can also be packaged into executable .jar files for easier distribution and execution.

Development: Linux provides a robust environment for Java development, supporting various Integrated Development Environments (IDEs) like Eclipse, IntelliJ IDEA, and VS Code, along with build tools like Maven and Gradle.

Server-side applications: Linux is a dominant platform for deploying Java-based server applications, including web servers (e.g., Apache Tomcat, Jetty), application servers (e.g., WildFly, GlassFish), and big data frameworks (e.g., Apache Hadoop, Apache Cassandra).

Performance and stability: Linux is known for its stability and performance, making it a reliable choice for running demanding Java applications, especially in production environments.

Containerization support: Linux is the primary platform for Docker and Kubernetes, enabling Java applications to be containerized and orchestrated with consistent environments.

This is part of a series of articles about software deployment

Tutorial: installing Java on Linux

Let’s see how to install Java on common Linux distros.

First, check if Java is already installed on your system. Run:

java -version

If Java is not present, the terminal will suggest installation options. For example, on Ubuntu you may see:

Command 'java' not found, but can be installed with:
sudo apt install default-jdk
sudo apt install default-jre
apt install openjdk-XX-jdk

If no Java version is found, proceed with installation.

1. Install Java with apt in Ubuntu

Start by updating the package list:

sudo apt update

To install the Java Runtime Environment (JRE):

sudo apt install default-jre

To install the Java Development Kit (JDK):

sudo apt install default-jdk

You can install a specific OpenJDK version by replacing the version number, for example:

sudo apt install openjdk-15-jdk

2. Install Java on Linux Mint

Linux Mint comes with OpenJDK by default. If you want to install Oracle JDK instead, first remove OpenJDK:

sudo apt-get update && sudo apt-get remove openjdk*

Download the correct .tar.gz file for your architecture (x86 or x64) from Oracle’s Java Downloads page. Then:

cd ~/Downloads
tar -zxvf jdk-<version>.tar.gz
sudo mkdir -p /opt/java
sudo mv jdk<version> /opt/java

Set the installed JDK as default:

sudo update-alternatives --install "/usr/bin/java" "java" "/opt/java/jdk<version>/bin/java" 1
sudo update-alternatives --set java /opt/java/jdk<version>/bin/java

Verify:

java -version

3. Install Java on CentOS and Fedora

Update system packages:

sudo yum update

Install JDK 11:

sudo yum install java-11-openjdk-devel

Or install JRE 11:

sudo yum install java-11-openjdk

For Oracle JDK, download the .rpm from Oracle’s site and run:

sudo yum localinstall jre-<version>-linux-x64.rpm

Check installation:

java -version

4. Install Java on AlmaLinux

Update packages:

sudo yum update

Install JDK 17:

sudo yum install java-17-openjdk-devel

Or install JRE 17:

sudo yum install java-17-openjdk

Verify:

java -version

5. Install Java OpenJDK on Red Hat Linux (RHEL8)

Update packages:

sudo dnf update

Install OpenJDK 11:

sudo dnf install java-11-openjdk-devel

Check installation:

java -version

6. Install Java on Arch Linux and Manjaro

Check if Java is installed:

java -version

List available JRE packages:

pacman -sS java | grep jre

Install the latest JRE:

sudo pacman -S jre-openjdk

Install JDK:

sudo pacman -S jdk-openjdk

Best practices for deploying Java applications on Linux

1. Keep Java updated

Keeping Java updated on Linux is critical for mitigating security vulnerabilities and ensuring compatibility with modern libraries and frameworks. Use your system’s package manager—apt, dnf, or pacman—to obtain the latest patches and updates for OpenJDK or your preferred Java distribution. Automated updates, where possible, reduce the risk of running outdated Java runtimes, which are frequent security targets for exploits.

For environments that cannot use automated updates due to regulatory or operational constraints, create a scheduled manual process to review, test, and deploy the latest supported Java releases. Subscribe to security advisories from your distribution, Oracle, or the OpenJDK project to stay aware of relevant CVEs. For critical production systems, always prioritize applying security updates and verify application compatibility in a staging environment before rolling changes out broadly.

2. Use SDKMAN or package managers for easy version management

SDKMAN is a popular tool for managing parallel versions of Java on Linux, simplifying installation, switching, and updating. Install SDKMAN with a simple shell command and then use commands like sdk install java and sdk use java to manage Java versions effortlessly. This flexibility is valuable for developers who need to test or run applications against multiple JVM releases without complicating the system-wide Java installation.

Using native package managers (apt, dnf, yum, or pacman) is best when managing Java for system-wide use or on production servers, ensuring conformity with standard repositories and automated updates. Combine these tools by using SDKMAN for per-user and experimental needs, and the OS package manager for production deployments. This hybrid approach maintains stability, pliability, and easy rollback or upgrades across environments.

3. Configure JVM memory settings for workload

Java applications can run on a wide variety of Linux systems, from minimal VMs to high-memory servers. Adjust JVM memory allocation flags—such as -Xmx (maximum heap size), -Xms (initial heap size), and -Xss (stack size per thread)—to match your workload and hardware. Inefficient memory settings can cause performance bottlenecks, excessive garbage collection, or out-of-memory errors.

Monitor application resource usage with tools such as jstat, jvisualvm, or system metrics (top, vmstat). Consistently tuning heap and garbage collection parameters to complement your service’s memory needs and Linux system resources leads to more predictable behavior under varying loads. Review documentation or use profiling data from test environments to determine optimal JVM settings before deploying changes in production.

4. Monitor and optimize application performance

For Java applications on Linux, continuous monitoring is essential to identify performance bottlenecks and ensure resource efficiency. Tools like VisualVM, JConsole, and Prometheus/JMX exporters provide real-time insights into memory usage, thread activity, and garbage collection behavior. Integrate these tools with Linux monitoring frameworks such as top, htop, or Prometheus/Grafana stacks for unified system and application observability.

When issues such as high CPU usage, stalled threads, or long garbage collection pauses arise, analyze JVM logs and use profilers (YourKit, Flight Recorder) to diagnose root causes. Tune JVM options, adjust thread pools, and review application code for blocking operations. Establish performance baselines and monitor against them continuously to ensure Java services meet SLAs and respond promptly to demand spikes or network changes.

5. Secure Java applications and runtime environment

Securing Java on Linux involves both hardening the runtime environment and following secure coding standards. Limit JVM permissions using SecurityManager policies (for legacy applications), sandboxing, or Linux-level controls like SELinux and AppArmor. Regularly patch the JVM and runtime dependencies, and avoid running Java applications with unnecessary system privileges to reduce the attack surface.

Restrict network, file system, and system resource access to only what the application requires. Implement secure transport (TLS/SSL) for all data in transit and validate all input to prevent injection or deserialization attacks. Monitor the Java process with Linux auditing tools and enable logging for security events. Maintain up-to-date software bills of materials (SBOMs) to track and remediate third-party vulnerabilities across the Java supply chain.

Help us continuously improve

Please let us know if you have any feedback about this page.

Send feedback

Categories:

Next article
Node.js with NGINX