Install Maven on Ubuntu 22.04 [Step-by-Step]

Maven is a build automation tool used for Java projects and can be used under the Apache License 2.0 license. Maven can also be used to create and manage projects written in C#, Ruby, Scala and other languages besides Java.

In this article, we will describe the installation of Maven on Ubuntu 22.04, a popular Linux distribution.

Advertisement

 

How to install Maven on Ubuntu 22.04?

You can install it on Ubuntu 22.04 with two different methods.

 

Method-1: Install Maven From Repository

Ubuntu is one of the most used distributions among Linux distributions. One of the reasons for this is that it keeps applications developed for Linux systems in its repository. Maven is one of them. Let's check the maven version in the repository:

foc@ubuntu22:~$ sudo apt search maven
maven/jammy,jammy 3.6.3-5 all
Java software project management and comprehension tool

To install Maven, it should be written as:

foc@ubuntu22:~$ sudo apt install maven -y

After the above command, the installation is completed with the packages that maven needs from the package repository. Installed Maven version:

foc@ubuntu22:~$ mvn -v
Apache Maven 3.6.3
Maven home: /usr/share/maven
Java version: 11.0.16, 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-50-generic", arch: "amd64", family: "unix"

Maven package also installed Java, version:

foc@ubuntu22:~$ java -version
openjdk version "11.0.16" 2022-07-19
OpenJDK Runtime Environment (build 11.0.16+8-post-Ubuntu-0ubuntu122.04)
OpenJDK 64-Bit Server VM (build 11.0.16+8-post-Ubuntu-0ubuntu122.04, mixed mode, sharing)

The installation from the package repository on Ubuntu 22.04 was completed successfully.

Advertisement

 

Method-2: Install From Binary tar.gz Archive

In this method, we start by installing the packages required for installation:

foc@ubuntu22:/opt$ sudo apt install wget tar default-jdk

The default-jdk package installs the latest Java version in the repository. Version:

foc@ubuntu22:~$ java -version
openjdk version "11.0.16" 2022-07-19
OpenJDK Runtime Environment (build 11.0.16+8-post-Ubuntu-0ubuntu122.04)
OpenJDK 64-Bit Server VM (build 11.0.16+8-post-Ubuntu-0ubuntu122.04, mixed mode, sharing)

Change to the directory where you will download Maven and we will use this directory as the main directory:

foc@ubuntu22:~$ cd /opt/

Go to Maven's official download address, right click on the binary link, copy the link address.

Install Maven on Ubuntu 22.04 [Step-by-Step]

 

Then pull the compressed file to the server with wget:

foc@ubuntu22:/opt$ sudo wget https://dlcdn.apache.org/maven/maven-3/3.8.6/binaries/apache-maven-3.8.6-bin.tar.gz

--2022-10-23 00:07:29-- https://dlcdn.apache.org/maven/maven-3/3.8.6/binaries/apache-maven-3.8.6-bin.tar.gz
Resolving dlcdn.apache.org (dlcdn.apache.org)... 151.101.2.132, 2a04:4e42::644
Connecting to dlcdn.apache.org (dlcdn.apache.org)|151.101.2.132|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 8676320 (8,3M) [application/x-gzip]
Saving to: ‘apache-maven-3.8.6-bin.tar.gz’

apache-maven-3.8.6- 100%[==================>] 8,27M 2,73MB/s in 3,0s

2022-10-23 00:07:32 (2,73 MB/s) - ‘apache-maven-3.8.6-bin.tar.gz’ saved [8676320/8676320]

Extract the compressed file from the archive:

foc@ubuntu22:/opt$ sudo tar xvf apache-maven-3.8.6-bin.tar.gz

To access Maven from any directory, a file must be created under the /etc/profile.d directory and settings must be made. If these settings are not given, it will only be accessed from the directory where maven was downloaded. Create maven.sh:

foc@ubuntu22:/opt$ sudo nano /etc/profile.d/maven.sh
export JAVA_HOME=/usr/lib/jvm/default-java
export M2_HOME=/opt/apache-maven-3.8.6
export MAVEN_HOME=/opt/apache-maven-3.8.6
export PATH=${M2_HOME}/bin:${PATH}

Then pull these settings to the user:

foc@ubuntu22:/opt$ source /etc/profile.d/maven.sh

Installation is complete. Maven version:

foc@ubuntu22:~$ mvn -v
Apache Maven 3.8.6 (84538c9988a25aec085021c365c560670ad80f63)
Maven home: /opt/apache-maven-3.8.6
Java version: 11.0.16, 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-50-generic", arch: "amd64", family: "unix"

 

Bonus Tip: Update Java Version (JDK Binaries)

In Ubuntu 22.04 repositories it was Java 11.0.16. Let's explain the necessary steps to download the more updated version.

Copy the link from the official java address:

Install Maven on Ubuntu 22.04 [Step-by-Step]

Then put the compressed binary file under the opt directory with wget:

Advertisement
foc@ubuntu22:/opt$ sudo wget https://download.java.net/java/early_access/jdk20/20/GPL/openjdk-20-ea+20_linux-x64_bin.tar.gz
--2022-10-23 11:13:48--  https://download.java.net/java/early_access/jdk20/20/GPL/openjdk-20-ea+20_linux-x64_bin.tar.gz
Resolving download.java.net (download.java.net)... 2.17.236.86
Connecting to download.java.net (download.java.net)|2.17.236.86|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 196568892 (187M) [application/x-gzip]
Saving to: ‘openjdk-20-ea+20_linux-x64_bin.tar.gz’

openjdk-20-ea+20_li 100%[===================>] 187,46M  1,99MB/s    in 1m 42s  

2022-10-23 11:15:31 (1,83 MB/s) - ‘openjdk-20-ea+20_linux-x64_bin.tar.gz’ saved [196568892/196568892]

Extract the compressed file:

foc@ubuntu22:/opt$ sudo tar xvf openjdk-20-ea+20_linux-x64_bin.tar.gz
foc@ubuntu22:/opt$ ls 
jdk-20

Then edit the file where you wrote the PATHs:

foc@ubuntu22:/opt$ sudo vi /etc/profile.d/maven.sh
export JAVA_HOME=/opt/jdk-20

Apply the PATH change to the user:

foc@ubuntu22:/opt$ source /etc/profile.d/maven.sh
foc@ubuntu22:/opt$ echo $JAVA_HOME
/opt/jdk-20

You can see that the Java version has changed when you pull the Maven version:

foc@ubuntu22:/opt$ mvn -version
Apache Maven 3.8.6 (84538c9988a25aec085021c365c560670ad80f63)
Maven home: /opt/apache-maven-3.8.6
Java version: 20-ea, vendor: Oracle Corporation, runtime: /opt/jdk-20
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "5.15.0-50-generic", arch: "amd64", family: "unix"

 

Summary

In Maven related locale below command will help.

foc@ubuntu22:~$ mvn --help

usage: mvn [options] [<goal(s)>] [<phase(s)>]

Options:
 -am,--also-make                        If project list is specified, also
                                        build projects required by the
                                        list
 -amd,--also-make-dependents            If project list is specified, also
                                        build projects that depend on
                                        projects on the list
...

We explained how to install Maven on Ubuntu 22.04. For more information we think article 50 Maven Interview Questions and Answers for freshers and experienced will help.

Advertisement

 

References

maven.apache.org - Downloading Apache Maven

 

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment

X