33

Maven 2 is in the repositories, but not Maven 3. How do I properly install Maven 3? Guides that I found don't really work properly.

I am using Natty, but I am sure this won't change anything.

Seth
  • 58,122

8 Answers8

25

Maven3 is the default in Ubuntu 12.04 (precise)

Maven2 is the latest in the repositories for 11.10.

PPAs for maven3 seem rather scarce - here is one for maverick - although not the very latest version available.

The best advice is to stay with the stable version available for your distro version.

However if you like to keep up-to-date yourself it is very straightforward to download the code and install yourself.

Download the .tar.gz file from here and follow the installation guidance on the same web-page

fossfreedom
  • 172,746
25

Here is a maven3 PPA that worked fine for me on 64 bit Ubuntu 11.10

sudo add-apt-repository ppa:natecarlson/maven3
sudo apt-get update && sudo apt-get install maven3

If you have maven2 already installed from canonical repos this will not replace it but give you a mvn3 binary.

If you prefer to have the binary called mvn instead of mvn3 you can simply do the following:

sudo ln -s /usr/bin/mvn3 /usr/bin/mvn

Source

realgt
  • 713
4

The question is old, but gold. I had to find a solution today. Here we go. There is no need to use an unsecure repository, because maven3 is already included in the Ubuntu repos. A solution at Ubuntu 14.04 LTS will be like:

  • sudo apt-get install maven
  • sudo update-alternatives --config mvn

With the 2nd command you're asked to set the used version. You can distinguish the correct directories by selecting the path where maven2 is NOT part of it.

   *0    /usr/share/maven2/bin/mvn   200       Auto-Modus
    1    /usr/share/maven/bin/mvn    150       manueller Modus
    2    /usr/share/maven2/bin/mvn   200       manueller Modus

In my case I had to select 1

With the issued command mvn -v you may verify, if the setting succeeded and whether you selected the correct version. Below you have an example of the returned values.

  ~$ mvn -version
  Apache Maven 3.0.5
  Maven home: /usr/share/maven
  Java version: 1.7.0_75, vendor: Oracle Corporation
  Java home: /usr/lib/jvm/java-7-openjdk-amd64/jre
  Default locale: de_DE, platform encoding: UTF-8
  OS name: "linux", version: "3.13.0-45-generic", arch: "amd64", family: "unix"

Hope this helps you a bit.

Semo
  • 288
2

Here is maven3 PPA which can resolve this issue.

Follow the installation:

sudo apt-get purge maven maven2 maven3
sudo add-apt-repository ppa:andrei-pozolotin/maven3
sudo apt-get update && sudo apt-get install maven3

And now you have installed maven3:

nazar@lelyak-desktop ~ $ mvn -version
Apache Maven 3.3.3 (7994120775791599e205a5524ec3e0dfe41d4a06; 2015-04-22T14:57:37+03:00)
Maven home: /usr/share/maven3
Java version: 1.7.0_80, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-7-oracle/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.13.0-24-generic", arch: "amd64", family: "unix"

Link to Launchpad

catch23
  • 1,254
  • 9
  • 31
  • 42
  • 1
    Failed to fetch http://ppa.launchpad.net/natecarlson/maven3/ubuntu/dists/trusty/main/binary-amd64/Packages 404 Not Found – habitats Feb 04 '16 at 19:27
2

I have made a new script that should not only install Maven + add it to bin, but picks up if you are using Ubuntu as a VirtualBox guest and then attempts to mount existing '.m2' and maven folder which may/may not contain existing settings.

Maven remains the same if it runs on Windows or Unix, so you can use same Maven between both is this is your VirtualBox setup...

PS. Note the build of scripts that can mount/unmount shared folders from the /usr/local/bin :)

#!/bin/bash
#Author: Yucca Nel http://thejarbar.org
#Will restart system
PATH="/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/bin:."
export PATH

#Modify these variables as needed...
tempWork=/tmp/work
defaultStartScript=/etc/init.d/rc.local
defaultMaven=3.0.3
locBin=/usr/local/bin
mavenUsrLib=/usr/lib/maven

mkdir -p $mavenUsrLib
mkdir -p $HOME/.m2

read -p "Please [Enter] full path name of your local startup script ($defaultStartScript is the default). Please
make sure on this before providing a value by consulting documentation for your system:" locStartScript
locStartScript=${locStartScript:-$defaultStartScript}

read -p "Please [Enter] Maven Version ($defaultMaven is default):" mavenVersion
mavenVersion=${mavenVersion:-$defaultMaven}


if [ ! -f $locStartScript ]
then
    echo "The file you provided could not be found. Remember to include the full path and try again. Exiting in 7 secs..."
    sleep 7
    exit 1
fi

mkdir -p /$tempWork
cd /$tempWork

sudo wget http://mirrors.powertech.no/www.apache.org/dist//maven/binaries/apache-maven-$mavenVersion-bin.tar.gz
tar -zxvf ./*

#Move it to a more logical location
sudo mv -f ./apache-maven-$mavenVersion $mavenUsrLib/

#If you have Maven on Windows and use VirtualBox, you can set up the maven to be a virtualbox shared folder.
#The name must match the name used below (ignore if irrelevant to you).


if [ -f /sbin/mount.vboxsf ]
then
    sudo /sbin/umount $HOME/.m2
    sudo /sbin/umount $mavenUsrLib
    sudo /sbin/mount.vboxsf .m2 $HOME/.m2
    sudo /sbin/mount.vboxsf maven $mavenUsrLib
fi

if mountpoint -q $HOME/.m2 &&  mountpoint -q $mavenUsrLib
then
#Add it to the start script to automate process...
    sudo sed -ie '$d' $locStartScript
if ! grep "sudo /sbin/mount.vboxsf .m2 $HOME/.m2" $locStartScript
then
    echo "sudo /sbin/mount.vboxsf .m2 $HOME/.m2" | sudo tee -a $locStartScript
fi

if ! grep "sudo /sbin/mount.vboxsf maven $mavenUsrLib" $locStartScript
then
    echo "sudo /sbin/mount.vboxsf maven $mavenUsrLib" | sudo tee -a $locStartScript
fi
    echo "exit 0" | sudo tee -a $locStartScript
    sudo chmod +x $locStartScript

#Create a mount and unmount script file...
    rm -rf $tempWork/*
    echo '#!/bin/bash' > $tempWork/maven-mount.sh
    echo "sudo /sbin/mount.vboxsf .m2 $HOME/.m2" >> $tempWork/maven-mount.sh
    echo "sudo /sbin/mount.vboxsf maven $mavenUsrLib" >> $tempWork/maven-mount.sh
    echo "echo 'mounted maven'" >> $tempWork/maven-mount.sh
    echo "exit 0" >> $tempWork/maven-mount.sh

    echo '#!/bin/bash' > $tempWork/maven-umount.sh
    echo "sudo umount $HOME/.m2" >> $tempWork/netbeans-umount.sh
    echo "sudo umount $mavenUsrLib" >> $tempWork/netbeans-umount.sh
    echo "echo 'unmounted maven'" >> $tempWork/maven-mount.sh
    echo 'exit 0' >> $tempWork/maven-umount.sh

#Script for mounting ALL VirtualBox shared solders....
#If there isn't one create one...
if [ ! -f $locBin/mount-all-from-host.sh ]
then
    echo '#!/bin/bash' > $tempWork/mount-all-from-host.sh
    echo "sudo /sbin/mount.vboxsf .m2 $HOME/.m2" | sudo tee -a $tempWork/mount-all-from-host.sh
    echo "sudo /sbin/mount.vboxsf maven $mavenUsrLib" | sudo tee -a $tempWork/mount-all-from-host.sh
    echo "exit 0" | sudo tee -a $tempWork/mount-all-from-host.sh

#Otherwise if there is one, but no mount, add one...
elif ! grep "sudo /sbin/mount.vboxsf .m2 $HOME/.m2" $locBin/mount-all-from-host.sh
then
    sudo sed -ie '$d' $locBin/mount-all-from-host.sh
    echo "sudo /sbin/mount.vboxsf .m2 $HOME/.m2" | sudo tee -a $locBin/mount-all-from-host.sh
    echo "exit 0" | sudo tee -a $locBin/mount-all-from-host.sh

elif ! grep "sudo /sbin/mount.vboxsf maven $mavenUsrLib" $locBin/mount-all-from-host.sh
then
    sudo sed -ie '$d' $locBin/mount-all-from-host.sh
    echo "sudo /sbin/mount.vboxsf maven $mavenUsrLib" | sudo tee -a $locBin/mount-all-from-host.sh
    echo "exit 0" | sudo tee -a $locBin/mount-all-from-host.sh

fi

#Script for unmounting ALL VirtualBox shared folders...
#If there isn't one create one...
if [ ! -f $locBin/umount-all-from-host.sh ]
then
    echo '#!/bin/bash' > $tempWork/umount-all-from-host.sh
    echo "sudo umount -a -t vboxsf" | sudo tee -a $tempWork/umount-all-from-host.sh
    echo "echo 'unmounted all VirtualBox shared folders'" | sudo tee -a $tempWork/umount-all-from-host.sh
    echo "exit 0" | sudo tee -a $tempWork/umount-all-from-host.sh
fi

    sudo chmod +x $tempWork/*
    sudo mv -f $tempWork/*.sh $locBin/
    rm -rf $tempWork
fi

sudo ln -f -s $mavenUsrLib/apache-maven-$mavenVersion/bin/* /usr/bin/
sudo rm -rf $tempWork
sudo reboot

exit 0
thejartender
  • 141
  • 7
1

I upgraded but got a classnotfound error. To fix this:

After spending some time trying various combinations , I found that this is because I have both M2_HOME and M3_HOME set in my environment variables.Once I removed M2_HOME from my environment variables, I could get this working back again.May be this could save some serious time for some one.

lambda23
  • 3,232
0

Download maven from maven official site and extract.

Move the application directory to /usr/local

sudo cp -R apache-maven-X.X.X /usr/local

Make a soft link in /usr/bin for universal access of mvn

sudo ln -s /usr/local/apache-maven-X.X.X/bin/mvn /usr/bin/mvn

Verifify mvn installation

mvn --version
Thirumal
  • 979
  • 5
  • 13
  • 25
0

If you are not comfortable with a PPA (personal package archive) where you have no assurance of the provenance this is an alternative.

From a security perspective if you don't know where it came from don't install it.

In my linked article I retrieve the latest file from apache which is a known and trusted source. You can get the latest version

\#identify the latest version of maven
    latest=$(curl http://www-us.apache.org/dist/maven/maven-3/ | tac | sed -ne 's/[^0-9]*\(\([0-9]\.\)\{0,3\}[0-9]\).*/\1/p' | head -1)
\#download it
    wget http://www-us.apache.org/dist/maven/maven-3/$latest/binaries/apache-maven-$latest-bin.tar.gz

then install it from

\#Unpack it
    sudo tar -zxf apache-maven-$latest-bin.tar.gz -C /usr/local/
\#create a sym link to it
    sudo ln -s /usr/local/apache-maven-$latest/bin/mvn /usr/bin/mvn

as outlined in the link above and my post on

TomRed
  • 121
  • 1
    Please do not post the same answer to multiple questions. If the same information really answers both questions, then one question (usually the newer one) should be closed as a duplicate of the other. You can indicate this by voting to close it as a duplicate or, if you don't have enough reputation for that, raise a flag to indicate that it's a duplicate. Otherwise tailor your answer to this question and don't just paste the same answer in multiple places. – DavidPostill Apr 22 '17 at 12:03