Installing The Latest Apache Maven on Ubuntu

Vanessa Fotso
2 min readFeb 19, 2023

--

java.lang.IllegalStateException

I recently encountered a maven exception when compiling a java project. The error was as follow:

[ERROR] Error executing Maven.
[ERROR] java.lang.IllegalStateException: Unable to load cache item
[ERROR] Caused by: Unable to load cache item
[ERROR] Caused by: Could not initialize class com.google.inject.internal.cglib.core.$MethodWrapper

My java version at the time was

openjdk version "17.0.2" 2022-10-18
OpenJDK Runtime Environment (build 17.0.2+8-Ubuntu-2ubuntu120.04)
OpenJDK 64-Bit Server VM (build 17.0.2+8-Ubuntu-2ubuntu120.04, mixed mode, sharing)

and my maven version at the time was

Apache Maven 3.6.3
Maven home: /usr/share/maven
Java version: 17.0.2, vendor: Oracle Corporation, runtime: /usr/lib/jvm/java-17-openjdk-amd64
Default locale: en, platform encoding: UTF-8
OS name: "linux", version: "5.10.16.3-microsoft-standard-wsl2", arch: "amd64", family: "unix"

The cause of the error was that maven version(3.6.3) is old. I needed to upgrade to the latest version of maven.

Unfortunately, I could not upgrade to the latest maven version (3.9.0 at the time) using the aptpackage manager on Ubuntu. Generally, the easiest way to install anything on Ubuntu is via the apt package manager. However, it often does not include the latest JDK packages.

These are the steps to install the latest maven version on Ubuntu:

  1. Download the latest maven binaries

a. cd into the /tmp directory on your terminal

b. Check https://maven.apache.org/download.cgi and copy the link for the “Binary tar.gz archive” file.

c. Run the following command to download the binaries:

wget link_to _binary_tar.gz_file_you_copied

d . Untar the archive file and extract it in the /opt directory

tar -xvf apache-maven-3.9.0-bin.tar.gz
mv apache-maven-3.9.0 /opt/

Note: the latest maven version I was downloading was 3.9.0. Make sure to replace the version in the commands above with the maven version you are downloading.

e. Create a sym-link that will point to the Maven installation directory

sudo ln -s /opt/apache-maven-3.9.0 /opt/maven

2. Set up the environment variables

a. Open /etc/profile.d/maven.sh

sudo nano /etc/profile.d/maven.sh

b. Edit the file by adding the following to it:

export JAVA_HOME=/usr/lib/jvm/default-java
export M2_HOME=/opt/maven
export MAVEN_HOME=/opt/maven
export PATH=${M2_HOME}/bin:${PATH}

Then, save and close the file.

c. Make the script executable:

sudo chmod +x /etc/profile.d/maven.sh

d. Load the Env variables and use it without restarting the terminal

source /etc/profile.d/maven.sh

3. Verify the installed maven version

mvn -v

The output should be something like this:

Apache Maven 3.9.0 (9b58d2bad23a66be161c4664ef21ce219c2c8584)
Maven home: /opt/maven
Java version: 17.0.2, vendor: Oracle Corporation, runtime: /opt/jdk-17.0.2
Default locale: en, platform encoding: UTF-8
OS name: "linux", version: "5.10.16.3-microsoft-standard-wsl2", arch: "amd64", family: "unix"

That’s all! You now have the latest maven version installed in your Linux system.

--

--

Vanessa Fotso
Vanessa Fotso

Written by Vanessa Fotso

Health IT Software Engineer with broad technical exposure and passion for learning.