Skip to content

Scala Installation and Environment Setup

This chapter will guide you through setting up the Scala development environment, including installation and configuration of JDK, Scala, build tools, and IDE.

System Requirements

Before starting, ensure your system meets the following requirements:

  • Operating System: Windows 10+, macOS 10.14+, or Linux
  • Memory: At least 4GB RAM (8GB+ recommended)
  • Disk Space: At least 2GB available space
  • Network: Stable internet connection (for downloading dependencies)

Step 1: Install Java JDK

Scala runs on the Java Virtual Machine (JVM), so you first need to install JDK.

Check Existing Java Installation

Open a terminal or command prompt and run:

bash
java -version
javac -version

If Java 8 or higher is displayed, you can skip JDK installation.

Install JDK

SDKMAN is an excellent tool for managing multiple JDK versions:

bash
# Install SDKMAN
curl -s "https://get.sdkman.io" | bash
source "$HOME/.sdkman/bin/sdkman-init.sh"

# Install JDK
sdk install java 17.0.2-open
sdk use java 17.0.2-open

Method 2: Direct Download Installation

  1. Visit OpenJDK official website or Oracle JDK official website
  2. Download JDK 17 or higher suitable for your operating system
  3. Follow the installation wizard to complete installation
  4. Configure the JAVA_HOME environment variable

Verify JDK Installation

bash
java -version
# Should display something like:
# openjdk version "17.0.2" 2022-01-18

Step 2: Install Scala

bash
# Install Scala
sdk install scala 3.2.2
sdk use scala 3.2.2

# Verify installation
scala -version

Method 2: Using Coursier

Coursier is the installation tool recommended by the Scala community:

Linux/macOS:

bash
curl -fL https://github.com/coursier/launchers/raw/master/cs-x86_64-pc-linux.gz | gzip -d > cs
chmod +x cs
./cs setup

Windows:

Download and run cs-x86_64-pc-win32.exe

Method 3: Manual Installation

  1. Visit Scala official website
  2. Download the latest Scala distribution
  3. Extract to an appropriate directory
  4. Add the bin directory to your PATH environment variable

Verify Scala Installation

bash
scala -version
# Should display something like:
# Scala code runner version 3.2.2

Step 3: Install sbt (Scala Build Tool)

sbt is the standard build tool for Scala.

Install Using SDKMAN

bash
sdk install sbt 1.8.2

Install Using Package Manager

macOS (Homebrew):

bash
brew install sbt

Ubuntu/Debian:

bash
echo "deb https://repo.scala-sbt.org/scalasbt/debian all main" | sudo tee /etc/apt/sources.list.d/sbt.list
curl -sL "https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x2EE0EA64E40A89B84B2DF73499E82A75642AC823" | sudo apt-key add
sudo apt-get update
sudo apt-get install sbt

Windows:

Download and install sbt-1.8.2.msi

Verify sbt Installation

bash
sbt --version
# Should display something like:
# sbt version in this project: 1.8.2

Step 4: Choose and Configure IDE

IntelliJ IDEA is the preferred IDE for Scala development.

Installation Steps:

  1. Download IntelliJ IDEA Community Edition (free)
  2. Install and start IDEA
  3. Install Scala plugin:
    • Open FileSettingsPlugins
    • Search for "Scala" and install
    • Restart IDEA

Configure Scala SDK:

  1. Open FileProject Structure
  2. Select Global Libraries
  3. Click +Scala SDK
  4. Select the installed Scala version

Visual Studio Code

VS Code is also a good choice, especially for lightweight development.

Installation Steps:

  1. Download and install VS Code
  2. Install Scala extensions:
    • Open Extensions panel (Ctrl+Shift+X)
    • Search and install "Scala (Metals)"
    • Install "Scala Syntax (official)"

Vim/Neovim

For Vim users, you can use the coc-metals plugin.

Step 5: Create Your First Scala Project

Create Project Using sbt

bash
# Create project directory
mkdir my-first-scala-project
cd my-first-scala-project

# Create basic project structure
mkdir -p src/main/scala
mkdir -p src/test/scala

# Create build.sbt file
cat > build.sbt << EOF
ThisBuild / version := "0.1.0-SNAPSHOT"
ThisBuild / scalaVersion := "3.2.2"

lazy val root = (project in file("."))
  .settings(
    name := "my-first-scala-project"
  )
EOF

Create Hello World Program

bash
# Create main program file
cat > src/main/scala/Main.scala << 'EOF'
@main def hello(): Unit =
  println("Hello, Scala!")
  println("Welcome to the world of functional programming!")
EOF

Run the Program

bash
# Compile and run
sbt run

# Or enter sbt shell
sbt
> run
> exit

You should see the output:

Hello, Scala!
Welcome to the world of functional programming!

Step 6: Use Scala REPL

Scala REPL (Read-Eval-Print Loop) is a great tool for learning and experimentation.

Start REPL

bash
scala

Try Some Code in REPL

scala
scala> val name = "World"
val name: String = World

scala> println(s"Hello, $name!")
Hello, World!

scala> val numbers = List(1, 2, 3, 4, 5)
val numbers: List[Int] = List(1, 2, 3, 4, 5)

scala> val doubled = numbers.map(_ * 2)
val doubled: List[Int] = List(2, 4, 6, 8, 10)

scala> :quit  // Exit REPL

Project Structure Explanation

The standard Scala project structure is as follows:

my-scala-project/
├── build.sbt                 # Build configuration file
├── project/                  # sbt project configuration
│   ├── build.properties      # sbt version
│   └── plugins.sbt           # sbt plugins
├── src/
│   ├── main/
│   │   ├── scala/            # Main source code
│   │   └── resources/        # Resource files
│   └── test/
│       ├── scala/            # Test code
│       └── resources/        # Test resources
├── target/                   # Compilation output (auto-generated)
└── lib/                      # Local dependency libraries (optional)

Common sbt Commands

bash
sbt compile      # Compile project
sbt run          # Run project
sbt test         # Run tests
sbt clean        # Clean compilation output
sbt console      # Start Scala REPL with project classpath
sbt reload       # Reload build configuration
sbt update       # Update dependencies

Configuration File Examples

Detailed build.sbt Configuration

scala
ThisBuild / version := "0.1.0-SNAPSHOT"
ThisBuild / scalaVersion := "3.2.2"
ThisBuild / organization := "com.example"

lazy val root = (project in file("."))
  .settings(
    name := "my-scala-project",

    // Dependencies
    libraryDependencies ++= Seq(
      "org.scalatest" %% "scalatest" % "3.2.15" % Test,
      "org.typelevel" %% "cats-core" % "2.9.0"
    ),

    // Compiler options
    scalacOptions ++= Seq(
      "-deprecation",
      "-feature",
      "-unchecked",
      "-Xfatal-warnings"
    ),

    // JVM options
    javaOptions ++= Seq(
      "-Xmx2G",
      "-XX:+UseG1GC"
    )
  )

project/build.properties

properties
sbt.version=1.8.2

Troubleshooting

Common Issues

1. Java Version Incompatible

Error: Unsupported major.minor version

Solution: Ensure Java 8 or higher is being used

2. Slow sbt Dependency Download

Solution: Configure domestic mirror sources

Create a ~/.sbt/repositories file:

[repositories]
local
maven-central

3. IDE Cannot Recognize Scala Code

Solution:

  • Ensure Scala plugin is installed
  • Check if project is properly imported
  • Re-import the project

Performance Optimization

Increase sbt Memory

Create a .sbtopts file in the project root directory:

-Xmx2G
-XX:+UseG1GC
-XX:+CMSClassUnloadingEnabled

Enable Parallel Compilation

Add to build.sbt:

scala
Global / concurrentRestrictions += Tags.limit(Tags.Compile, 4)

Verify Installation

Create a test script to verify all tools are correctly installed:

bash
#!/bin/bash
echo "=== Scala Environment Check ==="

echo "1. Java version:"
java -version

echo -e "\n2. Scala version:"
scala -version

echo -e "\n3. sbt version:"
sbt --version

echo -e "\n4. Creating test project..."
mkdir -p test-scala-env/src/main/scala
cd test-scala-env

cat > build.sbt << 'EOF'
scalaVersion := "3.2.2"
name := "test-env"
EOF

cat > src/main/scala/Test.scala << 'EOF'
@main def test(): Unit =
  println("✅ Scala environment is working correctly!")
EOF

echo "5. Compiling and running test..."
sbt run

cd ..
rm -rf test-scala-env

echo -e "\n🎉 Environment setup complete!"

Next Steps

Now you have successfully set up your Scala development environment! Next we will learn:

  • Scala Basic Syntax - Learn Scala's basic syntax rules
  • How to write and run Scala programs
  • Core concepts and features of Scala

Ready to start writing Scala code? Let's continue learning!

Content is for learning and research only.