Apache Maven is a powerful project management tool that is based on the Project Object Model (POM). It is used for managing project builds, dependencies, and documentation. Maven simplifies the build process like ANT but it is much more advanced. If you’re developing Java applications, Maven can help manage your project’s lifecycle and dependencies. This guide will walk you through the process of installing Maven on Ubuntu 20.04.
You may like: How to Install Gradle on Ubuntu 20.04
Step 1: Update Your System
Before you begin any installation, it’s a good practice to update the package repository. This ensures you have access to the latest versions of the packages and dependencies. Open your terminal and run the following command:
sudo apt update && sudo apt upgrade -y
Step 2: Install Java
Maven requires Java to be installed on your system. You can choose to install either OpenJDK or Oracle JDK. This guide will use OpenJDK 11 as it’s freely available and easy to install through Ubuntu’s package manager. Run the following command to install OpenJDK 11:
sudo apt install openjdk-11-jdk -y
After the installation completes, you can verify it by checking the Java version:
java -version
The output should display the installed version of Java, confirming that Java is successfully installed on your system.
Step 3: Download Apache Maven
The next step is to download the latest version of Maven. You can find the download link for the latest version on the official Apache Maven download page. However, to make things easier, you can also download it directly using wget with the link to the binary archive. As of writing this guide, Maven 3.9.6 is the latest version, but you should check the official Maven website for the latest version.
wget https://archive.apache.org/dist/maven/maven-3/3.9.6/binaries/apache-maven-3.9.6-bin.tar.gz
Step 4: Extract the Archive
Once the download is complete, extract the Maven archive to the /opt directory:
sudo tar xf apache-maven-3.9.6-bin.tar.gz -C /opt
To keep things organized, it’s a good idea to create a symbolic link to the Maven directory. This way, if you update Maven in the future, you can simply update the symlink without changing your environment variables:
sudo ln -s /opt/apache-maven-3.9.6 /opt/maven
Step 5: Setup Environment Variables
You need to set up environment variables for Maven so that it can be run from anywhere in the terminal. To do this, create a new file in the /etc/profile.d directory:
sudo nano /etc/profile.d/maven.sh
Add the following lines to the file:
export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
export M2_HOME=/opt/maven
export MAVEN_HOME=/opt/maven
export PATH=${M2_HOME}/bin:${PATH}
Save and close the file. Then, make it executable:
sudo chmod +x /etc/profile.d/maven.sh
To apply the changes, source the profile file:
source /etc/profile.d/maven.sh
Step 6: Verify Maven Installation
Finally, to verify that Maven has been installed correctly, you can check its version:
mvn -version
The command should output the Maven version, the Java version, and the operating system information. This confirms that Maven is successfully installed on your system.
Maven home: /usr/local/maven
Java version: 11.0.22, vendor: Ubuntu, runtime: /usr/lib/jvm/java-11-openjdk-amd64
Default locale: en_IN, platform encoding: UTF-8
OS name: "linux", version: "5.15.0-100-generic", arch: "amd64", family: "unix"
Congratulations! You have successfully installed Apache Maven on Ubuntu 20.04. You can now proceed to use Maven for managing your Java projects’ lifecycle and dependencies.