How to install Scala in Ubuntu?


Scala is a high-level, general-purpose programming language. It supports both object-oriented programming and functional programming. Scala provides language interoperability with Java which means code written in Scala can be compiled to Java byte code and run on JVM.

Libraries written in either language can be directly referenced to Scala or Java code. It uses curly braces in syntax which is more like C programming language.

In this article, we will discuss how to install Scala in Ubuntu Linux.

Prerequisites

To follow this guide you should have the following.

  • Have access to a user account on the Ubuntu system with sudo privilege.
  • A recent version of Java installed on your system

Installing Java

Before proceeding with the installation of Scala language on your Ubuntu system check whether you have Java installed on your system or not.

You can check this by using the given command –

java --version

If java is already installed you can move to the next step to install Scala in Ubuntu else first you need to install the java on your system.

java version

Use the given command to install Java in your system.

sudo apt install default-jdk -y

The default-jdk package will install JDK and JRE on Ubuntu.

Installing Scala in Ubuntu

Scala is available in the Ubuntu repository you can install it directly with the apt command. After installing Java on your system use the given command to refresh the local package repository and upgrade pending packages –

sudo apt update && sudo apt upgrade -y

Next, install the Scala by using the following command –

sudo apt install scala -y

This will start the downloading and installation process.

Run hello world program in Scala

Once the installation is completed you can use any IDE or text editor to write the Scala code on your system.

Open a text editor and paste the given code –

// Program in Scala to print the Hello World!
object hello
{
        def main(args: Array[String])
             {
                  println("Hello World!")
             }
}

As you can see in the image below.hello.scala

Save this file with the .scala extension.

Compile the code with the following syntax.

scalac file-name.scala

For example –

scalac hello.scala

Now execute the code by using the given command.

scala class-name

For example –

scala hello

You can see the output of hello.scala program in the image below.

Conclusion

I hope you have successfully install Scala in your Ubuntu system. Now if you have a query then write us in the comments below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.