Cyberithub

How to Install Scala Programming Language on Ubuntu 20.04 LTS

Advertisements

In this article, I will take you through the steps to install scala programming language on Ubuntu 20.04 LTS. Scala is designed to address the needs of modern software development challenges. It is high level, statically typed, multi-paradigm language which is mixture of both object oriented and functional programming. It allows us to build high performance concurrent systems with easy access to rich set of libraries. You can install scala tool on almost any platform to run scala programs. Here we are going to install scala tool on a Ubuntu 20.04 LTS System.

Important Features

  • It is a statically typed language.
  • It has a sophisticated type inference system.
  • It works well with thousands of Java Libraries.
  • It is an object oriented programming language hence all OOPS feature supported.
  • It is also a functional programming language which means functions can be passed as a variable into other functions.
  • It allows us to write concise and readable code.
  • It has expressive type system.
  • It runs on JVM so Java and Scala stacks can be freely mixed for totally seamless integration.
  • It combines the flexibility of Java style interfaces with the power of classes.
  • It is well suited for high order functions.
  • It is much ideal for teaching programming to beginners.

How to Install Scala Programming Language on Ubuntu 20.04 LTS

How to Install Scala Programming Language on Ubuntu 20.04 LTS

Also Read: How to Install latest version of GO 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 System.

 

Step 2: Update Your Server

In the first step, you need to sync the installed packages with the latest available versions from the default Ubuntu repo by using sudo apt update command as shown below.

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
Hit:3 http://in.archive.ubuntu.com/ubuntu focal InRelease
Get:4 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB]
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 https://apt.boltops.com stable InRelease
Get:8 http://security.ubuntu.com/ubuntu focal-security/main amd64 DEP-11 Metadata [40.8 kB]
Get:9 http://in.archive.ubuntu.com/ubuntu focal-backports InRelease [108 kB]
Get:10 http://security.ubuntu.com/ubuntu focal-security/restricted amd64 Packages [1,241 kB]
Get:11 http://in.archive.ubuntu.com/ubuntu focal-updates/main amd64 DEP-11 Metadata [277 kB]
Get:12 http://in.archive.ubuntu.com/ubuntu focal-updates/universe amd64 DEP-11 Metadata [405 kB]
Get:13 http://in.archive.ubuntu.com/ubuntu focal-updates/multiverse amd64 DEP-11 Metadata [944 B]
............................................

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

cyberithub@ubuntu:~$ sudo apt upgrade
Reading package lists... Done
Building dependency tree
Reading state information... Done
Calculating upgrade... Done
The following packages were automatically installed and are no longer required:
golang-1.13-go golang-1.13-race-detector-runtime golang-1.13-src golang-race-detector-runtime golang-src linux-headers-5.15.0-46-generic
linux-hwe-5.15-headers-5.15.0-46 linux-image-5.15.0-46-generic linux-modules-5.15.0-46-generic linux-modules-extra-5.15.0-46-generic
Use 'sudo apt autoremove' to remove them.
Try Ubuntu Pro beta with a free personal subscription on up to 5 machines.
Learn more at https://ubuntu.com/pro
The following packages will be upgraded:
docker-ce docker-ce-cli docker-ce-rootless-extras libasn1-8-heimdal libgmp-dev libgmp10 libgmpxx4ldbl libgssapi3-heimdal libhcrypto4-heimdal
libheimbase1-heimdal libheimntlm0-heimdal libhx509-5-heimdal libkrb5-26-heimdal libroken18-heimdal libwind0-heimdal unzip
16 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
13 standard LTS security updates
Need to get 70.4 MB/71.9 MB of archives.
After this operation, 348 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y
........................................................

 

Step 3: Install Java

Since scala requires JVM, you need to install Java in your system in case if it is already not installed. You can either install default-jdk package or the latest available OpenJDK package. Here we are installing openjdk-17-jdk package using sudo apt install openjdk-17-jdk command as shown below.

cyberithub@ubuntu:~$ sudo apt install openjdk-17-jdk
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
golang-1.13-go golang-1.13-race-detector-runtime golang-1.13-src golang-race-detector-runtime golang-src libhawtjni-runtime-java libjansi-java
libjansi-native-java libjline2-java linux-headers-5.15.0-46-generic linux-hwe-5.15-headers-5.15.0-46 linux-image-5.15.0-46-generic
linux-modules-5.15.0-46-generic linux-modules-extra-5.15.0-46-generic scala-library scala-parser-combinators scala-xml
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
fonts-dejavu-extra libatk-wrapper-java libatk-wrapper-java-jni openjdk-17-jdk-headless openjdk-17-jre
Suggested packages:
openjdk-17-demo openjdk-17-source visualvm
The following NEW packages will be installed:
fonts-dejavu-extra libatk-wrapper-java libatk-wrapper-java-jni openjdk-17-jdk openjdk-17-jdk-headless openjdk-17-jre
0 upgraded, 6 newly installed, 0 to remove and 0 not upgraded.
Need to get 245 MB of archives.
After this operation, 260 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y
.............................................................

After successful installation, you can check java version by using java -version command as shown below.

cyberithub@ubuntu:~$ java -version
openjdk version "17" 2021-09-14
OpenJDK Runtime Environment Temurin-17+35 (build 17+35)
OpenJDK 64-Bit Server VM Temurin-17+35 (build 17+35, mixed mode, sharing)

 

Step 4: Install Scala

There are multiple ways to install scala on Ubuntu/Debian based systems. You can choose any of the below method depending on the version you need to install.

a) Using apt or apt-get

If don't need the latest scala version then you can install it from default Ubuntu repo by using sudo apt install scala command as shown below. This will install the version currently available on Ubuntu repo which might not be the latest one.

cyberithub@ubuntu:~$ sudo apt install scala
[sudo] password for cyberithub:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
golang-1.13-go golang-1.13-race-detector-runtime golang-1.13-src golang-race-detector-runtime golang-src linux-headers-5.15.0-46-generic
linux-hwe-5.15-headers-5.15.0-46 linux-image-5.15.0-46-generic linux-modules-5.15.0-46-generic linux-modules-extra-5.15.0-46-generic
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
libhawtjni-runtime-java libjansi-java libjansi-native-java libjline2-java scala-library scala-parser-combinators scala-xml
Suggested packages:
scala-doc
The following NEW packages will be installed:
libhawtjni-runtime-java libjansi-java libjansi-native-java libjline2-java scala scala-library scala-parser-combinators scala-xml
0 upgraded, 8 newly installed, 0 to remove and 0 not upgraded.
Need to get 25.0 MB of archives.
After this operation, 28.7 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
.....................................................................

b) Using cs setup

If you are looking to install the latest version of scala then it is recommended to install through cs setup. You just need to visit the official website and get the latest curl command to install scala applications as shown below.

cyberithub@ubuntu:~$ curl -fL https://github.com/coursier/launchers/raw/master/cs-x86_64-pc-linux.gz | gzip -d > cs && chmod +x cs && ./cs setup
 % Total  % Received %  Xferd Average Speed   Time     Time     Time    Current
                              Dload  Upload   Total    Spent    Left    Speed
 0     0  0     0    0      0     0        0 --:--:-- --:--:-- --:--:--   0
100 20.0M 100 20.0M  0      0 1518k        0 0:00:13  0:00:13  --:--:-- 2568k
Checking if a JVM is installed
Found a JVM installed under /usr/lib/jvm/java-17-openjdk-amd64.

Checking if ~/.local/share/coursier/bin is in PATH
Should we add ~/.local/share/coursier/bin to your PATH via ~/.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

To reflect the changes done in ~/.profile file, you can either logout and login to your system again or you can just run source ~/.profile command in the current terminal to update the changes.

cyberithub@ubuntu:~$ source ~/.profile

Now you can check installed version by using scala -version command as shown below.

cyberithub@ubuntu:~$ scala -version
Scala code runner version 3.2.0 -- Copyright 2002-2022, LAMP/EPFL

 

Step 5: Verify Installation

After successful installation, you can verify the installed files by using dpkg -L scala command as shown below.

cyberithub@ubuntu:~$ dpkg -L scala
/.
/usr
/usr/share
/usr/share/doc
/usr/share/doc/scala
/usr/share/doc/scala/copyright
/usr/share/java
/usr/share/java/scala-compiler-2.11.12.jar
/usr/share/java/scalap-2.11.12.jar
/usr/share/lintian
/usr/share/lintian/overrides
/usr/share/lintian/overrides/scala
/usr/share/maven-repo
/usr/share/maven-repo/org
/usr/share/maven-repo/org/scala-lang
/usr/share/maven-repo/org/scala-lang/scala-compiler
/usr/share/maven-repo/org/scala-lang/scala-compiler/2.11.12
/usr/share/maven-repo/org/scala-lang/scala-compiler/2.11.12/scala-compiler-2.11.12.pom
/usr/share/maven-repo/org/scala-lang/scala-compiler/2.11.x
/usr/share/maven-repo/org/scala-lang/scala-compiler/2.11.x/scala-compiler-2.11.x.pom
/usr/share/scala-2.11
.......................................................

 

Step 6: Write Your First Program

Now that scala is installed successfully, you can test the installation by running a simple print statement which displays Hello from CyberITHub !! on the output. You need to first start the scala prompt by running scala command and then write below given print statement and press Enter. You will see the statement on the output. Then to exit the prompt, you need to do :q and then press Enter.

cyberithub@ubuntu:~$ scala
Welcome to Scala 2.11.12 (OpenJDK 64-Bit Server VM, Java 17).
Type in expressions for evaluation. Or try :help.

scala> println("Hello from CyberITHub !!")
Hello from CyberITHub !!

scala> :q

 

Step 7: Uninstall Scala 

If you are looking to uninstall scala then you need to use sudo apt remove scala command as shown below. But before running below command please check if for any unwanted package removal as sometimes it might remove some of the packages which are critical for running other applications. Hence it is important to verify below command especially if you are running this on a production or critical systems.

cyberithub@ubuntu:~$ sudo apt remove scala
[sudo] password for cyberithub:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
golang-1.13-go golang-1.13-race-detector-runtime golang-1.13-src golang-race-detector-runtime golang-src libhawtjni-runtime-java libjansi-java
libjansi-native-java libjline2-java linux-headers-5.15.0-46-generic linux-hwe-5.15-headers-5.15.0-46 linux-image-5.15.0-46-generic
linux-modules-5.15.0-46-generic linux-modules-extra-5.15.0-46-generic scala-library scala-parser-combinators scala-xml
Use 'sudo apt autoremove' to remove them.
The following packages will be REMOVED:
scala
0 upgraded, 0 newly installed, 1 to remove and 0 not upgraded.
After this operation, 16.3 MB disk space will be freed.
Do you want to continue? [Y/n] Y
(Reading database ... 302410 files and directories currently installed.)
Removing scala (2.11.12-4) ...

Leave a Comment