Set up Gradle on Ubuntu 22.04: Quick and Efficient Steps

Posted on

Set up Gradle on Ubuntu 22.04: Quick and Efficient Steps

Set up Gradle on Ubuntu 22.04: Quick and Efficient Steps

This tutorial, brought to you by Orcacore, is designed to guide you through the process of how to Set up Gradle on Ubuntu 22.04. Gradle is a powerful build automation tool, particularly favored for JVM-based languages like Java, Groovy, and Scala. Gradle’s strength lies in its configurability, allowing you to define and execute custom Tasks for compiling JAR files, running tests, generating documentation, and much more.

The question often arises: Do I really need Gradle or a similar build system? The answer is a resounding yes if your project goes beyond the simplest "Hello World" example. For anything more complex, a build system like Gradle becomes indispensable.

Before we begin, ensure you’re logged into your Ubuntu 22.04 server as a non-root user with sudo privileges. If you haven’t already, follow our guide on Initial Server Setup with Ubuntu 22.04 to configure this.

Now, let’s proceed with the steps to install the latest release of Gradle directly from the official Gradle website, ensuring a smooth and efficient Set up Gradle on Ubuntu 22.04.

1. Install OpenJDK on Ubuntu 22.04

Gradle depends on Java SE 8 or later. So, our first step is to ensure a compatible Java Development Kit (JDK) is installed on your Ubuntu 22.04 machine.

First, update the package index on your system to ensure you have the latest package information. This is done using the following command:

sudo apt update

Next, install OpenJDK using the default-jre package. This will install the recommended Java Runtime Environment (JRE) for Ubuntu 22.04.

sudo apt install default-jre -y

To confirm that Java has been installed successfully and to verify its version, execute the following command:

java -version
openjdk version "11.0.18" 2023-01-17
OpenJDK Runtime Environment (build 11.0.18+10-Ubuntu-0ubuntu122.04.1)
OpenJDK 64-Bit Server VM (build 11.0.18+10-Ubuntu-0ubuntu122.04.1, mixed mode, sharing)

2. Install Gradle on Ubuntu 22.04

Now that Java is installed, we can proceed with installing Gradle itself. This involves downloading the Gradle distribution, extracting it, and configuring environment variables. The final step will confirm the successful Set up Gradle on Ubuntu 22.04.

Download the Gradle Build Tool

Visit the Gradle Releases page to find the latest version. Once you have identified the latest version, use the wget command to download the binary distribution to the /tmp directory. In this example, we’ll use version 8.0.2.

sudo wget https://downloads.gradle-dn.com/distributions/gradle-8.0.2-bin.zip -P /tmp

After the download is complete, extract the archive to the /opt/gradle directory. This location is a common convention for installing software manually.

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

Set up Environment Variables

To make Gradle accessible from anywhere in the system, you need to configure the PATH environment variable. This allows you to execute Gradle commands without specifying the full path to the executable.

export PATH=$PATH:/opt/gradle/gradle-8.0.2/bin

This command temporarily sets the PATH variable for the current session. To make it permanent, you need to add it to your shell’s configuration file (e.g., .bashrc or .zshrc). Open the file with a text editor:

nano ~/.bashrc

Add the following line to the end of the file:

export PATH=$PATH:/opt/gradle/gradle-8.0.2/bin

Save the file and source it to apply the changes:

source ~/.bashrc

Verify Gradle Installation

Finally, verify that Gradle is installed correctly by checking its version:

gradle -v
Welcome to Gradle 8.0.2!

Here are the highlights of this release:
 - Improved Kotlin DSL script editing experience
 - Introduce new configuration cache instrumentation APIs
 - Support for running tests in parallel in the IDE

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

------------------------------------------------------------
Gradle 8.0.2
------------------------------------------------------------

Build time:   2023-03-03 11:51:35 UTC
Revision:     c9f32a5f5b96bf4a9a0f3d3b62b38c64c5902f55

Kotlin:       1.8.10
Groovy:       3.0.17
Ant:          Apache Ant(TM) version 1.10.11 compiled on March 22 2021
JVM:          11.0.18 (Ubuntu 11.0.18+10-Ubuntu-0ubuntu122.04.1)
OS:           Linux 5.15.0-60-generic amd64

That completes the installation of Gradle on your Ubuntu 22.04 system.

Conclusion

You have successfully learned how to Set up Gradle on Ubuntu 22.04. Gradle streamlines the build process by automating tasks such as compiling, testing, packaging, and deploying applications. Now you are set to use Gradle in your JVM-based projects.

Consider subscribing to us on Facebook and Twitter for more tutorials and updates.

You might also find these articles helpful:

Apache Maven installation on Ubuntu 22.04

Install chkrootkit Ubuntu 22.04

Tesseract OCR Setup on Ubuntu 22.04

Install Anaconda Python Ubuntu 22.04

Nginx Let’s Encrypt Ubuntu 22.04

CSF Firewall Setup Ubuntu 22.04

Alternative Solutions for Installing Gradle on Ubuntu 22.04

While the above method details how to manually download and install Gradle, there are alternative approaches that can simplify the process. Here are two different ways to achieve the same result:

1. Using SDKMAN! (Software Development Kit Manager)

SDKMAN! is a versatile tool for managing multiple software development kits (SDKs) on Unix-like systems. It provides a convenient way to install, manage, and switch between different versions of SDKs, including Gradle. This method offers the advantage of easy updates and version management.

Explanation:

SDKMAN! simplifies the installation and management of various SDKs. It handles the download, installation, and environment configuration automatically. It also allows you to easily switch between different Gradle versions if needed.

Steps:

  1. Install SDKMAN!: Open a terminal and run the following command:

    curl -s "https://get.sdkman.io" | bash

    Follow the on-screen instructions to complete the installation. You will likely need to open a new terminal or source your .bashrc file.

  2. Install Gradle: Once SDKMAN! is installed, use the following command to install the latest stable version of Gradle:

    sdk install gradle

    SDKMAN! will download and install Gradle, automatically configuring the necessary environment variables.

  3. Verify Installation: Verify the installation by checking the Gradle version:

    gradle -v

    The output should display the version of Gradle installed by SDKMAN!.

Code Example:

The above commands are sufficient for installation. There are no specific code examples related to building applications, which is where you will see Gradle in action.

2. Using Snap Package Manager

Snap is a package management system developed by Canonical (the company behind Ubuntu). It allows you to easily install and manage applications and their dependencies in a sandboxed environment. This can be a quick and easy solution for installing Gradle.

Explanation:

Snap packages are self-contained and include all the necessary dependencies, simplifying the installation process. They also provide automatic updates and security features. However, they can sometimes be larger in size compared to traditional packages.

Steps:

  1. Install Gradle: Open a terminal and run the following command:

    sudo snap install gradle

    This command installs the latest stable version of Gradle from the Snap store.

  2. Set up Environment Variables (Optional): By default, Snap applications are not added to the system’s PATH. If you want to run Gradle commands directly from the terminal without specifying the full path, you might need to create an alias or manually add the Snap’s bin directory to your PATH. This step is sometimes required but not always. The snap installation should handle this.
  3. Verify Installation: Verify the installation by checking the Gradle version:

    gradle -v

    If the command is not recognized, you may need to use the full path to the Gradle executable:

    /snap/bin/gradle -v

    If that works, you do need to update your path or create an alias.

Code Example:

The commands above are sufficient for installing Gradle via snap. Again, application building examples aren’t relevant here, but rather in actually using the installed Gradle.

These alternative methods provide different approaches to installing Gradle on Ubuntu 22.04, catering to various preferences and system configurations. Choosing the right method depends on your specific needs and familiarity with the tools involved. No matter which installation method you choose, remember that the result is the same: the ability to use Gradle for your Java, Groovy, or Scala projects. The Set up Gradle on Ubuntu 22.04 process can be achieved in multiple ways.

Leave a Reply

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