54

Note: When originally posted I was trying to install maven2. Since the main answer is for maven3 I have updated the title. The rest of the question remains as it was originally posted.

I'm trying to install maven2 on a redhat linux box using the command

 yum install maven2

but yum doesn't seem to be able to find maven2.

No package maven2 available

I've run across other posts about this topic, but the answer to the following post suggests to add repos. I add said repos, but run into errors after adding them.

How to install Maven into Red Hat Enterprise Linux 6?

I can only access this box via command line so simply downloading maven from their website is difficult for me.

OrwellHindenberg
  • 4,988
  • 8
  • 38
  • 58

5 Answers5

107

Go to mirror.olnevhost.net/pub/apache/maven/binaries/ and check what is the latest tar.gz file

Supposing it is e.g. apache-maven-3.2.1-bin.tar.gz, from the command line; you should be able to simply do:

wget http://mirror.olnevhost.net/pub/apache/maven/binaries/apache-maven-3.2.1-bin.tar.gz

And then proceed to install it.

UPDATE: Adding complete instructions (copied from the comment below)

  1. Run command above from the dir you want to extract maven to (e.g. /usr/local/apache-maven)
  2. run the following to extract the tar:

    tar xvf apache-maven-3.2.1-bin.tar.gz
    
  3. Next add the env varibles such as

    export M2_HOME=/usr/local/apache-maven/apache-maven-3.2.1

    export M2=$M2_HOME/bin

    export PATH=$M2:$PATH

  4. Verify

    mvn -version
    
ntg
  • 12,950
  • 7
  • 74
  • 95
Icarus
  • 63,293
  • 14
  • 100
  • 115
  • 6
    That did it. I'll add a few more instructions for the extreme noobs out there (like myself :) ) 1. Run Icarus's command from the dir you want to extract maven too. 2. run the following to extract the tar tar xvf apache-maven-3.0.4-bin.tar.gz 3. Next add the env varibles such as export M2_HOME=/usr/local/apache-maven/apache-maven-3.0.4 export M2=$M2_HOME/bin export PATH=$M2:$PATH 4. Verify with mvn -version – OrwellHindenberg Aug 22 '12 at 17:09
  • 2
    When i run the command "mvn -version" i get the output "Exception in thread "main" java.lang.NoClassDefFoundError: org/codehaus/classworlds/Launcher"....Any idea why?? – Lucy Dec 04 '14 at 12:07
  • 1
    This worked on my first ssh session however I have to run point 3 each tie I open a new ssh session. That's quite annoying – Ced Jun 19 '16 at 14:02
  • 1
    @Ced just add those commands to your .bash_profile file and you won't ever have to do this again. – Icarus Jun 20 '16 at 14:49
  • I normally create a symbolic link without the explicit version (`ln -s /usr/local/apache-maven/apache-maven-3.2.1 /usr/local/apache-maven/current`) number, so that I can add `/usr/local/apache-maven/current` to `PATH` and not have to update `PATH` if I choose to install a newer version of Maven. – djb Jan 24 '17 at 21:06
  • I'm getting unmet dependency errors: ubuntu@ip-10-149-136-120:~$ mvn -version The program 'mvn' can be found in the following packages: * maven * maven2 Try: sudo apt-get install – FractalBob Jul 01 '17 at 21:56
  • ...followed by ubuntu@ip-10-149-136-120:~$ sudo apt-get install maven2 Reading package lists... Done Building dependency tree Reading state information... Done You might want to run 'apt-get -f install' to correct these: The following packages have unmet dependencies: linux-headers-generic : Depends: linux-headers-3.13.0-123-generic but it is not going to be installed linux-image-virtual : Depends: linux-image-3.13.0-123-generic but it is not going to be installed maven2 : Depends: libmaven2-core-java but it is not going to be installed – FractalBob Jul 01 '17 at 21:57
  • ...and Recommends: libmaven-clean-plugin-java but it is not going to be installed Recommends: libmaven-compiler-plugin-java (>= 2.5.1) but it is not going to be installed Recommends: libmaven-install-plugin-java (>= 2.4) but it is not going to be installed Recommends: libmaven-jar-plugin-java but it is not going to be installed Recommends: libmaven-resources-plugin-java but it is not going to be installed Recommends: libmaven-shade-plugin-java (>= 1.7.1) but it is not going to be installed – FractalBob Jul 01 '17 at 21:57
  • ...and finally E: Unmet dependencies. Try 'apt-get -f install' with no packages (or specify a solution). ubuntu@ip-10-149-136-120:~$ – FractalBob Jul 01 '17 at 21:58
  • BTW, I did run apt-get -f install and it seems to have completed without error. – FractalBob Jul 01 '17 at 21:59
  • For a new bee ...After downloading tar.gz file of latest Maven.We can move this .gz file to any path of the Linux server using Filezilla or WinSCP. Then start extracting .gz file and the steps told in Answer. Thanks! – Anurag_BEHS Sep 08 '17 at 17:08
5

I made the following script:

#!/bin/bash

# Target installation location
MAVEN_HOME="/your/path/here"

# Link to binary tar.gz archive
# See https://maven.apache.org/download.cgi?html_a_name#Files
MAVEN_BINARY_TAR_GZ_ARCHIVE="http://www.trieuvan.com/apache/maven/maven-3/3.3.9/binaries/apache-maven-3.3.9-bin.tar.gz"

# Configuration parameters used to start up the JVM running Maven, i.e. "-Xms256m -Xmx512m"
# See https://maven.apache.org/configure.html
MAVEN_OPTS="" # Optional (not needed)

if [[ ! -d $MAVEN_HOME ]]; then
  # Create nonexistent subdirectories recursively
  mkdir -p $MAVEN_HOME

  # Curl location of tar.gz archive & extract without first directory
  curl -L $MAVEN_BINARY_TAR_GZ_ARCHIVE | tar -xzf - -C $MAVEN_HOME --strip 1

  # Creating a symbolic/soft link to Maven in the primary directory of executable commands on the system
  ln -s $MAVEN_HOME/bin/mvn /usr/bin/mvn

  # Permanently set environmental variable (if not null)
  if [[ -n $MAVEN_OPTS ]]; then
    echo "export MAVEN_OPTS=$MAVEN_OPTS" >> ~/.bashrc
  fi

  # Using MAVEN_HOME, MVN_HOME, or M2 as your env var is irrelevant, what counts
  # is your $PATH environment.
  # See http://stackoverflow.com/questions/26609922/maven-home-mvn-home-or-m2-home
  echo "export PATH=$MAVEN_HOME/bin:$PATH" >> ~/.bashrc
else
  # Do nothing if target installation directory already exists
  echo "'$MAVEN_HOME' already exists, please uninstall existing maven first."
fi
ecwpz91
  • 1,527
  • 1
  • 13
  • 8
3

Pretty much what others said, but using "~/.bash_profile" and step by step (for beginners):

  1. Move to home folder and create a new folder for maven artifacts:
    • cd ~ && mkdir installed-packages
  2. Go to https://maven.apache.org/download.cgi and wget the latest artifact:
    • If you don't have wget installed: sudo yum install -y wget
    • cd ~/installed-packages
    • wget http://www-eu.apache.org/dist/maven/maven-3/3.5.0/binaries/apache-maven-3.5.0-bin.tar.gz
  3. Uncompress the downloaded file:
    • tar -xvf apache-maven-3.5.0-bin.tar.gz
  4. Create a symbolic link of the uncompressed file:
    • ln -s ~/installed-packages/apache-maven-3.5.0 /usr/local/apache-maven
  5. Edit ~/.bash_profile (This is where environment variables are commonly stored):
    • vi ~/.bash_profile
    • Add the variable: MVN_HOME=/usr/local/apache-maven (do this before PATH variable is defined)
      • (For those who don't know vi tool: Press i key to enable insert mode)
    • Go to the end of the line where PATH variable is defined and append the following: :$MVN_HOME:$MVN_HOME/bin
    • Save changes
      • (For those who don't know vi tool: Press esc key to exit insert mode and :wq! to save and quit file)
  6. Reload environment variables:
    • source ~/.bash_profile
  7. Confirm that maven command now works properly:
    • mvn --help
Antony Fuentes
  • 1,013
  • 9
  • 13
3

Installing maven in Amazon Linux / redhat

--> sudo wget http://repos.fedorapeople.org/repos/dchen/apache-maven/epel-apache-maven.repo -O /etc/yum.repos.d/epel-apache-maven.repo

--> sudo sed -i s/\$releasever/6/g /etc/yum.repos.d/epel-apache-maven.repo

-->sudo yum install -y apache-maven

--> mvn --version

Output looks like


Apache Maven 3.5.2 (138edd61fd100ec658bfa2d307c43b76940a5d7d; 2017-10-18T07:58:13Z) Maven home: /usr/share/apache-maven Java version: 1.8.0_171, vendor: Oracle Corporation Java home: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.171-8.b10.amzn2.x86_64/jre Default locale: en_US, platform encoding: UTF-8 OS name: "linux", version: "4.14.47-64.38.amzn2.x86_64", arch: "amd64", family: "unix"

*If its thrown error related to java please follow the below step to update java 8 *

Installing java 8 in amazon linux/redhat

--> yum search java | grep openjdk

--> yum install java-1.8.0-openjdk-headless.x86_64

--> yum install java-1.8.0-openjdk-devel.x86_64

--> update-alternatives --config java #pick java 1.8 and press 1

--> update-alternatives --config javac #pick java 1.8 and press 2

Thank You

sachin_ur
  • 2,375
  • 14
  • 27
0

Sometimes you may get "Exception in thread "main" java.lang.NoClassDefFoundError: org/codehaus/classworlds/Launcher" even after setting M2_HOME and PATH parameters correctly.

This exception is because your JDK/Java version need to be updated/installed.

Om Sao
  • 7,064
  • 2
  • 47
  • 61