How to install Apache Maven on Ubuntu 20.04

s2 0

Apache Maven is an open source project management and understanding tool mainly used for Java projects. Maven uses a Project Object Model (POM), which is essentially an XML file that contains information about the project, configuration details, the project’s dependencies, and more.

In this tutorial, we are going to show you two different ways to install Apache Maven on Ubuntu 20.04.

The official Ubuntu repositories contain Maven packages that can be installed with the apt Package manager. This is the easiest way to install Maven on Ubuntu. However, the version contained in the repositories may lag behind the latest version of Maven.

To install the latest version of Maven, follow the instructions in the second part of this article, which shows how to download the binary distribution archive from the official website.

Choose the installation method that works best for your setup and environment.

requirements

The instructions assume that you are logged in as root or a user with sudo privileges.

Install Apache Maven on Ubuntu 20.04 with apt

Install Maven on Ubuntu with apt is a simple, straightforward process.

Update the package index and install Maven by entering the following commands:

sudo apt updatesudo apt install maven

To verify the installation, run mvn -version:

mvn -version

The output should look something like this:

Apache Maven 3.6.3
Maven home: /usr/share/maven
Java version: 11.0.7, vendor: Ubuntu, runtime: /usr/lib/jvm/java-11-openjdk-amd64
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "5.4.0-26-generic", arch: "amd64", family: "unix"

That’s it. Maven is now installed on your system and you can use it.

Install the latest version of Apache Maven

This section provides step-by-step instructions on how to download and install the latest version of Apache Maven on Ubuntu 20.04.

1. Install OpenJDK

Maven 3.3+ requires JDK 1.7 or higher to be installed.

Install OpenJDK 11 by typing:

sudo apt updatesudo apt install default-jdk

Verify the installation by running the following command:

java -version

The output should look something like this:

openjdk version "11.0.7" 2020-04-14
OpenJDK Runtime Environment (build 11.0.7+10-post-Ubuntu-3ubuntu1)
OpenJDK 64-Bit Server VM (build 11.0.7+10-post-Ubuntu-3ubuntu1, mixed mode, sharing)

2. Download Apache Maven

At the time of this writing, the latest version of Apache is Maven 3.6.3. Before proceeding to the next step, visit the Maven download page
to see if a newer version is available.

Download the Apache Maven in. down /tmp Directory:

wget https://www-us.apache.org/dist/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz -P /tmp

As soon as the download is complete, unzip the archive in /opt Directory:

sudo tar xf /tmp/apache-maven-*.tar.gz -C /opt

To have more control over Maven versions and updates, we’ll create a symbolic link
maven this points to the Maven installation directory:

sudo ln -s /opt/apache-maven-3.6.3 /opt/maven

When a new version is released, you can update your Maven installation by extracting the newer version and changing the symlink to point to it.

3. Set up environment variables

Next we need to set up the environment variables. To do this, open your text editor and create a new file called maven.sh by doing /etc/profile.d/ Directory.

sudo nano /etc/profile.d/maven.sh

Paste the following code:

/etc/profile.d/maven.sh
export JAVA_HOME=/usr/lib/jvm/default-java
export M2_HOME=/opt/maven
export MAVEN_HOME=/opt/maven
export PATH=${M2_HOME}/bin:${PATH}

Save and close the file. This script is provided when the shell is started.

Make the script executable with chmod
:

sudo chmod +x /etc/profile.d/maven.sh

Finally, load the environment variables with the source
Command:

source /etc/profile.d/maven.sh

4. Check the installation

To check if Maven is installed, use the mvn -version Command that prints the Maven version:

mvn -version

You should see something like this:

Maven home: /opt/maven
Java version: 11.0.7, vendor: Ubuntu, runtime: /usr/lib/jvm/java-11-openjdk-amd64
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "5.4.0-26-generic", arch: "amd64", family: "unix"

That’s it. The latest version of Maven is now installed on your Ubuntu system.

diploma

We showed you how to install Apache Maven on Ubuntu 20.04. You should visit the officer now Apache Maven documentation
Page and learn how to get started with Maven.

If you run into any problem or have any feedback, please leave a comment below.

Tags: