How to use third party libraries with Scala REPL?

Harshal Kshatriya picture Harshal Kshatriya · Sep 15, 2013 · Viewed 21.1k times · Source

I've downloaded Algebird and I want to try out few things in the Scala interpreter using this library. How do I achieve this?

Answer

Rüdiger Klaehn picture Rüdiger Klaehn · Sep 15, 2013

Of course, you can use scala -cp whatever and manually manage your dependencies. But that gets quite tedious, especially if you have multiple dependencies.

A more flexible approach is to use sbt to manage your dependencies. Search for the library you want to use on search.maven.org. Algebird for example is available by simply searching for algebird. Then create a build.sbt referring to that library, enter the directory and enter sbt console. It will download all your dependencies and start a scala console session with all dependencies automatically on the classpath.

Changing things like the scala version or the library version is just a simple change in the build.sbt. To play around you don't need any scala code in your directory. An empty directory with just the build.sbt will do just fine.

Here is a build.sbt for using algebird:

name := "Scala Playground"

version := "1.0"

scalaVersion := "2.10.2"

libraryDependencies += "com.twitter" % "algebird-core" % "0.2.0"

Edit: often when you want to play around with a library, the first thing you have to do is to import the namespace(s) of the library. This can also be automated in the build.sbt by adding the following line:

initialCommands in console += "import com.twitter.algebird._"