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:
java -version
javac -versionIf Java 8 or higher is displayed, you can skip JDK installation.
Install JDK
Method 1: Using SDKMAN (Recommended)
SDKMAN is an excellent tool for managing multiple JDK versions:
# 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-openMethod 2: Direct Download Installation
- Visit OpenJDK official website or Oracle JDK official website
- Download JDK 17 or higher suitable for your operating system
- Follow the installation wizard to complete installation
- Configure the
JAVA_HOMEenvironment variable
Verify JDK Installation
java -version
# Should display something like:
# openjdk version "17.0.2" 2022-01-18Step 2: Install Scala
Method 1: Using SDKMAN (Recommended)
# Install Scala
sdk install scala 3.2.2
sdk use scala 3.2.2
# Verify installation
scala -versionMethod 2: Using Coursier
Coursier is the installation tool recommended by the Scala community:
Linux/macOS:
curl -fL https://github.com/coursier/launchers/raw/master/cs-x86_64-pc-linux.gz | gzip -d > cs
chmod +x cs
./cs setupWindows:
Download and run cs-x86_64-pc-win32.exe
Method 3: Manual Installation
- Visit Scala official website
- Download the latest Scala distribution
- Extract to an appropriate directory
- Add the
bindirectory to your PATH environment variable
Verify Scala Installation
scala -version
# Should display something like:
# Scala code runner version 3.2.2Step 3: Install sbt (Scala Build Tool)
sbt is the standard build tool for Scala.
Install Using SDKMAN
sdk install sbt 1.8.2Install Using Package Manager
macOS (Homebrew):
brew install sbtUbuntu/Debian:
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 sbtWindows:
Download and install sbt-1.8.2.msi
Verify sbt Installation
sbt --version
# Should display something like:
# sbt version in this project: 1.8.2Step 4: Choose and Configure IDE
IntelliJ IDEA (Recommended)
IntelliJ IDEA is the preferred IDE for Scala development.
Installation Steps:
- Download IntelliJ IDEA Community Edition (free)
- Install and start IDEA
- Install Scala plugin:
- Open
File→Settings→Plugins - Search for "Scala" and install
- Restart IDEA
- Open
Configure Scala SDK:
- Open
File→Project Structure - Select
Global Libraries - Click
+→Scala SDK - Select the installed Scala version
Visual Studio Code
VS Code is also a good choice, especially for lightweight development.
Installation Steps:
- Download and install VS Code
- Install Scala extensions:
- Open Extensions panel (
Ctrl+Shift+X) - Search and install "Scala (Metals)"
- Install "Scala Syntax (official)"
- Open Extensions panel (
Vim/Neovim
For Vim users, you can use the coc-metals plugin.
Step 5: Create Your First Scala Project
Create Project Using sbt
# 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"
)
EOFCreate Hello World Program
# 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!")
EOFRun the Program
# Compile and run
sbt run
# Or enter sbt shell
sbt
> run
> exitYou 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
scalaTry Some Code in REPL
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 REPLProject 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
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 dependenciesConfiguration File Examples
Detailed build.sbt Configuration
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
sbt.version=1.8.2Troubleshooting
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-central3. 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:+CMSClassUnloadingEnabledEnable Parallel Compilation
Add to build.sbt:
Global / concurrentRestrictions += Tags.limit(Tags.Compile, 4)Verify Installation
Create a test script to verify all tools are correctly installed:
#!/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!