Apache Maven is very common and mainly used for Java build automation tools. It completely automates dependency management, code compilation, test running, and intelligently generated status reports on the fly, making development much more straightforward. Maven ensures your projects build quickly and regularly and are unchallenged by Java developers working on demanding applications.
This guide will set Apache Maven up on an Ubuntu server. Each blog entry will either set up a development environment for a brand-new project or configure a server for continuous integration (CI). First, Maven makes your projects build quickly and reliably, an indispensable utility for Java developers specializing in large applications.
Next, we will instruct you on updating your Ubuntu system so everything is ready for installation. The next step is to install Java, which is an absolute must if Maven is going to run. Then, we’ll install Apache Maven, explaining all the commands that are happening behind the scenes. Then, we talk about optionally configuring environment variables so that you can thank Maven just fine from any terminal session.
Start by updating your Ubuntu system so that your installed packages are up-to-date. After that, install Java, as Maven requires it to function. Once you have installed Java, you can start installing Apache Maven.
Step 1: Update Your System
Updating your system’s package index before installing new software is always a good idea. This step ensures you have the latest information on available packages.
Open your terminal to 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, a free and open-source version of Java.
Run the following command to install OpenJDK:
sudo apt install default-jdk
For confirmation of the application of Java, Check the Java 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 you have installed Java, 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! You have successfully installed Maven on your Ubuntu VPS.
Step 4: Set Up Environment Variables (Optional)
Configuring environment variables is optional but can simplify how you interact with Apache Maven on your Ubuntu system. Setting these variables makes it easier to use Maven commands without needing to type the full path to the Maven executable each time. This configuration benefits those managing multiple projects or using Maven frequently, as it allows for more convenient access.
When you install Maven, the system places its binaries 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 can enable 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 (The system does not recognize Maven commands 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 (The system does not recognize Maven commands 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 got the Apache Maven installed on Ubuntu in four easy steps! Whatever distribution you are using (Linux, Mac, or Ubuntu), setting up Maven there would require steps to be taken. Maven is an essential tool for managing Java projects; there is a guarantee that the dependencies and builds of your project will be well organized.
If you encounter issues like mvn: command not found or need to check Maven version discrepancies between local and global installations, you know how to troubleshoot and reinstall the software.
About the writer
Vinayak Baranwal wrote this article. Use the provided link to connect with Vinayak on LinkedIn for more insightful content or collaboration opportunities.