Debian

How to Install Gradle on Debian 10

How to Install Gradle on Debian 10

Gradle is a powerful tool that can be used to automate the compilation and execution of source code on various platforms. It was originally created by Johnson Markham in April 2012, but after he passed away from cancer it became active again under Google’s ownership with help from many others who continue developing this great project today.

One of the main advantages of Gradle is its flexibility. It can be used to build both simple and complex projects, and it can be easily customized to suit the needs of each project.

Gradle also integrates well with other development tools. For example, it can be used to compile Java code, run unit tests, and create Javadoc documentation. Another advantage of Gradle is its speed. It can quickly build large projects without bogging down your computer’s CPU or memory.

From mobile apps to web applications, Gradle can be used to build just about any type of software. If you’re looking for a build automation system that is both powerful and customizable, then Gradle is definitely worth considering.

Gradle is written in Java and uses Groovy as its scripting language. Groovy is a powerful JVM-based language that was designed for use with Java. If you’re not familiar with Groovy, don’t worry – Gradle can be used without knowing any of its features. However, if you want to take advantage of some of the more advanced features of Gradle, it’s worth learning at least a little bit about Groovy.

Gradle is similar to Apache Maven, a popular build automation system for Java projects. However, there are some key differences between Gradle and Maven:

• Powerful dependency management – The dependency management tool allows you to quickly import external libraries into your project. By contrast, Apache Maven uses a central repository that contains thousands of commonly used libraries that have been submitted by the community. This makes it easier to share code between developers, but if you’re working on a smaller project then it may not be worth using this feature.

• Easier script configuration – With Gradle you can configure each build task using a separate Groovy script file. You can also configure different tasks at the same time by adding additional scripts to a single file. Apache Maven uses a XML-based configuration file, which can be more difficult to manage if you have a large project.

• More powerful plugin system – Gradle has a powerful plugin system that allows you to add custom tasks and plugins to your project. This can be useful for integrating with other development tools, or for automating common tasks. Apache Maven doesn’t have as many plugins available, and the ones that are available are not always as powerful as those in Gradle.

If you’re considering using Gradle for your next project, be sure to check out the Gradle website for more information on how to get started. There are also plenty of good books and articles on the subject, so be sure to do some research before you dive in.

Prerequisites

In order to install Gradle on Debian 10, you will need to have:

  • A server running Debian 10 with a minimum of 2GB of RAM
  • A user account with sudo privileges

Updating the System

Gradle will compile code, so it’s best to have an up-to-date system. You can update your Debian server using the following command:

sudo apt update && sudo apt upgrade -y

If you are not yet logged in as root, then you will be asked for your sudo password. Otherwise, the required packages will be automatically upgraded, and your system will be updated.

Installing Java

If you plan to use Gradle in a Java project, then it’s best to install the JDK. The JDK (Java Development Kit) contains everything you need to compile and run java programs. The most recent version of the JDK available is OpenJDK 17.

But we will use the JDK 11 because it has long-term support and is the official reference implementation for Java SE. Also, some old library dependencies are not compatible with JDK 11.

You can install Oracle JDK 11 using this command.

sudo apt install openjdk-11-jdk -y

To check if you installed your JDK successfully, open a new terminal window and run the command below.

The -version command is a simple command that can run any program and shows the version for that particular program. For java, it will show the version of Java that you’ve installed on your machine. The result of this command depends on which Jave you have installed.

java -version

You will get an output similar to the one below. If you have installed OpenJDK, your output will show a version of 11. This is what you should expect with a successful installation.

version “11.0.12” 2021-07-20 indicates that the JDK was successfully installed

OpenJDK Runtime Environment (build 11.0.12+7-post-Debian-2deb10u1) means that the current OpenJDK version is 11.0.12

OpenJDK 64-Bit Server VM (build 11.0.12+7-post-Debian-2deb10u1, mixed mode, sharing) means that the VM is running in 64-bit mode and shares memory with other processes.

Configuring Java 11

In order to use Gradle with Java 11, we need to configure our environment variables. We can do this by editing the /etc/environment file.

A variable is a named container for a value that can be changed. In the context of this article, we will set the JAVA_HOME environment variable to the path of our JDK installation.

First, run the command below to get a list of all the installed Java versions. The update-alternatives command is used to configure the system for choosing which terminal program to use. It can also be used to choose which Java version to use.

sudo update-alternatives --config java

You should see an output similar to the one below.

You can see that OpenJDK 11 is located at /usr/lib/jvm/java-11-openjdk-amd64/bin/java. We need to add this path to our environment variable in order for it to work properly.

Open the /etc/environment file in your favorite editor. This example uses nano.

sudo nano /etc/environment

Copy and paste the following line to the bottom of the file and save it. This sets the JAVA_HOME environment variable to the path of the Java 11 installation.

Notice that we will remove the trailing bin/java part of the path prior to adding it to our environment variables. This ensures that when Java is called, Gradle will use this version instead of calling OpenJDK 11 directly.

JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"

Save the file and close it. Run the following source command to reload your environment variables. You can also do this by logging out and logging back in.

source /etc/environment

Now, run the echo command to verify that it worked.

echo $JAVA_HOME

The output will be the path of the JDK installation. If you don’t see this, then something went wrong! Check the /etc/environment file to make sure you added the correct line.

Installing Gradle

Now that we have Java 11 configured, we can install Gradle. We’ll be using the binary distribution for Gradle.

We can download the Gradle binaries directly from the Gradle website. The Gradle binaries are pre-built packages for Gradle that have already been built by the creators of Gradle.

Most people will use these binaries to install Gradle because it is much easier than building them yourself. The benefit of this is twofold: First, your machine doesn’t have to build anything, which can take a long time. Second, the binaries are guaranteed to be compatible with your machine and Gradle versions.

The Gradle downloads page has different binaries for different operating systems. We’ll be using the one for Linux.

Run the wget command below to download the Gradle binaries. https://services.gradle.org/distributions/gradle-6.3-bin.zip is the URL of the Gradle binaries. -P /tmp specifies the local directory to download the file to

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

When the download is finished, we’ll need to unzip it with the unzip command.

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

d is a flag that specifies the destination directory for the file contents. /opt/gradle is the local directory to extract Gradle to. ~/tmp/gradle-*.zip is an example of specifying the name of the files to unzip. This works because unzip by default unzips all files with .zip as their extension.

This will create a new directory at /opt/gradle/ called gradle-6.3. You can verify that this works by checking out its contents with the ls command.

ls /opt/gradle/gradle-*

The output will be a list of all the files in the directory, including the gradle-6.3 directory that we just created.

At this point, we have Gradle installed, but it isn’t configured. We need to configure the path to the Gradle executable in our environment variables.

At this point, we have Gradle installed, but it isn’t configured. We need to configure the PATH to the Gradle executable in our environment variables.

PATH is an environment variable that specifies a list of directories that the shell should search for executable files. This is useful because it allows us to have multiple versions of Gradle installed on our machine and be able to call them easily from the command line.

We can configure the path to the Gradle executable by setting the GRADLE_HOME environment variable. This variable will tell Gradle where to find its home directory.

The command below will create a new file named gradle.sh in the /etc/profile.d directory.

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

Paste the following code into the file, making sure to replace the path with the correct path on your machine.

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

Where:

export GRADLE_HOME=/opt/gradle/gradle-6.3 sets the GRADLE_HOME environment variable to /opt/gradle/gradle-6.3

export PATH=${GRADLE_HOME}/bin:${PATH} sets the PATH environment variable to ${GRADLE_HOME}/bin:${PATH} The ${PATH} variable is important because it makes Gradle accessible from the command line.

Now we just need to save and exit by typing CTRL+X to save the file as a .sh file, hitting ENTER, and then typing Y and ENTER.

Next, run the sudo chmod +x /etc/profile.d/gradle.sh command to make the file executable. When you create a new file in the /etc/profile.d directory, it will not be executable by default. So we need to make the file executable so that our system can run it when we log in.

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

We can now load our changes by running the source command below. This will tell the current shell to use these environment variables so we can be sure Gradle is working.

source /etc/profile.d/gradle.sh

Finally, we can verify that everything is working by running the gradle command below.

gradle -v

The output will show you the Gradle version that is currently installed. In this case, it should be 6.3. If you see a different version or no version at all, then something may have gone wrong with the installation process. Check the steps above to see if you missed anything.

Conclusion

Installing Gradle on a Debian 10 machine is a fairly simple process. It can be done with APT, and the installation of Java 11 will automatically bring along any dependencies that are needed to make this work properly.

Similar Posts