Installing Java on a Linux server is a straightforward process. Here's a step-by-step guide:
1. Determine the Java Version You Need
- OpenJDK: This is the open-source implementation of Java, widely available and often the default choice for Linux servers.
- Oracle JDK: The proprietary version of Java, offering features like commercial support and advanced tools.
2. Choose Your Installation Method
You can install Java using a package manager or by downloading and installing the binaries directly.
A. Using a Package Manager (Recommended)
- Update Your System: Run
sudo apt update
(Debian/Ubuntu) orsudo yum update
(Red Hat/CentOS) to ensure you have the latest package lists. - Install Java:
- OpenJDK:
- Debian/Ubuntu:
sudo apt install openjdk-11-jdk
(replace11
with the desired Java version) - Red Hat/CentOS:
sudo yum install java-11-openjdk-devel
(replace11
with the desired Java version)
- Debian/Ubuntu:
- Oracle JDK: You'll need to download the Oracle JDK from https://www.oracle.com/java/technologies/downloads/ and follow the instructions in the downloaded file.
- OpenJDK:
- Verify Installation: Run
java -version
to check the installed Java version.
B. Downloading and Installing Binaries
- Download the binaries: Go to https://www.oracle.com/java/technologies/downloads/ and download the appropriate Java version for your Linux distribution.
- Extract the binaries: Use the
tar
command to extract the downloaded archive. For example,tar -xvf jdk-11.0.14_linux-x64_bin.tar.gz
. - Set Environment Variables: Add the following lines to your shell configuration file (e.g.,
.bashrc
,.zshrc
):export JAVA_HOME=/path/to/jdk-11.0.14 export PATH=$JAVA_HOME/bin:$PATH
Replace
/path/to/jdk-11.0.14
with the actual path to your Java installation directory. - Apply changes: Run
source ~/.bashrc
orsource ~/.zshrc
to apply the changes. - Verify Installation: Run
java -version
to check the installed Java version.
3. Configure Your Java Environment
- Set Default Java Version: If you have multiple Java versions installed, you can use the
update-alternatives
command to set the default version. For example, to set OpenJDK 11 as the default:sudo update-alternatives --config java
Select the desired Java version from the list.
4. Test Your Java Installation
- Compile and Run a Java Program: Create a simple Java program (e.g.,
HelloWorld.java
) and compile and run it using thejavac
andjava
commands. - Use a Java Development Environment (IDE): Install a Java IDE like Eclipse or IntelliJ IDEA to develop and test your Java applications.
Conclusion
By following these steps, you can successfully install Java on your Linux server and begin developing or running Java applications.