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:
If 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:
Method 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
Step 2: Install Scala
Method 1: Using SDKMAN (Recommended)
Method 2: Using Coursier
Coursier is the installation tool recommended by the Scala community:
Linux/macOS:
Windows:
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
Step 3: Install sbt (Scala Build Tool)
sbt is the standard build tool for Scala.
Install Using SDKMAN
Install Using Package Manager
macOS (Homebrew):
Ubuntu/Debian:
Windows:
Download and install sbt-1.8.2.msi
Verify sbt Installation
Step 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 Hello World Program
Run the Program
You should see the output:
Step 6: Use Scala REPL
Scala REPL (Read-Eval-Print Loop) is a great tool for learning and experimentation.
Start REPL
Try Some Code in REPL
Project Structure Explanation
The standard Scala project structure is as follows:
Common sbt Commands
Configuration File Examples
Detailed build.sbt Configuration
project/build.properties
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:
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:
Enable Parallel Compilation
Add to build.sbt:
Verify Installation
Create a test script to verify all tools are correctly installed:
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!