How Do You Install the Latest Version of Apache Ant?

Problem scenario
You want to use the same Bash script to install Apache Ant on CentOS/RedHat/Fedora, Debian/Ubuntu, or Linux SUSE.  How do you install the latest version of Apache Ant on to any distribution of Linux? 

Solution
Updated 4/8/21
With a "sudo bash" command, run a script with the following content (e.g., call the script below "installant.sh" and run "sudo bash installant.sh"):

#!/bin/bash
# Written by www.continualintegration.com

antversion=1.10.9  # Change this version as necessary

distro=$(cat /etc/*-release | grep NAME)

debflag=$(echo $distro | grep -i "ubuntu")
if [ -z "$debflag" ]
then   # If it is not Ubuntu, test if it is Debian.
  debflag=$(echo $distro | grep -i "debian")
  echo "determining Linux distribution..."
else
   echo "You have Ubuntu Linux!"
fi

rhflag=$(echo $distro | grep -i "red*hat")
if [ -z "$rhflag" ]
then   #If it is not RedHat, see if it is CentOS or Fedora.
  rhflag=$(echo $distro | grep -i "centos")
  if [ -z "$rhflag" ]
    then    #If it is neither RedHat nor CentOS, see if it is Fedora.
    echo "It does not appear to be CentOS or RHEL..."
    rhflag=$(echo $distro | grep -i "fedora")
    fi
fi

if [ -z "$rhflag" ]
  then
  echo "...still determining Linux distribution..."
else
  echo "You have a RedHat distribution (e.g., CentOS, RHEL, or Fedora)"
  yum -y install java-1.8.0-openjdk*
  JAVA_HOME=/usr/lib/jvm/jre-1.8.0-openjdk
  echo 'export JAVA_HOME=/usr/lib/jvm/jre-1.8.0-openjdk' >> ~/.bashrc
  source ~/.bashrc
  source /etc/environment
fi

if [ -z "$debflag" ]
then
  echo "...still determining Linux distribution..."
else
   echo "You are using either Ubuntu Linux or Debian Linux."
   apt-get -y update # This is necessary on new AWS Ubuntu servers.
   apt -y install openjdk-9-jre-headless
apt -y install openjdk-11-jre-headless
   apt-get -y remove ant
fi

suseflag=$(echo $distro | grep -i "suse")
if [ -z "$suseflag" ]
then
  if [ -z "$debflag" ]
  then
    if [ -z "$rhflag" ]
      then
      echo "*******************************************"
      echo "Could not determine the Linux distribution!"
      echo "Installation aborted. Nothing was done."
      echo "******************************************"
      exit
    fi
  fi
else
   zypper -n install java-1_8_0-openjdk
fi

curl http://ftp.wayne.edu/apache/ant/binaries/apache-ant-$antversion-bin.tar.gz > /bin/apache-ant-$antversion-bin.tar.gz

cd /bin
tar -xvf apache-ant-$antversion-bin.tar.gz
mkdir -p /usr/local/apache-ant
mv /bin/apache-ant-$antversion/* /usr/local/apache-ant

echo 'PATH="$PATH":/usr/local/apache-ant/bin/' > /etc/profile.d/ant.sh

echo 'To finish the process, run this command:'
echo 'export PATH="$PATH":/usr/local/apache-ant/bin/'

echo "Run 'ant -version' to see if ant is installed."

Leave a comment

Your email address will not be published. Required fields are marked *