Friday, June 7, 2019

Start Scala programming with VScode in Ubuntu

Scala, VScode, Ubuntu

I will explain how to start Scala with VScode on Ubuntu in this post.
Versions are:
  • Scala: 2.11.12
  • VScode: 1.34
  • Ubuntu: 18.04.2 LTS

Install sbt

At first check the latest version of sbt from here:
http://dl.bintray.com/sbt/debian/
It was 1.2.8 for me as of now, so install sbt 1.2.8 from there:
$ curl -L -o sbt.deb http://dl.bintray.com/sbt/debian/sbt-1.2.8.deb
$ sudo dpkg -i sbt.deb
$ sudo apt-get update
$ sudo apt-get install sbt
Now you can run sbt:
$ sbt
[info] Loading project definition from /home/user/project
[info] Set current project to shu (in build file:/home/user/)
[info] sbt server started at local:///home/user/.sbt/1.0/server/9a48bc25b5f71ce94d5c/sock
sbt:user> 
To check the version:
$ sbt "show sbtVersion"
[info] Loading project definition from /home/user/project
[info] Set current project to shu (in build file:/home/shu/)
[info] 1.2.8

Install Scala support on VScode

Install "Scala Language Server" on your vscode:


Create a new project

And run these commands on the vscode terminal:
$ cd {path of directory in which you want to save the project}
$ sbt new sbt/scala-seed.g8
You will be asked what name you will give to the project. I named it "scala-test". And a new scala project is generated. Grab the project and drag and drop on the vscode.

If it starts compiling, wait until the compiling finishes.

Hello World

Then run this command on the vscode terminal to see if you can run the project:
$ cd ./scala-test //(or your project name that you just gave)
$ sbt run

If you see this, it means you succeeded to compile and run the project. You are ready to code in Scala.
$ sbt run
[info] Loading project definition from /home/shu/user/scala-test/project
[info] Updating ProjectRef(uri("file:/home/shu/user/scala-test/project/"), "scala-test-build")...
[info] Done updating.
[info] Compiling 1 Scala source to /home/shu/user/scala-test/project/target/scala-2.12/sbt-1.0/classes ...
[info] Done compiling.
[info] Loading settings for project root from build.sbt ...
[info] Set current project to scala test (in build file:/home/shu/user/scala-test/)
[info] Updating ...
[info] Done updating.
[info] Compiling 1 Scala source to /home/shu/user/scala-test/target/scala-2.12/classes ...
[info] Done compiling.
[info] Packaging /home/shu/user/scala-test/target/scala-2.12/scala-test_2.12-0.1.0-SNAPSHOT.jar ...
[info] Done packaging.
[info] Running example.Hello 
hello
[success] Total time: 2 s, completed Jun 7, 2019 11:46:32 PM

References