Cyberithub

How to Install Groovy on Ubuntu 20.04 LTS (Focal Fossa)

Advertisements

In this article, I will take you through the steps to install groovy on Ubuntu 20.04 LTS (Focal Fossa). If you are from Java Background, then you can use groovy to bring the power of advanced language features such as closures, dynamic methods, and the Meta Object Protocol (MOP) to the Java platform. Your Java knowledge won't become obsolete by walking down the Groovy path. Groovy allows you to build  experience and familiarity with the Java platform hence providing an option to choose when you use which tool and when to combine the two seamlessly.

According to official website, "Apache Groovy is a powerful, optionally typed and dynamic language, with static-typing and static compilation capabilities, for the Java platform aimed at improving developer productivity. It integrates smoothly with any Java program, and immediately delivers to your application powerful features, including scripting capabilities, Domain-Specific Language authoring, runtime and compile-time meta-programming and functional programming". While it can be easily installed on various Linux and Windows platforms but here we will only see the steps to install it on Ubuntu 20.04 LTS System.

How to Install Groovy on Ubuntu 20.04 LTS (Focal Fossa)

How to Install Groovy on Ubuntu 20.04 LTS (Focal Fossa)

Also Read: How to Install Wireshark on Ubuntu 20.04 LTS (Focal Fossa)

Step 1: Prerequisites

a) You should have a running Ubuntu 20.04 LTS Server.

b) You should have sudo or root access to run privileged commands.

c) You should have apt or apt-get utility available in your Server.

d) You should have curl utility available in your System.

 

Step 2: Update Your Server

Before going through the steps to install groovy in your system, it is absolutely important to first sync your system packages with the latest available versions from default Ubuntu repo by running sudo apt update command as shown below. This will also update the package cache for any recent repo changes done in the system.

cyberithub@ubuntu:~$ sudo apt update
[sudo] password for cyberithub:
Hit:1 https://download.docker.com/linux/ubuntu focal InRelease
Hit:2 https://dl.google.com/linux/chrome/deb stable InRelease
Get:3 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB]
Hit:4 http://in.archive.ubuntu.com/ubuntu focal InRelease
Hit:5 http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu focal InRelease
Get:6 http://in.archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]
Hit:7 http://ppa.launchpad.net/wireshark-dev/stable/ubuntu focal InRelease
Hit:8 https://apt.boltops.com stable InRelease
Get:9 http://security.ubuntu.com/ubuntu focal-security/main i386 Packages [508 kB]
Get:10 http://in.archive.ubuntu.com/ubuntu focal-backports InRelease [108 kB]
Get:11 http://in.archive.ubuntu.com/ubuntu focal-updates/main i386 Packages [738 kB]
Get:12 http://security.ubuntu.com/ubuntu focal-security/main amd64 Packages [1,785 kB]
..............................................

If any of the packages needs to be upgraded then run sudo apt upgrade command as well.

cyberithub@ubuntu:~$ sudo apt upgrade
[sudo] password for cyberithub:
Reading package lists... Done
Building dependency tree
Reading state information... Done
Calculating upgrade... Done
Try Ubuntu Pro beta with a free personal subscription on up to 5 machines.
Learn more at https://ubuntu.com/pro
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

 

Step 3: Install Java

Since Java needs to be installed for groovy to work, so we will either install default-jdk package which might not be the latest one or we can install the latest available package using sudo apt install openjdk-17-jdk command as shown below. But if you decided to install default-jdk then run sudo apt install default-jdk command.

cyberithub@ubuntu:~$ sudo apt install openjdk-17-jdk
Reading package lists... Done
Building dependency tree
Reading state information... Done
Suggested packages:
openjdk-17-demo openjdk-17-source visualvm
The following NEW packages will be installed:
openjdk-17-jdk
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 10.5 kB of archives.
After this operation, 55.3 kB of additional disk space will be used.
Get:1 http://in.archive.ubuntu.com/ubuntu focal-updates/universe amd64 openjdk-17-jdk amd64 17.0.4+8-1~20.04 [10.5 kB]
Fetched 10.5 kB in 0s (23.9 kB/s)
Selecting previously unselected package openjdk-17-jdk:amd64.
(Reading database ... 264858 files and directories currently installed.)
Preparing to unpack .../openjdk-17-jdk_17.0.4+8-1~20.04_amd64.deb ...
Unpacking openjdk-17-jdk:amd64 (17.0.4+8-1~20.04) ...
Setting up openjdk-17-jdk:amd64 (17.0.4+8-1~20.04) ...
update-alternatives: using /usr/lib/jvm/java-17-openjdk-amd64/bin/jconsole to provide /usr/bin/jconsole (jconsole) in auto mode

 

Step 4: Install SDKMAN

Since groovy will be installed through sdkman so you need to first install this software development kit manager by running sudo curl -s "https://get.sdkman.io" | bash as shown below.

cyberithub@ubuntu:~$ sudo curl -s "https://get.sdkman.io" | bash
[sudo] password for cyberithub:

                                 -+syyyyyyys:
                             `/yho:`       -yd.
                          `/yh/`             +m.
                        .oho.                 hy .`
                      .sh/`                   :N`                `-/o` `+dyyo:.
                    .yh:`                     `M-          `-/osysoym  :hs` `-+sys:      hhyssssssssy+
                  .sh:`                       `N:          ms/-``  yy.yh-      -hy.    `.N-````````+N.
                `od/`                         `N-       -/oM-       ddd+`    `sd:     hNNm        -N:
               :do`                           .M.       dMMM-      `ms.     /d+`     `NMMs       `do
             .yy-                             :N`    ```mMMM.       -     -hy.       /MMM:       yh
           `+d+`            `:/oo/`      `-/osyh/ossssssdNMM`           .sh:         yMMN`      /m.
          -dh-            :ymNMMMMy `-/shmNm-`:N/-.`` `.sN              /N-         `NMMy      .m/
        `oNs`          -hysosmMMMMydmNmds+-.:ohm         :               sd`        :MMM/      yy
       .hN+           /d: -MMMmhs/-.`      .MMMh  .ss+-                  `yy`       sMMN`     :N.
      :mN/           `N/  `o/-`            :MMMo  +MMMN-        .`        `ds       mMMh      do
     /NN/            `N+....--:/+oooosooo+:sMMM:  hMMMM:       `my         .m+     -MMM+     :N.
    /NMo              -+ooooo+/:-....`...:+hNMN. `NMMMd`       .MM/         -m:    oMMN.     hs
   -NMd`                                     :mm -MMMm- .s/    -MMm.         /m-   mMMd     -N.
  `mMM/                                       .- /MMh. -dMo    -MMMy          od. .MMMs..---yh
  +MMM.                                          sNo`.sNMM+    :MMMM/          sh`+MMMNmNm+++-
  mMMM-                                          /--ohmMMM+    :MMMMm.         `hyymmmdddo
  MMMMh.                  ````                 `-+yy/`yMMM/    :MMMMMy         -sm:.``..-:-.`
  dMMMMmo-.``````..-:/osyhddddho.          `+shdh+.   hMMM:    :MmMMMM/     ./yy/` `:sys+/+sh/
  .dMMMMMMmdddddmmNMMMNNNNNMMMMMs          sNdo-      dMMM-    `-/yd/MMMMm-:sy+.  :hs-      /N`
   `/ymNNNNNNNmmdys+/::----/dMMm:         +m-         mMMM+ohmo/.` sMMMMdo-     .om:       `sh
      `.-----+/.` `.-+hh/`              `od.          NMMNmds/     `mmy:`      +mMy       `:yy.
            /moyso+//+ossso:.          .yy`          `dy+:`         ..        :MMMN+---/oys:
          /+m: `.-:::-`               /d+                                     +MMMMMMMNh:`
         +MN/                       -yh.                                      `+hddhy+.
        /MM+                      .sh:
       :NMo                     -sh/
      -NMs                   `/yy:
     .NMy                 `:sh+.
    `mMm`              ./yds-
   `dMMMmyo:-.````.-:oymNy:`
   +NMMMMMMMMMMMMMMMMms:`
     -+shmNMMMNmdy+:`


Now attempting installation...


Looking for a previous installation of SDKMAN...
Looking for unzip...
Looking for zip...
Looking for curl...
Looking for sed...
Installing SDKMAN scripts...
Create distribution directories...
Getting available candidates...
Prime the config file...
Installing script cli archive...
* Downloading...
######################################################################## 100.0%
* Checking archive integrity...
* Extracting archive...
* Copying archive contents...
* Cleaning up...

* Downloading...
######################################################################## 100.0%
* Checking archive integrity...
* Extracting archive...
* Copying archive contents...
* Cleaning up...

Set version to 5.16.0 ...
Set native version to 0.0.15 ...
Attempt update of interactive bash profile on regular UNIX...
Added sdkman init snippet to /home/cyberithub/.bashrc
Attempt update of zsh profile...
Updated existing /home/cyberithub/.zshrc




All done!


You are subscribed to the STABLE channel.

Please open a new terminal, or run the following in the existing one:

source "/home/cyberithub/.sdkman/bin/sdkman-init.sh"

Then issue the following command:

sdk help

Enjoy!!!

Then run below source command to initialize sdkman either in the current terminal or in the new terminal.

cyberithub@ubuntu:~$ source "/home/cyberithub/.sdkman/bin/sdkman-init.sh"

 

Step 5: Install Groovy

Once sdkman is installed successfully, the next step is to install groovy by using sdk install groovy command as shown below.

cyberithub@ubuntu:~$ sdk install groovy
==== BROADCAST =================================================================
* 2022-10-18: micronaut 3.7.2 available on SDKMAN!
* 2022-10-17: gradle 6.9.3 available on SDKMAN!
* 2022-10-16: groovy 4.0.6 available on SDKMAN!
================================================================================

Downloading: groovy 4.0.6

In progress...

###################################################################################################################################################### 100.0%

Installing: groovy 4.0.6
Done installing!


Setting groovy 4.0.6 as default.

 

Step 6: Check Version

After successful installation, you can verify the installed version by using groovy -version command as shown below.

cyberithub@ubuntu:~$ groovy -version
Groovy Version: 4.0.6 JVM: 17 Vendor: Eclipse Adoptium OS: Linux

 

Step 7: Write Your First Program

Now that groovy is installed successfully, it is time to write your first groovy program. In below program, we are displaying Hi, This is from CyberITHub !! on the output using println() function.

cyberithub@ubuntu:~$ nano hello.groovy
class Hello {
    public static void main(String[] args) {
              println("Hi, This is from CyberITHub !!");
   }
}

Output

cyberithub@ubuntu:~$ groovy hello.groovy
Hi, This is from CyberITHub !!

 

Step 8: Check all available options

You can also check all the options available with groovy utility using groovy -h command as shown below.

cyberithub@ubuntu:~$ groovy -h
Usage: groovy [options] [filename] [args]
The Groovy command line processor.
    -cp, -classpath, --classpath=<path>
                         Specify where to find the class files - must be
                               first argument
-D, --define=<property=value>
                         Define a system property
    --disableopt=optlist[,optlist...]
                         Disables one or all optimization elements; optlist
                         can be a comma separated list with the elements:
                         all (disables all optimizations),
                         int (disable any int based optimizations)
-d, --debug              Debug mode will print out full stack traces
-c, --encoding=<charset> Specify the encoding of the files
-e=<script>              Specify a command line script
-i=[<extension>]         Modify files in place; create backup if extension
                         is given (e.g. '.bak')
-n                       Process files line by line using implicit 'line'
...............................

Leave a Comment