Apache Maven is one of the most popular build automation tools used primarily for Java projects. It significantly simplifies the software development process by automating important tasks such as dependency management, code compilation, running tests, and generating reports. Maven makes sure that your projects are built quickly and regularly, making it an indispensable tool for Java developers working on complex applications.
Whether you are setting up a development environment for a new project or configuring a server for continuous integration (CI), this guide will walk you through the entire process of installing Apache Maven on an Ubuntu server. Installing Maven on Ubuntu is a straightforward process, and we’ll guide you step by step to make sure everything is set up correctly.
First, we will show you how to update your Ubuntu system, ensuring that all your packages are up-to-date and ready for the installation. Next, we will move on to installing Java, which is a mandatory prerequisite for Maven to function. After setting up Java, we’ll cover the installation of Apache Maven itself, explaining each command so that you understand what is happening behind the scenes. Finally, we will discuss how to optionally configure environment variables, allowing you to access Maven more easily from any terminal session.
By the end of this guide, you’ll have a fully functional Maven setup on your Ubuntu server, ready to handle your Java project builds easily and quickly. Whether you’re a developer or managing a team, Maven’s automated processes will help make your workflows simple and save time. Let’s dive in and get started with the Maven installation!
Step 1: Update Your System
Before installing any new software, it’s always a good idea to update your system’s package index. This makes sure that you have the latest information about available packages.
Open your terminal and run the following command:
sudo apt update
This command will fetch the latest package lists from the repositories. If there are any updates, install them using:
sudo apt upgrade
Step 2: Install Java
Maven requires Java to run, so you need to install Java on your VPS. You can install OpenJDK, which is a free and open-source version of Java.
Run the following command to install OpenJDK:
sudo apt install default-jdk
To verify the installation of Java, check the version:
java -version
You should see output similar to this:
openjdk version "11.0.11" 2021-04-20
OpenJDK Runtime Environment (build 11.0.11+9-Ubuntu-0ubuntu2)
OpenJDK 64-Bit Server VM (build 11.0.11+9-Ubuntu-0ubuntu2, mixed mode, sharing)
Step 3: Install Maven
Now that Java is installed, it’s time to install Apache Maven. Run the following command to install Maven:
sudo apt install maven
After the installation is complete, verify that Maven is installed correctly by checking its version:
mvn -version
The output should look like this:
Apache Maven 3.6.3 (c) 2021 Apache Software Foundation
Maven home: /usr/share/maven
Java version: 11.0.11, vendor: Oracle Corporation, runtime: /usr/lib/jvm/java-11-openjdk-amd64
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "5.4.0-42-generic", arch: "amd64", family: "unix"
If you see this output, congratulations! Maven is successfully installed on your Ubuntu VPS.
Step 4: Set Up Environment Variables (Optional)
Configuring environment variables is an optional step, but it can simplify how you interact with Apache Maven on your Ubuntu system. By setting these variables, you make it easier to use Maven commands without needing to type the full path to the Maven executable each time. This is particularly helpful for those managing multiple projects or using Maven frequently, as it allows for more convenient access.
When you install Maven, the binaries are stored in a specific directory. Without environment variables, you’d need to provide the full path (/usr/share/maven/bin/mvn) each time you run a command, which can be tedious. By setting the MAVEN_HOME environment variable and adding it to your system’s PATH, you enable the use of the simple mvn command from anywhere in the terminal. To do this, edit the ~/.bashrc file.
How to Set Environment Variables for Maven
Open the file with a text editor like nano:
nano ~/.bashrc
Add the following lines at the end of the file:
export MAVEN_HOME=/usr/share/maven
export PATH=$MAVEN_HOME/bin:$PATH
Save the file and exit the editor. To apply the changes, run:
source ~/.bashrc
Troubleshooting: If source ~/.bashrc Doesn’t Apply Changes
If applying the changes doesn’t work as expected (for example, Maven commands aren’t recognized globally), here are some steps to resolve the issue:
1. Check if you edited the correct file (~/.bashrc is for the current user; /etc/profile applies system-wide).
2. Confirm there are no errors in the .bashrc file by running:
Troubleshooting: If source ~/.bashrc Doesn’t Apply Changes
If applying the changes doesn’t work as expected (for example, Maven commands aren’t recognized globally), here are some steps to resolve the issue:
1. Check if you edited the correct file (~/.bashrc is for the current user; /etc/profile applies system-wide).
2. Confirm there are no errors in the .bashrc file by running:
source ~/.bashrc
If errors appear, review the changes made.
3. Restart your terminal session or log out and log back in for the changes to take effect.
Now, you can use Maven commands from any terminal session without specifying the full path.
Conclusion
You’ve successfully completed the Apache Maven installation on Ubuntu in just four simple steps! Whether you’re working on Maven installation for Linux, Maven installation on Mac, or setting up Maven on Ubuntu, these steps will guide you through the process. Maven is an important tool for Java project management, ensuring that your project dependencies and builds are well-organized.
If you encounter issues like mvn: command not found or need to check Maven version discrepancies between local and global installations, you now know how to troubleshoot and reinstall the software.
About the writer
This article was written by Vinayak Baranwal, a skilled system administrator, content writer, and AI development enthusiast with expertise in delivering detailed, research-driven content. Vinayak specializes in simplifying complex technical topics, making them accessible to a broad audience while ensuring precision and clarity. His work spans IT services, web hosting, and server solutions, providing valuable insights for businesses and developers alike.
For more insightful content or collaboration opportunities, feel free to connect with Vinayak on LinkedIn through the provided link.