Scala Development Environment
Installation
We will use SdkMan! to install Scala, so if you haven't installed SdkMan yet, install it first. (SdkMan Installation)
Install Scala 2
sdk install scala 2.13.16
Downloading: scala 2.13.16
In progress...
######################################################################### 100.0%
Installing: scala 2.13.16
Done installing!
Setting scala 2.13.16 as default.
Install Scala 3
You may get an error like the following when you run scala 3 installed by sdkman.
/Users/USERNAME/.sdkman/candidates/scala/3.3.5/bin/common: line 33: /opt/local/bin/tput: Bad CPU type in executable
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
If so, use Couriser to install Scala 3.
sdk install scala 3.3.5
Downloading: scala 3.3.5
In progress...
######################################################################### 100.0%
Installing: scala 3.3.5
Done installing!
Do you want scala 3.3.5 to be set as default? (Y/n): n
Different Java Versions
Would you like to install different Java versions? Check out the Java page.
sbt
Installation
You can install sbt with the following command.
sdk install sbt
Downloading: sbt 1.10.5
In progress...
######################################################################### 100.0%
Installing: sbt 1.10.5
Done installing!
Or you can install a specific version of sbt with
sdk install sbt 1.9.9
Downloading: sbt 1.9.9
In progress...
######################################################################### 100.0%
Installing: sbt 1.9.9
Done installing!
Do you want sbt 1.9.9 to be set as default? (Y/n): y
Setting sbt 1.9.9 as default.
Upgrade
Upgrade sbt can be done with
sdk upgrade sbt
Available defaults:
sbt (local: 1.9.9, 1.10.5, 1.10.3, 1.10.1, 1.10.5; default: 1.10.11)
Use prescribed default version(s)? (Y/n): y
Downloading: sbt 1.10.11
In progress...
######################################################################### 100.0%
Installing: sbt 1.10.11
Done installing!
Setting sbt 1.10.11 as default.
List sbt
You can get the list of all available and installed sbt with
sdk list sbt
================================================================================
Available Sbt Versions
================================================================================
> * 1.10.11 1.8.2 1.4.6 1.2.6
1.10.10 1.8.1 1.4.5 1.2.5
1.10.9 1.8.0 1.4.4 1.2.4
1.10.7 1.7.3 1.4.3 1.2.3
1.10.6 1.7.2 1.4.2 1.2.1
* 1.10.5 1.7.1 1.4.1 1.2.0
1.10.4 1.7.0 1.4.0 1.1.6
1.10.3 1.6.2 1.3.13 1.1.5
1.10.2 1.6.1 1.3.12 1.1.4
1.10.1 1.6.0 1.3.11 1.1.2
1.10.0 1.5.8 1.3.10 1.1.1
* 1.9.9 1.5.7 1.3.9 1.1.0
1.9.8 1.5.6 1.3.8 1.0.4
1.9.7 1.5.5 1.3.7 1.0.3
1.9.6 1.5.4 1.3.6 1.0.2
1.9.5 1.5.3 1.3.5 1.0.1
1.9.4 1.5.2 1.3.4 1.0.0
1.9.3 1.5.1 1.3.3 0.13.18
1.9.2 1.5.0 1.3.1 0.13.17
1.9.1 1.4.9 1.3.0
1.9.0 1.4.8 1.2.8
1.8.3 1.4.7 1.2.7
================================================================================
+ - local version
* - installed
> - currently in use
================================================================================
Set Default
If you have multiple sbts installed, you can set the default sbt manually by
sdk default sbt 1.9.9
setting sbt 1.9.9 as the default version for all shells.
Uninstall sbt
To uninstall a specific version of sbt, you can use the following command.
sdk uninstall sbt SBT_VERSION_HERE
e.g.) To uninstall sbt 1.10.0, run
sdk uninstall sbt 1.10.0
removed sbt 1.10.0.
scala-cli
Installation
sdk install scalacli
Downloading: scalacli 1.8.0
In progress...
######################################################################### 100.0%
Installing: scalacli 1.8.0
Done installing!
Setting scalacli 1.8.0 as default.
Install a specific version
sdk install scalacli VERSION_HERE
e.g.) To install scalacli 1.8.1, run
sdk install scalacli 1.8.1
Downloading: scalacli 1.8.1
In progress...
######################################################################### 100.0%
Installing: scalacli 1.8.1
Done installing!
Do you want scalacli 1.8.1 to be set as default? (Y/n): y
Setting scalacli 1.8.1 as default.
Upgrade
sdk upgrade scalacli
Available defaults:
scalacli (local: 1.8.0; default: 1.8.2)
Use prescribed default version(s)? (Y/n): y
Downloading: scalacli 1.8.2
In progress...
######################################################################### 100.0%
Installing: scalacli 1.8.2
Done installing!
Setting scalacli 1.8.2 as default.
List available scala-cli versions
sdk list scalacli
Ammonite REPL
Rrerequisites
Let's create a bin folder in your home directory and add it to your $PATH so that Ammonite REPL can be found when you run amm.
That $HOME/bin folder can be used for any other commands you want to add to your $PATH as well.
mkdir -p $HOME/bin
{ echo $PATH | grep -q "$HOME/bin" ; } || echo 'export PATH="$HOME/bin:$PATH"' >> $HOME/.zshrc
Ammonite REPL for Scala 3.5:
mkdir -p $HOME/bin; sh -c '(echo "#!/usr/bin/env sh" && curl -L https://github.com/com-lihaoyi/Ammonite/releases/download/3.0.2/3.5-3.0.2) > $HOME/bin/amm && chmod +x $HOME/bin/amm' && amm
Ammonite REPL for Scala 2.13:
mkdir -p $HOME/bin; sh -c '(echo "#!/usr/bin/env sh" && curl -L https://github.com/com-lihaoyi/Ammonite/releases/download/3.0.2/2.13-3.0.2) > $HOME/bin/amm && chmod +x $HOME/bin/amm' && amm
Try ammonite-repl
amm
Loading...
Welcome to the Ammonite Repl 3.0.0-2-6342755f (Scala 2.13.15 Java 21.0.5)
@
sbt plugins (Optional)
Global sbt plugins
You can set global sbt plugins by running the following command. Please choose the ones you want to have.
echo '
addSbtPlugin("io.github.todokr" % "sbt-project-switcher" % "0.1.4")
addSbtPlugin("com.timushev.sbt" % "sbt-updates" % "0.6.4")
addSbtPlugin("com.github.cb372" % "sbt-explicit-dependencies" % "0.3.1")
addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.5.2")
addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % "0.13.0")
addSbtPlugin("net.vonbuchholtz" % "sbt-dependency-check" % "5.1.0")
addSbtPlugin("com.github.sbt" % "sbt-license-report" % "1.6.1")
addDependencyTreePlugin
' >> $HOME/.sbt/1.0/plugins/sbt-plugins.sbt
sbt-project-switcher
sbt-tpolecat
sbt-tpolecat is an SBT plugin for automagically configuring scalac options
Add the following line to project/plugins.sbt.
addSbtPlugin("org.typelevel" % "sbt-tpolecat" % "0.5.2")
sbt-wartremover
WartRemover is a linting tool that can help you write safe and correct code.
Add the following line to project/plugins.sbt.
addSbtPlugin("org.wartremover" % "sbt-wartremover" % "3.2.1")
In build.sbt,
wartremoverErrors ++= Warts.all
Or more practical settings might be
wartremoverErrors ++= Warts.allBut(Wart.Any, Wart.Nothing, Wart.ImplicitConversion, Wart.ImplicitParameter)
Also add the following setting to exclude the source code generated by build from wart checking.
wartremoverExcluded += sourceManaged.value