Scala Tutorial

Basics

Control Statements

OOP Concepts

Parameterized - Type

Exceptions

Scala Annotation

Methods

String

Scala Packages

Scala Trait

Collections

Scala Options

Miscellaneous Topics

How to install Scala in Linux?

To install Scala on a Linux system, you can use package managers or manually install from the official distribution. Below are the steps for both approaches:

1. Using a Package Manager:

For Debian/Ubuntu:

  1. First, ensure that Java is installed on your system, since Scala runs on the Java Virtual Machine (JVM):

    sudo apt update
    sudo apt install default-jdk
    
  2. Install Scala:

    sudo apt install scala
    

For Fedora:

  1. Install Java:

    sudo dnf install java-latest-openjdk
    
  2. Install Scala:

    sudo dnf install scala
    

For other Linux distributions:

The process might vary depending on the distribution. If Scala is available in the official repositories, you can use the system's package manager to install it.

2. Manual Installation:

  1. Ensure Java is installed. You can download and install it from the official Oracle website or use OpenJDK based on your preference.

  2. Download the latest Scala distribution from the official Scala website.

  3. Extract the downloaded archive:

    tar -xvzf scala-<version>.tgz
    
  4. Move the extracted directory to a location like /opt:

    sudo mv scala-<version> /opt/scala
    
  5. Add the Scala binaries to your PATH. You can do this by adding the following lines to your ~/.bashrc or ~/.zshrc (depending on your shell):

    export SCALA_HOME=/opt/scala
    export PATH=$PATH:$SCALA_HOME/bin
    
  6. Reload your shell or execute:

    source ~/.bashrc
    

    or

    source ~/.zshrc
    

3. Using SDKMAN! (Recommended for developers):

SDKMAN! is a tool for managing multiple versions of software development kits. It makes installing Scala (and other SDKs like Java, Groovy, etc.) very easy.

  1. Install SDKMAN!:

    curl -s "https://get.sdkman.io" | bash
    

    Then, open a new terminal or:

    source "$HOME/.sdkman/bin/sdkman-init.sh"
    
  2. Install Scala:

    sdk install scala
    

This will install the latest stable version of Scala. If you need a specific version, you can specify it in the install command.

After installation, you can verify that Scala was installed correctly by running:

scala -version

This should display the version of Scala that you installed.

  1. Scala installation steps for Linux:

    • Description: Installing Scala on Linux involves downloading the distribution and setting up the necessary environment variables.
    • Steps:
      # Download Scala distribution
      wget https://downloads.lightbend.com/scala/2.13.8/scala-2.13.8.tgz
      
      # Extract the archive
      tar -xzvf scala-2.13.8.tgz
      
      # Move Scala to a desired location
      sudo mv scala-2.13.8 /usr/local/
      
      # Add Scala bin directory to PATH
      export PATH=$PATH:/usr/local/scala-2.13.8/bin
      
      # Verify installation
      scala -version
      
  2. Installing Scala with a package manager on Linux:

    • Description: Some Linux distributions offer Scala installation via package managers.
    • Steps:
      • For Debian/Ubuntu:
        sudo apt-get update
        sudo apt-get install scala
        
  3. Configuring PATH for Scala on Linux:

    • Description: Add Scala's bin directory to the system's PATH to run Scala commands from any location.
    • Steps:
      export PATH=$PATH:/usr/local/scala-2.13.8/bin
      
  4. Checking Scala installation on Linux:

    • Description: Verify that Scala is correctly installed by checking the version.
    • Steps:
      scala -version
      
  5. Managing Scala versions on Linux:

    • Description: Use tools like sdkman or coursier to manage multiple Scala versions on your system.
    • Steps:
      • Install sdkman:
        curl -s "https://get.sdkman.io" | bash
        source "$HOME/.sdkman/bin/sdkman-init.sh"
        sdk version
        
      • Install Scala with sdkman:
        sdk install scala 2.13.8