Install Apache Maven on Ubuntu 20.04 with Easy Steps
In this tutorial, we aim to guide you through the process of how to Install Apache Maven on Ubuntu 20.04. Apache Maven stands as a fundamental tool in Java development, widely recognized as the most utilized build management tool for Java projects. Maven’s streamlined configuration model, based on XML, allows developers to quickly define and understand the structure of Java-based projects, making it easier to start and share new projects.
Maven also provides excellent support for test-driven development and long-term project maintenance. Its declarative configuration style, combined with a vast array of plugins, makes it a popular choice for continuous integration and continuous delivery (CI/CD) pipelines. Therefore, knowing how to Install Apache Maven on Ubuntu 20.04 is essential for any Java developer.
You can now proceed with the following steps to set up Maven on your Ubuntu 20.04 system.
Before you begin to Install Apache Maven on Ubuntu 20.04, ensure you are logged into your server as a non-root user with sudo
privileges. If you haven’t already, follow our guide on Initial Server Setup with Ubuntu 20.04 to create such a user.
Now, let’s walk through the steps to install the latest release of Maven on Ubuntu 20.04.
1. Install OpenJDK on Ubuntu 20.04
To set up Apache Maven, Java needs to be installed on your server. Ubuntu 20.04 comes with OpenJDK-11 as the default Java version, which is fully compatible with the latest versions of Apache Maven.
First, update your local package index using the following command:
sudo apt update
Next, install OpenJDK by executing this command:
sudo apt install default-jdk -y
To verify the Java installation, check its version:
java --version
openjdk version "11.0.18" 2023-01-17
OpenJDK Runtime Environment (build 11.0.18+10-Ubuntu-0ubuntu120.04.1)
OpenJDK 64-Bit Server VM (build 11.0.18+10-Ubuntu-0ubuntu120.04.1, mixed mode, sharing)
2. Install Apache Maven LTS on Ubuntu 22.04
At this stage, visit the Apache Maven Downloads page to find the latest version of Maven available. Use the wget
command to download the binary tar.gz file.
sudo wget https://dlcdn.apache.org/maven/maven-3/3.9.0/binaries/apache-maven-3.9.0-bin.tar.gz
Once the download is complete, extract the downloaded file:
tar -xvf apache-maven-*-bin.tar.gz
Then, move the extracted directory to the /usr/share
directory:
sudo mv apache-maven-3.9.0 /usr/share/maven
To make the mvn
command available from anywhere in your terminal, you need to add the Maven’s bin
directory to your system’s PATH
environment variable. You can accomplish this by adding the following lines to your ~/.bashrc
file:
# echo 'export PATH="$PATH:/usr/share/maven"' >> ~/.bashrc
# echo 'export PATH="$PATH:/usr/share/maven/bin"' >> ~/.bashrc
After modifying the ~/.bashrc
file, source it to apply the changes:
source ~/.bashrc
Finally, verify the Maven installation by checking the Maven version:
mvn -v
Apache Maven 3.9.0 (9b58d2bad23916cd93fe8a5fba65a0ba1f02984)
Maven home: /usr/share/maven
Java version: 11.0.18, vendor: Ubuntu, runtime: /usr/lib/jvm/java-11-openjdk-amd64
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "5.15.0-67-generic", arch: "amd64", family: "unix"
3. Remove Apache Maven From Ubuntu 20
If you decide that you no longer need Maven, you can remove it from your Ubuntu 20.04 system using the following command:
sudo rm /usr/share/maven
And to remove Java, you can use:
sudo apt autoremove default-jdk --purge
Conclusion
You have now successfully learned how to Install Apache Maven on Ubuntu 20.04. Apache Maven streamlines the build process, manages dependencies, and ensures consistency among software development teams by defining project structures, build configurations, and lifecycle tasks.
Alternative Solutions for Installing Apache Maven on Ubuntu 20.04
While the manual installation method described above works reliably, there are alternative ways to install Apache Maven on Ubuntu 20.04 that can simplify the process, particularly for users who prefer package managers. Here are two such alternatives:
1. Using SDKMAN! (Software Development Kit Manager)
SDKMAN! is a tool for managing parallel versions of multiple Software Development Kits on most Unix-based systems. It provides a convenient command-line interface for installing, switching between, and managing different versions of software like Java, Maven, Gradle, and more.
Explanation:
SDKMAN! simplifies the installation and management of Maven by handling the download, installation, and path configuration automatically. It also allows you to easily switch between different Maven versions if needed.
Steps:
-
Install SDKMAN!:
Open your terminal and execute the following command:curl -s "https://get.sdkman.io" | bash
Follow the on-screen instructions to complete the installation. You might need to open a new terminal session or source the SDKMAN! initialization script:
source "$HOME/.sdkman/bin/sdkman-init.sh"
-
Install Maven using SDKMAN!:
sdk install maven
This command will download and install the latest stable version of Maven.
-
Verify Installation:
mvn -v
SDKMAN! will automatically configure your environment to use the newly installed Maven version.
-
Install a Specific Maven Version (Optional):
You can also install a specific version of Maven using SDKMAN!. First, list the available Maven versions:
sdk list maven
Then, install a specific version:
sdk install maven 3.8.6
2. Using Snap Package Manager
Snap is a package management system developed by Canonical (the company behind Ubuntu). It allows you to install software in a sandboxed environment, reducing the risk of conflicts with other system components.
Explanation:
Snap packages provide a self-contained environment for applications, ensuring that all dependencies are included and reducing the likelihood of installation issues. This can be especially useful for applications like Maven, which have specific dependencies.
Steps:
-
Install Snap (if not already installed):
Snap is usually pre-installed on Ubuntu systems. If it is not, you can install it with:sudo apt update sudo apt install snapd
-
Install Maven using Snap:
sudo snap install maven
-
Verify Installation:
The Snap version of Maven may not automatically add the
mvn
command to yourPATH
. You might need to use the full path or create an alias. First find the path:snap info maven
This will provide information about the snap, including the command path. It might look something like
/snap/bin/maven
. Then run the version check:/snap/bin/maven -v
-
Create an Alias (Optional):
To use the mvn
command directly, create an alias in your ~/.bashrc
file:
echo 'alias mvn="/snap/bin/maven"' >> ~/.bashrc
source ~/.bashrc
Now, you can use mvn -v
to verify the installation.