In the next article we are going to take a look at Apache Maven. This is one project management and understanding tool Free and open source that is mainly used for Java projects. Maven uses a project object model (POM) which is essentially an XML file. This contains information about the project, configuration details, project dependencies, and so on.
In this post we are going to see two different ways of to install Apache Maven on Ubuntu 18.10. The same instructions apply to other versions of Ubuntu and any distribution based on it. Including Linux Mint and Elementary OS.
The official Ubuntu repositories contain the Maven packages. These can install with apt package manager. This is the easiest way to install Maven on Ubuntu, however the version included in the repositories may lag behind the latest version. For this reason we will also see how we can install the latest version by following the instructions that we will see in the second part of this article. In it we will download Maven from its official website.
Install Apache Maven
Installation via APT
Installing Maven on Ubuntu using apt is a simple and straightforward process. We will only have to open a terminal (Ctrl + Alt + T) and start updating the package index:
sudo apt update
Then we will install Maven by typing the following command in the same terminal:
sudo apt install maven
Once the installation is finished, we can verify is writing:
mvn -version
The output should show us something like the following:
That's all. Maven is already installed on our system and we can start using it.
Installation of the latest version by downloading it from your website
The following sections provide step-by-step instructions on how to install the latest version of Apache Maven on Ubuntu 18.10. We are going to download the latest version of Apache Maven from its official website. But first of all we will start by installing OpenJDK.
Install OpenJDK
Maven 3.3+ requires JDK 1.7 or higher to be installed. Java installation is pretty simple. We will start by updating the package index, typing in a terminal (Ctrl + Alt + T):
sudo apt update
We continue installing the OpenJDK package typing in the same terminal:
sudo apt install default-jdk
After installation, we can verify installation executing the following command:
java -version
If everything has been correct, the output should show us something like this:
Download Apache Maven
At the time of writing, the latest version of Apache Maven is 3.5.4. Before proceeding to the next step, it is interesting consult the download page to see if a newer version is available.
After checking, we will start with download Apache Maven in the / tmp directory using the following wget command in a terminal (Ctrl + Alt + T):
wget https://www-us.apache.org/dist/maven/maven-3/3.5.4/binaries/apache-maven-3.5.4-bin.tar.gz -P /tmp/
Once the download is complete, we will extract the file in the / opt directory:
sudo tar xf /tmp/apache-maven-*.tar.gz -C /opt/
To have more control over Maven versions and updates, we are going to create a symbolic link that will point to the installation directory:
sudo ln -s /opt/apache-maven-3.5.4 /opt/maven
Later, if we want to update the installation, we will only have to unzip the most recent version and change the symbolic link to point to the latest version.
Set environment variables
The next step to follow will be to configure the environment variables. To do this, we will open our preferred text editor and we will create a new file called maven.sh inside the directory /etc/profile.d/.
sudo vim /etc/profile.d/maven.sh
Once the file is open, we will have to paste the following configuration:
export JAVA_HOME=/usr/lib/jvm/default-java export M2_HOME=/opt/maven export MAVEN_HOME=/opt/maven export PATH=${M2_HOME}/bin:${PATH}
Once pasted, we save and close the file. Now we will have to make the script executable typing:
sudo chmod +x /etc/profile.d/maven.sh
To finish, let's load environment variables using the following command:
source /etc/profile.d/maven.sh
Check the installation
For validate that Maven is installed correctly, we will use the following command that will print the version:
mvn -version
You should see something like the following:
With all this, we have just successfully installed Apache Maven on Ubuntu, 18.10 in this example. Now we can visit the official page of the documentation by Apache Maven and learn to use it.