Raspberry Pi

How to Install and Use Scala on Raspberry Pi

Scalable language, commonly referred to as Scala, is a professional programing language that supports both objected oriented and functional programming. It’s a high-level, general-purpose programming language focusing on developing IoT-related projects. This programming language has excellent support for Java, allowing the user to use all libraries of Java.

This article shows different methods to install Scala on your Raspberry Pi system.

How to Install Scala on Raspberry Pi

There are two easiest ways to install Scala on your Raspberry Pi system, which are as follows:

Method 1: Install Scala Through Raspberry Pi Repository

Scala programming language can easily be installed from official Raspberry Pi repository using the following command:

$ sudo apt install scala -y

After the installation, use the following command to check the Scala version.

$ scala -version

The above method doesn’t install an updated version of Scala, but you can still use this version since it’s the easiest one to follow.

Method 2: Install Scala from deb file on Raspberry Pi

If you want to install an updated version of Scala on the Raspberry Pi system, you can follow the below-mentioned steps:

Step 1: First, download the Scala deb file from the following command:

$ wget https://downloads.lightbend.com/scala/2.12.10/scala-2.12.10.deb

Step 2: Now install the Scala deb file on your Raspberry Pi system through the following command:

$ sudo apt install ./scala-2.12.10.deb

Step 3: To check the Scala version, apply the below-mentioned command:

$ scala -version

The above command ensures that this method has successfully installed an updated version of Scala on the Raspberry Pi system.

How to Run Code Written in Scala on Raspberry Pi

To run code written in Scala, first create a file with the name of your choice. In my case, I am creating a “welcome” file through the following syntax:

$ sudo nano welcome.scala

In place of “.scala”, you can also use the extension “.sc” to save the file.

Within this file, add the following code:

// Scala program to print Welcome Message
object linuxhint
{
    // Main Method  
    def main(args: Array[String])  
    {
        // prints Hello World
        println("Welcome to Linux Hint!")  
    }
}

Save this file using “CTRL+X”.

To build the code, run the following command:

$ scalac welcome.scala

Make sure to replace the file name “welcome” in the above code with the name of the file you created.

Now, to get the output of the Scala file, execute the file through class name using the “scala” command:

$ scala <classname>

In the above the class is “linuxhint”, so to execute the file, run:

$ scala linuxhint

Remove Scala from Raspberry Pi

Removing Scala on Raspberry Pi is pretty easy. It doesn’t matter which method you choose for Scala installation; you can run the following command to remove this programming language from your system.

$ sudo apt remove scala -y

Conclusion

Scala is a robust and simple-syntax programming language that can easily be installed on your system through the official Raspberry Pi repository or a deb file. The installation through the official repository is easy, but it doesn’t install an updated version of Scala on your Raspberry Pi system. You can choose the deb method to install the updated Scala version on your system successfully. After the installation, you can run the code using the procedure provided in the above guidelines.

About the author

Awais Khan

I'm an Engineer and an academic researcher by profession. My interest for Raspberry Pi, embedded systems and blogging has brought me here to share my knowledge with others.