Gradle is an open-source build automation tool primarily used for building Java projects. It is based on the concepts of Apache Ant and Apache Maven, and uses Groovy instead of XML to define the project and build scripts.

Gradle is a general-purpose build tool that makes fewer assumptions about the underlying technology you are trying to build. This makes it flexible enough to build almost any type of software.

In this tutorial, you will learn how to install the latest version of Gradle on Ubuntu 18.04. The same instructions are applicable for Ubuntu 16.04 and other Ubuntu-based distributions like Kubuntu, Linux Mint and Elementary OS.

Prerequisites

To complete this tutorial successfully, you need:

Installing Gradle on Ubuntu

The following sections explain a step by step process to download and install the latest version of Gradle on Ubuntu 18.0. We will be downloading the current Gradle release from their official website.

Step 1 — Update the System

Before installing new packages on your Ubuntu system, run the following commands to update the system:

# update package index
$ sudo apt update

# upgrade packages
$ sudo apt -y upgrade

Step 2 — Install OpenJDK

Gradle requires Java Development Kit (JDK) 8 or higher to run. We will install the latest OpenJDK TLS version (Java 11) on the Ubuntu machine.

Java installation is pretty much straightforward. You can install OpenJDK binaries from Ubuntu official repositories by running the following command:

$ sudo apt install default-jdk

Next, verify the Java installation by typing:

$ java -version

You will see the following output:

openjdk version "11.0.4" 2019-07-16
OpenJDK Runtime Environment (build 11.0.4+11-post-Ubuntu-1ubuntu218.04.3)
OpenJDK 64-Bit Server VM (build 11.0.4+11-post-Ubuntu-1ubuntu218.04.3, mixed mode, sharing)

Step 3 — Download Gradle

You can download Gradle from official Gradle release page. At the time of writing this article, the latest Gradle version is 5.6.2.

There are two kinds of Gradle archives available on the release page: binary-only and complete. The "binary-only" archive includes the Gradle build tool only whereas "complete" archive comes with binary, documentation, and source code.

Let us start downloading the Gradle binary-only archive to the /tmp directory by running the following wget command:

$ wget https://services.gradle.org/distributions/gradle-5.6.2-bin.zip -P /tmp

Once the download is finished, create a new directory for Gradle installation:

$ sudo mkdir /opt/gradle

Next, extract the Gradle archive in the newly created directory:

$ sudo unzip -d /opt/gradle /tmp/gradle-*.zip

Note: If you see an error Command 'unzip' not found, type sudo apt install unzip to install unzip package.

Verify that the Gradle files are extracted successfully by listing the /opt/gradle/gradle-5.6.2 directory:

$ ls /opt/gradle/gradle-5.6.2/

You should see the following output:

LICENSE  NOTICE  bin  getting-started.html  init.d  lib  media

Step 4 — Setup Environment Variables

The next step is to configure the PATH variable to include the Gradle bin directory. It will enable users to execute the gradle command anywhere on the system.

Create a new file named gradle.sh inside the /etc/profile.d/ directory by typing the following command:

$ sudo nano /etc/profile.d/gradle.sh

Now paste the following lines inside the above file:

gradle.sh

export GRADLE_HOME=/opt/gradle/gradle-5.6.2
export PATH=${GRADLE_HOME}/bin:${PATH}

Save and close the file. Next, run the following command to make the script executable:

$ sudo chmod +x /etc/profile.d/gradle.sh

Load the environment variables by using the source command:

$ source /etc/profile.d/gradle.sh

Step 5 — Verify Installation

To verify that Gradle was installed successfully, run the following command:

$ gradle -v

You should see the following output:

Welcome to Gradle 5.6.2!

Here are the highlights of this release:
 - Incremental Groovy compilation
 - Groovy compile avoidance
 - Test fixtures for Java projects
 - Manage plugin versions via settings script

For more details see https://docs.gradle.org/5.6.2/release-notes.html


------------------------------------------------------------
Gradle 5.6.2
------------------------------------------------------------

Build time:   2019-09-05 16:13:54 UTC
Revision:     55a5e53d855db8fc7b0e494412fc624051a8e781

Kotlin:       1.3.41
Groovy:       2.5.4
Ant:          Apache Ant(TM) version 1.9.14 compiled on March 12 2019
JVM:          11.0.4 (Ubuntu 11.0.4+11-post-Ubuntu-1ubuntu218.04.3)
OS:           Linux 4.15.0-1021-aws amd64

That's it. The latest version of Gradle is now installed on your Ubuntu 18.04 machine. You are ready to build Java programs using Gradle.

Conclusion

In this article, you have learned to install the latest version of Gradle on a Ubuntu 18.04 system. It is a good time to check out the official Gradle documentation page to learn how to get started with Gradle.

The same instructions are also applicable for Ubuntu 16.04 and other Ubuntu-based distribution like Kubuntu, Linux Mint, and Elementary OS.

✌️ Like this article? Follow @attacomsian on Twitter. You can also follow me on LinkedIn and DEV. Buy me a coffee (cost $3)

Need help to start a new Spring Boot or MEAN stack project? I am available for contract work. Hire me to accomplish your business goals with engineering and design. Let’s talk about your project: hi@attacomsian.com.