Gradle is an open-source automation tool for building applications. It supports various languages including Java, Android, C/C++, and JavaScript. It is preferred by millions of developers due to its ability to automate the creation of applications which involves compiling, testing, deploying, and publishing applications.
In an earlier post, we described the installation of Gradle on CentOS. This post explains how to install Gradle on Ubuntu.
Note: We will be demonstrating the procedure on Ubuntu 20.04 LTS (Focal Fossa) machine.
Prerequisites
For Gradle, you must have Java JDK or JRE version 8 or above installed on your Ubuntu machine. To verify if Java is running, use the command below:
$ java -version
An output similar to the following will indicate that Java is installed on your machine.
To install Gradle on Ubuntu, there are following three methods:
- Installation via Direct Download
- Installation via Snap
- Installation via Apt
Installation via Direct Download
In the following method, we will install Gradle by downloading the zip package available at Gradle official website. Using this installation method, you can get the latest version of Gradle. The complete steps for the installation of Gradle via Direct Download are as follows:
1. To download Gradle’s latest version, visit the Releases page and download the binary package. As of May 2021, the latest version of Gradle available on the official Releases page is 7.0.2. So here, we are going to download the Gradle version 7.0.2. You can download it from the Releases page or use the following command to download it from the command line:
$ wget https://downloads.gradle-dn.com/distributions/gradle-7.0.2-bin.zip
2. The downloaded file is in zip format, you will need to extract it. Use the command below to unzip it in /opt/gradle directory.
$ sudo unzip -d /opt/gradle
To verify if the file has been extracted, use the command below:
$ ls /opt/gradle/gradle-7.0.2
You should see the following content in your output:
3. Now, you will need to setup the environment variable. You can do this by creating a new file inside the /etc/profile.d directory:
$ sudo nano /etc/profile.d/gradle.sh
Add the following lines in the file:
export GRADLE_HOME=/opt/gradle/gradle-7.0.2
export PATH=${GRADLE_HOME}/bin:${PATH}
Then save and close the file.
4. Then give the /etc/profile.d/gradle.sh file execute permission using the command below:
$ sudo chmod +x /etc/profile.d/gradle.sh
5. Now load the environment variables as follows:
$ source /etc/profile.d/gradle.sh
6. To verify the installation of Gradle and to view the version installed, use the command below:
$ gradle -v
The following output indicates Gradle version 7.0.2 has been installed.
Installation via Snap
The snap package for Gradle is also available for installation on Ubuntu. This method also installs the latest version of Gradle on your system.
The complete steps are as follows:
1. If snapd is not installed already on your system, you can install it as follows:
$ sudo apt update
$ sudo apt install snapd
2. Then install Gradle snap using the command below:
$ sudo snap install gradle --classic
The following output indicates Gradle version 7.0.2 has been installed.
Or you can use the following command to view detailed information:
$ /snap/bin/gradle -v
Installation via Apt
Another method to install Gradle is via apt. However, this method does not install the latest version of Gradle. The complete steps for the installation of Gradle via apt are as follows:
1. Update the repository index using this command:
$ sudo apt update
5. Now to install Gradle on your system, use the command below:
$ sudo apt install gradle -y
This command will install Gradle on your system.
6. To verify the installation of Gradle and to view the version installed, use the command below:
$ gradle -v
The following output indicates Gradle version 4.4.1 has been installed.
Uninstall Gradle
If you want to remove Gradle from your system, you can do so using the following methods:
If you have installed Gradle using the direct download method, you can uninstall it as follows:
$ sudo rm gradle-7.0.2-bin.zip
$ sudo rm -r /opt/gradle /gradle.sh
$ sudo rm -r /usr/lib/gradle/
To remove Gradle installed via snap, use the command below to remove it:
$ sudo snap remove gradle
To remove Gradle installed via apt, use the command below to remove it:
$ sudo apt remove gradle
In this post, we described the installation of Gradle on the Ubuntu system using three different ways. We described step by step procedure for each of the installation procedures. In the end, we also described how to uninstall Gradle if you ever have to do that.