Share on Social Media

Learn how to effortlessly install the Scala programming language on Linux 9 with our comprehensive guide. Unlock the power of functional programming and scalable application development with this step-by-step tutorial tailored for seamless integration on your Linux environment. #centlinux #linux #scala

What is Scala Programming Language?

Scala is a strong statically typed high-level general-purpose programming language that supports both object-oriented programming and functional programming. Designed to be concise, many of Scala’s design decisions are aimed to address criticisms of Java.

Scala source code can be compiled to Java bytecode and run on a Java virtual machine (JVM). Scala can also be compiled to JavaScript to run in a browser, or directly to a native executable. On the JVM Scala provides language interoperability with Java so that libraries written in either language may be referenced directly in Scala or Java code. Like Java, Scala is object-oriented, and uses a syntax termed curly-brace which is similar to the language C. Since Scala 3, there is also an option to use the off-side rule (indenting) to structure blocks, and its use is advised. Martin Odersky has said that this turned out to be the most productive change introduced in Scala 3.

Unlike Java, Scala has many features of functional programming languages (like Scheme, Standard ML, and Haskell), including currying, immutability, lazy evaluation, and pattern matching. It also has an advanced type system supporting algebraic data types, covariance and contravariance, higher-order types (but not higher-rank types), anonymous types, operator overloading, optional parameters, named parameters, raw strings, and an experimental exception-only version of algebraic effects that can be seen as a more powerful version of Java’s checked exceptions.

The name Scala is a portmanteau of scalable and language, signifying that it is designed to grow with the demands of its users. (Source: Wikipedia)

What is Scala used for?

Scala is a versatile programming language that is used for a wide range of purposes. Some common uses of Scala include:

  1. Web Development: Scala is often used for building web applications and APIs. Frameworks like Play Framework and Akka HTTP provide powerful tools for building scalable and high-performance web services.
  2. Big Data Processing: Scala is widely used in the field of big data processing. Apache Spark, one of the most popular big data processing frameworks, is written in Scala. Scala’s functional programming features and concise syntax make it well-suited for writing complex data processing pipelines.
  3. Concurrency and Parallelism: Scala’s actor-based concurrency model, inspired by Erlang, allows developers to build highly concurrent and distributed systems. The Akka toolkit, built on top of Scala, provides actors and other abstractions for building concurrent and fault-tolerant applications.
  4. Functional Programming: Scala is a functional programming language that supports higher-order functions, immutability, and pattern matching. It’s often used for writing expressive and concise code, especially in applications where functional programming paradigms are beneficial.
  5. Domain Specific Languages (DSLs): Scala’s flexible syntax and powerful type system make it well-suited for creating domain-specific languages (DSLs). DSLs allow developers to write code that closely mirrors the problem domain, leading to more maintainable and expressive codebases.
  6. Data Science and Machine Learning: Scala is gaining popularity in the field of data science and machine learning. Libraries like Breeze and Spark MLlib provide tools for numerical computing, data manipulation, and machine learning, making Scala a viable option for building data-driven applications.

Overall, Scala’s combination of object-oriented and functional programming features, along with its interoperability with Java, makes it a versatile language suitable for a wide range of applications across different domains.

Scala vs Java

Scala and Java are both popular programming languages with their own strengths and use cases. Here’s a comparison between the two:

  • Syntax and Features:
  • Scala: Scala is a hybrid language that combines object-oriented and functional programming paradigms. It has a concise syntax with powerful features such as type inference, pattern matching, higher-order functions, and immutability by default.
  • Java: Java is a strictly object-oriented programming language with a more verbose syntax compared to Scala. While Java has introduced some functional programming features in recent versions (e.g., lambda expressions, streams), it remains primarily imperative and object-oriented.
  • Interoperability:
  • Scala: Scala is interoperable with Java, meaning Scala code can easily call Java code and vice versa. This interoperability allows developers to leverage existing Java libraries and frameworks seamlessly in Scala projects.
  • Java: Java does not natively support interoperability with Scala code. However, Scala code can call Java code without any issues, making it possible to gradually introduce Scala into existing Java projects.
  • Conciseness and Expressiveness:
  • Scala: Scala’s concise syntax and powerful features enable developers to write expressive and concise code, often requiring fewer lines of code compared to Java. Scala’s support for functional programming paradigms also allows for more expressive and declarative code.
  • Java: Java’s syntax is more verbose compared to Scala, which can sometimes lead to boilerplate code. While recent versions of Java have introduced features to reduce verbosity (e.g., type inference, lambda expressions), Java code tends to be more verbose than equivalent Scala code.
  • Performance:
  • Scala: Scala performance is comparable to Java performance in most cases. Since Scala runs on the Java Virtual Machine (JVM), it benefits from the performance optimizations provided by the JVM.
  • Java: Java is known for its performance and efficiency. The Java Virtual Machine (JVM) optimizes Java bytecode for efficient execution, resulting in high-performance applications.
  • Community and Ecosystem:
  • Scala: Scala has a smaller but vibrant community compared to Java. It has a growing ecosystem of libraries and frameworks, particularly in areas such as big data processing, web development, and functional programming.
  • Java: Java has one of the largest and most active developer communities. It has a vast ecosystem of libraries, frameworks, and tools, making it well-suited for a wide range of applications and industries.

In summary, Scala and Java are both powerful programming languages with their own strengths and trade-offs. Scala offers concise syntax, powerful functional programming features, and interoperability with Java, making it suitable for expressive and scalable applications. Java, on the other hand, is known for its stability, performance, and extensive ecosystem, making it a popular choice for a wide range of applications. The choice between Scala and Java depends on factors such as project requirements, team expertise, and personal preferences.

Recommended Online Training: Scala Programming Language

1062700 c21b 2show?id=oLRJ54lcVEg&offerid=1486687.391974718089278685953174&bids=1486687

Environment Specification

We are using a minimal Rocky Linux 9 virtual machine with following specifications.

  • CPU – 3.4 Ghz (2 cores)
  • Memory – 2 GB
  • Storage – 20 GB
  • Operating System – Rocky Linux release 9.1 (Blue Onyx)
  • Hostname – scala-01.centlinux.com
  • IP Address – 192.168.88.128/24

Update your Rocky Linux Server

It is a best practice to update your Linux operating system, before installing a new software.

By using a ssh client, connect with your Rocky Linux server as root user.

Refresh your Yum cache by using dnf command.

# dnf makecache
Rocky Linux 9 - BaseOS                          1.3 kB/s | 4.1 kB     00:03
Rocky Linux 9 - AppStream                       1.2 kB/s | 4.5 kB     00:03
Rocky Linux 9 - Extras                          974  B/s | 2.9 kB     00:03
Metadata cache created.

Execute following command at Linux bash prompt to update your Linux operating system.

# dnf update -y

If the above command update software packages related to Linux Kernel.

Then, you should reboot your Linux operating system with new Linux Kernel before installing Scala 3 software.

# reboot

After reboot, check the versions of your Linux Kernel and Linux operating system.

# cat /etc/rocky-release
Rocky Linux release 9.1 (Blue Onyx)

# uname -r
5.14.0-162.12.1.el9_1.0.2.x86_64

Install Scala 3 Prerequisites

Scala 3 requires JVM (Java Virtual Machine). Therefore, you may install Oracle Java or OpenJDK, which is available in standard yum repositories, by using dnf command.

# dnf install -y wget gzip java-17-openjdk

We have also install wget and gzip software packages to download and unzip the Coursier software.

After installation, verify the version of Java.

# java --version
openjdk 17.0.6 2023-01-17 LTS
OpenJDK Runtime Environment (Red_Hat-17.0.6.0.10-3.el9_1) (build 17.0.6+10-LTS)
OpenJDK 64-Bit Server VM (Red_Hat-17.0.6.0.10-3.el9_1) (build 17.0.6+10-LTS, mixed mode, sharing)

Install Scala Programming Language

Execute wget command to download Coursier setup straight from the Linux commandline.

# wget https://github.com/coursier/launchers/raw/master/cs-x86_64-pc-linux.gz
--2023-02-14 21:46:28--  https://github.com/coursier/launchers/raw/master/cs-x86_64-pc-linux.gz
Resolving github.com (github.com)... 20.207.73.82
Connecting to github.com (github.com)|20.207.73.82|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://raw.githubusercontent.com/coursier/launchers/master/cs-x86_64-pc-linux.gz [following]
--2023-02-14 21:46:29--  https://raw.githubusercontent.com/coursier/launchers/master/cs-x86_64-pc-linux.gz
Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 185.199.111.133, 185.199.109.133, 185.199.110.133, ...
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|185.199.111.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 20759374 (20M) [application/octet-stream]
Saving to: ‘cs-x86_64-pc-linux.gz’

cs-x86_64-pc-linux. 100%[===================>]  19.80M  1.12MB/s    in 19s

2023-02-14 21:46:49 (1.04 MB/s) - ‘cs-x86_64-pc-linux.gz’ saved [20759374/20759374]

Now, use gunzip command to extract Coursier zip file.

# gunzip cs-x86_64-pc-linux.gz

Rename the extracted file to cs for better accessibility. Also grant the execute privilege to cs file.

# mv cs-x86_64-pc-linux cs
# chmod +x cs

Execute cs command to initiate Scala 3 installation on Rocky Linux server.

# ./cs setup
Checking if a JVM is installed
Found a JVM installed under /usr/lib/jvm/java-17-openjdk-17.0.6.0.10-3.el9_1.x86_64.

Checking if ~/.local/share/coursier/bin is in PATH
  Should we add ~/.local/share/coursier/bin to your PATH via ~/.profile, ~/.bash_profile? [Y/n] Y

Checking if the standard Scala applications are installed
  Installed ammonite
  Installed cs
  Installed coursier
  Installed scala
  Installed scalac
  Installed scala-cli
  Installed sbt
  Installed sbtn
  Installed scalafmt

Execute .bash_profile to setup Scala environment for your current Bash shell.

# source ~/.bash_profile

To verify Scala 3 installation, check the version of Scala software as follows.

# scala -version
Scala code runner version 3.2.2 -- Copyright 2002-2023, LAMP/EPFL

Scala Programming Language Example: HelloWorld

By using vim text editor, create a Scala source file.

# vi HelloWorld.scala

Add following code in this file.

object HelloWorld {
    def main(args: Array[String]) = {
        println("HelloWorld!")
    }
}

By using scalac command compile your Scala source file into Java byte code.

# scalac HelloWorld.scala

After successful compilation, you can execute your HelloWorld program.

# scala HelloWorld
HelloWorld!

Video: How to install Scala 3 on Rocky Linux 9

YouTube player

Final Thoughts

Empower your programming journey by mastering the installation of the Scala programming language on Linux 9. With our comprehensive guide, dive into the world of functional programming and unleash your creativity in building scalable and robust applications. Let this tutorial be your gateway to a new realm of software development possibilities.

Leave a Reply

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