Setting Up Scala on Ubuntu
Scala is a powerful and versatile programming language that combines object-oriented and functional programming paradigms. It is designed to be concise and expressive,
enabling developers to write robust and high-performance code. In this tutorial, we will guide you through setting up Scala 3 on an
Ubuntu system using SDKMAN! to manage the JDK and Coursier (cs
) to install Scala. Follow these steps to get your environment ready for Scala development.
Prerequisites
Before you begin, make sure you have:
- An Ubuntu system (18.04 or later)
- sudo access to install packages
Step 1: Install SDKMAN!
SDKMAN! is a tool for managing parallel versions of multiple software development kits on most Unix-based systems.
-
Open a terminal window.
-
Install SDKMAN!:
curl -s "https://get.sdkman.io" | bash
-
Follow the instructions displayed on the terminal to finalize the installation. This typically involves running:
source "$HOME/.sdkman/bin/sdkman-init.sh"
-
Verify the installation:
sdk version
You should see the version of SDKMAN! installed.
Step 2: Install JDK 21 using SDKMAN!
-
Use SDKMAN! to install JDK 21:
sdk install java 21.0.2-open
Or you can check for candidate versions by running:
sdk list java
-
Verify the installation:
java -version
You should see output similar to:
java -version openjdk version "21.0.2" 2024-01-16 OpenJDK Runtime Environment (build 21.0.2+13-58) OpenJDK 64-Bit Server VM (build 21.0.2+13-58, mixed mode, sharing)
Step 3: Install Coursier and Scala (cs
)
Coursier is a tool that simplifies the installation of Scala and related tools.
curl -fL https://github.com/coursier/coursier/releases/latest/download/cs-x86_64-pc-linux.gz | gzip -d > cs \
&& chmod +x cs \
&& ./cs setup
This installs Scala 3 by default, though projects can still use different versions of Scala depending on build settings - see Getting Started.
Note that Coursier can also manage the JVM installation.
cs java --jvm 21 --version
should output something like:
openjdk 21.0.3 2024-04-16 LTS
OpenJDK Runtime Environment Temurin-21.0.3+9 (build 21.0.3+9-LTS)
OpenJDK 64-Bit Server VM Temurin-21.0.3+9 (build 21.0.3+9-LTS, mixed mode, sharing)
Step 4: Set Up Your Development Environment
-
Ensure that your PATH includes the directory where Coursier installs executables. Typically, this is
~/.local/share/coursier/bin
. You can add this to your.bashrc
or.zshrc
file:echo 'export PATH="$PATH:$HOME/.local/share/coursier/bin"' >> ~/.bashrc source ~/.bashrc
-
Test your setup using the scala cli: Launch the Scala CLI using:
cs launch scala3
You should output similar to:
Welcome to Scala 3.4.2 (17.0.1, Java OpenJDK 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.
scala>
Now you can try Scala commands e.g.:
scala> 1 + 1
with output
val res0: Int = 2
Conclusion
You have successfully set up Scala 3 on your Ubuntu system using SDKMAN! for managing JDK 21 and Coursier for installing Scala. You can now start developing Scala applications on your machine. Happy coding!