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._"
This is an answer using Ammonite (as opposed to the Scala REPL) - but it is such a great tool that it is worth mentioning.
- You can install it with a one liner such as:
sudo sh -c '(echo "#!/usr/bin/env sh" && curl -L https://github.com/lihaoyi/Ammonite/releases/download/2.1.2/2.13-2.1.2) > /usr/local/bin/amm && chmod +x /usr/local/bin/amm' && amm
or using brew on macOS:
brew install ammonite-repl
For scala 2.10, you need to use an oder version 1.0.3:
sudo sh -c '(echo "#!/usr/bin/env sh" && curl -L https://github.com/lihaoyi/Ammonite/releases/download/1.0.3/2.10-1.0.3) > /usr/local/bin/amm && chmod +x /usr/local/bin/amm' && amm
- Run Ammonite in your terminal:
amm // Displays Loading... Welcome to the Ammonite Repl 2.1.0 (Scala 2.12.11 Java 1.8.0_242)
- Use in ivy import to import your 3rd part library:
Then you can use your library within the Ammonite-REPL:
import com.twitter.algebird._ import com.twitter.algebird.Operators._ Map(1 -> Max(2)) + Map(1 -> Max(3)) + Map(2 -> Max(4)) ...
You can use the scala's
-cp switch to keep jars on the classpath. There are other switches available too, for example,
-unchecked for turning on various warnings. Many more to be found with
scala -X... and
scala -Y.... You can find out more information about these switches with
sbt console will not import libraries declared with a test scope. To use those libraries in the REPL, start the console with
You should be aware, however, that starting the console this way skips compiling your test sources.
- How to use third party libraries with Scala REPL?
- How to use java libraries with scala using sbt?
- How to use IntelliJ with Play Framework and Scala
- How to use scala trait with `self` reference?
- Scala How to use extends with an anonymous class
- How to use Typesafe's Config in Scala with encrypted passwords
- How can I use JMH for Scala benchmarks together with sbt?
- How to use Scala XML with Apache Flink?
- How to use takeWhile with an Iterator in Scala
- How to use Scala ARM with Futures?
- Scala spark: how to use dataset for a case class with the schema has snake_case?
- How to use my classes from Scala worksheet in IntelliJ CE with Scala plugin?
- How to use countDistinct in Scala with Spark?
- How do you use external Scala compiler with IDEA 12?
- How to use Quasar with Scala under sbt?
- How does one use Google Guice's @Inject with Scala / (Play 2.4.x)
- How to use scala 2.10 trunk with sbt 0.11.0? (Unresolved dependencies)
- How to use play-plugins-mailer with Play 2.3 and Scala 2.11?
- How do I use Scala to parse CSV data with empty columns?
- SBT: how to use scala 2.11 libraries in scala 2.12 project
- How do I use nightly builds of Scala 2.9 with maven?
- Scala How to use pattern matching with a non generic LazyList?
- How do I create horizontal or vertical struts and glue for use with scala BoxPanel?
- Value and column operations in scala spark, how to use a value left of an operator with spark column?
- How do I use Swing with Scala 2.11 in Eclipse?
- How to use path-dependent types with type classes in Scala
- How to use Scala Cats' Kleisli with Either
- Scala - How to use a Timer without blocking on Futures with Await.result
- How to use instanceof with scala object?
- How can I use scala generators to generate date with range?
More Query from same tag
- Akka TypedActor - how to correctly handle async responses with context
- Manually reorder columns to specific location Spark 3 / Scala
- Why can't i use UpdateStateByKey in my program?
- Gradle and IntelliJ with mixed Java/Scala project and SDK 1.6
- Jackson ObjectNode: how to not include NULL on serialization
- How to add a file name to a column in a data frame as multiple files are merged together?
- How to search a MySQL database with encrypted fields
- Implicit Encoder for TypedDataset and Type Bounds in Scala
- How Apache Spark caching works with regard to uncached file sources with non linear DAGs?
- Error importing scalding in sbt project
- Scala: Polymorphism in argument type of higher order function
- how to convert a println output to a dataframe in Scala
- How to testing classes with dependency injection
- sbt - Scala - Brew - MacOS - Unable to connect to sbt shell at all: NoClassDefFoundError
- Play Framework, Specs2 - Calling controller method directly from unit test
- Scala cats: problem with implicits when trying to use `===` syntax for `Eq` trait
- Scalding for Scala 2.11
- Async Controller with PlayFramework 2.6
- Using Await.result crashes Akka HTTP server
- handling json requests and responses in scala play framework and send to angular js java script
- Getting an HTTP response as Array[Byte] with Scala and Dispatch
- provide a streaming interface for a http service for downloading very large files
- In Scala, why does getInstance fail to work with GregorianCalendar?
- How to union 2 dataframe without creating additional rows?
- How to check for existence of artifact before publishing in SBT
- How do you grab an element in a JSON tree without an explicit name in Play 2.0?
- Good naming convention for scala "pimpers"
- How to remove quotes from front and end of the string Scala
- NullPointerException while reading a column from the row
- How does play framework unit testing controller methods