How to Install Maven on Ubuntu 20.04
Introduction
Before we begin talking about how to install Maven, let's briefly understand - What is Maven?
Maven is a popular build automation tool used in software development. It simplifies the process of managing and building projects by automating tasks like compiling code, managing dependencies, and creating project reports.
With its intuitive structure, Maven allows developers to easily manage project lifecycles, collaborate with teams, and ensure consistent builds. This open-source tool enhances productivity, fosters code reuse, and promotes best practices in the software development process.
In this tutorial, you will install Maven on Ubuntu 20.04. Also, we will answer some FAQs related to Maven.
Advantages of Maven
- Simplified project management: Maven automates tasks like dependency management, building, and testing, streamlining project workflows.
- Dependency management: Maven resolves and manages project dependencies, ensuring consistent and error-free builds.
- Code reuse: Maven promotes code reuse through its centralized repository, enabling developers to share and reuse libraries easily.
- Consistent builds: Maven enforces best practices and standardizes build processes, resulting in consistent and reliable builds across different environments.
- Collaboration and scalability: Maven facilitates team collaboration, allowing multiple developers to work on a project simultaneously and scale projects efficiently.
Perquisites to Install Maven on Ubuntu 20.04
- A user with sudo privileges.
Step 1 – Install Apache Maven on Ubuntu with Apt
1) Firstly, update the package index:
sudo apt update
2) After that, install Maven by running the following command:
sudo apt install maven
3) You will have to verify the installation. Do it by running the mvn -version
command:
mvn -version
You will get the following output:
Output
Apache Maven 3.5.2
Maven home: /usr/share/maven
Java version: 10.0.2, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-11-openjdk-amd64
Default locale: en_US, platform encoding: ISO-8859-1
OS name: "linux", version: "4.15.0-36-generic", arch: "amd64", family: "unix"
Installation of Maven is now complete.
Step 2 - Install the Latest Version of Maven
1) You will now install the OpenJDK. OpenJDK is the default Java-development and has good runtime in Ubuntu 20.04. Start the installation of Java by updating the package index. Run the command:
sudo apt update
2) Now, install the OpenJDK package by running:
sudo apt install default-jdk
3) After that, you will verify the installation. Do it by running the following command:
java -version
The output will look like this:
Output
openjdk version "10.0.2" 2018-07-17
OpenJDK Runtime Environment (build 10.0.2+13-Ubuntu-1ubuntu0.18.04.2)
OpenJDK 64-Bit Server VM (build 10.0.2+13-Ubuntu-1ubuntu0.18.04.2, mixed mode)
4) After that, check the download page of Maven, if a newer version is available. Start by downloading Maven in the /tmp
directory, by the following wget
command:
wget http://www-eu.apache.org/dist/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz
5) After the download is complete, then extract the archive in the /opt
directory.
sudo tar xf apache-maven-3.6.3-bin.tar.gz -C /opt
6) For more control over Maven versions and updates, create a symbolic-link maven
. It will directly point to the Maven installation directory.
sudo ln -s /opt/apache-maven-3.6.3 /opt/maven
7) To upgrade your Maven installation later, simply unpack the newer version and then change the symlink to point to that version.
8) Next, you will set up the environment variables. Create a new file named mavenenv.sh
inside /etc/profile.d/
directory.
sudo nano /etc/profile.d/maven.sh
9) After that, paste the following configurations in /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}
10) You will then save and close the file. The script will get sourced at the shell startup. Further, make the script executable with the chmod
.
sudo chmod +x /etc/profile.d/maven.sh
11) Finally, load the environment variables using source
command with the following command.
source /etc/profile.d/maven.sh
12) After that, you need to verify the installations. You will do it using the mvn -version
command, which will print the Maven version:
mvn -version
You will get the following output:
Output
Apache Maven 3.6.0 (97c98ec64a1fdfee7767ce5ffb20918da4f719f3; 2018-10-24T18:41:47Z)
Maven home: /opt/maven
Java version: 10.0.2, vendor: Oracle Corporation, runtime: /usr/lib/jvm/java-11-openjdk-amd64
Default locale: en_US, platform encoding: ISO-8859-1
OS name: "linux", version: "4.15.0-36-generic", arch: "amd64", family: "unix"
FAQs to Install Maven on Ubuntu 20.04
How do I check if Maven is installed correctly on Ubuntu 20.04?
To check the Maven installation, open a terminal and run the command mvn -version
. It will display the installed version and other information.
Can I use a specific version of Maven on Ubuntu 20.04?
Yes, you can use a specific version of Maven on Ubuntu 20.04. Visit the Apache Maven website, download the desired version, and follow the installation instructions.
How do I set up Maven environment variables on Ubuntu 20.04?
Open the terminal and run the command sudo nano /etc/profile.d/maven.sh
. Add the following lines: export M2_HOME=/usr/share/maven
, export PATH=$PATH:$M2_HOME/bin
. Save the file and run source /etc/profile.d/maven.sh
to apply the changes.
Can I use Maven with IDEs on Ubuntu 20.04?
Yes, popular IDEs like Eclipse and IntelliJ have built-in support for Maven. You can import Maven projects directly into these IDEs.
How do I create a new Maven project on Ubuntu 20.04?
Use the Maven command-line tool to create a new project. Run mvn archetype:generate
and follow the prompts to choose a project template.
Can I customize Maven settings on Ubuntu 20.04?
Yes, Maven provides a settings.xml file where you can configure various options like repositories, proxies, and plugin configurations. The file is located in ~/.m2
directory.
Conclusion
We hope this detailed tutorial helped you to install Maven on Ubuntu 20.04. To learn more about Maven installation, check out the official Maven Documentation.
If you have any queries, please leave a comment below, and we’ll be happy to respond to them for sure.