Install Scala in Ubuntu Terminal Command Line

This tutorial intends to teach you to install the Scala programming language in Ubuntu from the terminal command line. Scala is a general-purpose, high-level, and scalable programming language. It also runs on JVM. This allows users to use the features of both Scala and Java. Scala has two versions which are Scala 2 and Scala 3. The current Scala 3 LTS is 3.3.1, and the Scala 2 LTS is 2.13.12.

What is the difference between Scala 2 and 3?

These are the same languages. But the main difference is for the compilers. The binary name for Scala 2’s (and below) compiler is scalac, but for Scala 3, the compiler name is dotc. Scala 3 has dropped some unsound and useless features to make the language smaller and more regular.

Now follow the steps below to start your Scala installation on the Ubuntu server from the APT package manager and use the CS setup to get the latest version.

Steps To Install Scala in Ubuntu Terminal Command Line

You can use this instruction guide for both Ubuntu 20.04 and Ubuntu 22.04. Here to show the installation steps we use Ubuntu 22.04. Before you start your installation, you must have access to your server as a non-root user with sudo privileges. To get the Ubuntu initial server setup guides, you can check the following guides:

Initial Server Setup with Ubuntu 22.04

Initial Server Setup with Ubuntu 20.04

Step 1 – Run Ubuntu System Update and Upgrade From Terminal

The first step is to run a system update and upgrade on Ubuntu from the terminal command line. To do this, you can run the commands below:

# sudo apt update
# sudo apt upgrade -y

Step 2 – Install Java for Scala in Ubuntu

At this point, you must have Java installed on your server because Scala is based on JVM. The default Java version on Ubuntu 22.04 is OpenJDK 11. To get the latest version, you can run the command below:

sudo apt install openjdk-17-jdk -y

Then, verify your installation by checking its version:

java --version
Output
openjdk 17.0.8.1 2023-08-24
OpenJDK Runtime Environment (build 17.0.8.1+1-Ubuntu-0ubuntu122.04)
OpenJDK 64-Bit Server VM (build 17.0.8.1+1-Ubuntu-0ubuntu122.04, mixed mode, sharing)

Step 3 – Scala Installation in Ubuntu from Terminal Command Line

At this point, you can start your Scala installation. You can use both the APT package manager and the CS setup to get the latest version. Follow the steps below to see how you can do this.

Install Scala with APT on Ubuntu

Here you can use the default Ubuntu package manager which is APT to install your Scala programming language. To do this, you can simply run the command below:

sudo apt install scala -y

Then, verify your Scala installation by checking its version:

Output
Scala code runner version 2.11.12 -- Copyright 2002-2017, LAMP/EPFL

As you can see, Scala 2 has been installed with default APT. To get the latest version, you can proceed to the next step.

Install Scala with CS Setup Installer on Ubuntu

At this point, it is recommended to get the latest version of Scala by using the CS setup installer. To do this, you can visit the Scala official website and run the following curl command:

sudo curl -fL https://github.com/coursier/coursier/releases/latest/download/cs-x86_64-pc-linux.gz | sudo gzip -d > cs 

Then, make your file executable with the command below:

sudo chmod +x cs

Next, run the CS setup installer to install the latest Scala on Ubuntu:

sudo ./cs setup

During the installation, you will be asked to add the path to the profile. Enter Y to continue. You will get the following output:

Output
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

Now you should reload the Profile file to apply the changes. To do this, run the command below:

sudo source ~/.profile

Verify your Scala installation on Ubuntu with the command below:

scala -version
Output
Scala code runner version 3.3.1 -- Copyright 2002-2023, LAMP/EPFL

As you can see, you have the latest Scala 3 on your server.

Step 4 – Access Scala Shell in Ubuntu Terminal Command Line

At this point, you can access and run Scala from the Ubuntu terminal. To do this, you can simply run the command below:

scala

In the output, you should see:

Output
Welcome to Scala 3.3.1 (17.0.8.1, Java OpenJDK 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.

scala>

To see if your Scala is working correctly, you can run a sample project. For example:

scala> println("Hello This is Orcacore Team!")
Hello This is Orcacore Team!

To exit from the Scala shell, you can type:

scala> q:

Step 5 – Uninstall or Remove Scala From Ubuntu Terminal

If you no longer want to use Scala, you can simply remove it from your Ubuntu server. To uninstall Scala with APT, you can run the command below:

sudo apt remove scala 

To uninstall Scala with CS setup, you can use the following command:

cs uninstall scala
Output
Uninstalled scala

Conclusion

At this point, you have learned to Install Scala in Ubuntu from the terminal command line with the APT package manager and get the latest version which is Scala 3 by using the CS setup installer. Also, you have learned to Access your Scala shell and test your program language with a sample project.

Hope you enjoy using it. You may also interested in these articles:

Install Plex Media Server on Ubuntu from Terminal

8 Subject Line Rules That Will Get Email Opened

Use I/O Redirection in Linux Using Command Examples

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Stay informed and not overwhelmed, subscribe now!