# How to install Gradle on Ubuntu 18.04

By Atta Ur Rehman Shah (https://attacomsian.com/about). Published Sep 22, 2019, updated Oct 14, 2022. Topics: Ubuntu, DevOps, Gradle.
Canonical URL: https://attacomsian.com/blog/install-gradle-on-ubuntu

> A step-by-step guide to installing the latest version of Gradle on a Ubuntu 18.04 machine.

[Gradle](https://gradle.org/) 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 develop almost any type of software.

In this tutorial, you will learn how to install the latest version of Gradle on Ubuntu 18.04. The 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 the following:

1. A Ubuntu 18.04 machine (get one from [DigitalOcean](https://attacomsian.com/go/digitalocean))
2. A [user with sudo privileges](https://attacomsian.com/blog/create-user-with-sudo-privileges-on-ubuntu)

## 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 &mdash; Update the System

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

```bash
# update package index
$ sudo apt update

# upgrade packages
$ sudo apt -y upgrade
```

### Step 2 &mdash; 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](https://attacomsian.com/blog/install-java-on-ubuntu) is pretty much straightforward. You can install OpenJDK binaries from Ubuntu official repositories by running the following command:

```bash
$ sudo apt install default-jdk
```

Next, verify the Java installation by typing:

```bash
$ java -version
```

You will see the following output:

```bash
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 &mdash; Download Gradle

You can download Gradle from [official Gradle release page](https://gradle.org/releases/). When writing this article, the latest Gradle version is `5.6.2`. 

Two kinds of **Gradle archives** are available on the release page: **binary-only** and **complete**. The "binary-only" archive includes the Gradle build tool only, and the "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:

```bash
$ 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:

```bash
$ sudo mkdir /opt/gradle
```

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

```bash
$ 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 the `unzip` package.

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

```bash
$ ls /opt/gradle/gradle-5.6.2/
```

You should see the following output:

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

### Step 4 &mdash; 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:

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

Now paste the following lines inside the above file:

<p class="file-desc"><span>gradle.sh</span></p>

```bash
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:

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

Load the environment variables by using the `source` command:

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

### Step 5 &mdash; Verify Installation


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

```bash
$ gradle -v
```

You should see the following output:

```bash
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 time to check out the official [Gradle documentation](https://docs.gradle.org/current/userguide/userguide.html) page to get started with Gradle.

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