Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to install mongo-scala-driver for scala version 3 using sbt?

Tags:

mongodb

scala

sbt

I am trying to install mongo-scala-driver but I am running into this error:

[error] (update) sbt.librarymanagement.ResolveException: Error downloading org.mongodb.scala:mongo-scala-driver_3:4.2.3

My build.sbt file looks like this:

name := "project"
version := "0.1"

scalaVersion := "3.0.2"

libraryDependencies += "org.scalactic" %% "scalactic" % "3.2.9"
libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.9" % "test"

libraryDependencies += "org.mongodb.scala" %% "mongo-scala-driver" % "4.2.3"

I am wondering if it is failing because I am using scala version 3.0.2 whereas the last Scala version that the Mongo Scala docs have listed is 2.12. The instructions I am following are here: https://mongodb.github.io/mongo-java-driver/4.2/driver-scala/getting-started/installation/.

I am new to Scala so this could very well be a different problem, but I am having trouble finding any solutions.

Any help would be much appreciated. Thank you!

like image 945
Harrison Sligting Avatar asked Sep 03 '25 15:09

Harrison Sligting


1 Answers

Scala artifacts lack binary compatibility (cross major versions) which means that a library x compiled with Scala 2.12.x can not be used with Scala 2.13.x. So, library owners compile their libraries for multiple Scala versions and publish multiple binary packages.

So, for any library abc developed by organization org.xyx, they will publish artifacts named abc_2.11, abc_2.12, abc_2.13.

So, if you are working with Scala 2.12 and want to use version 1.2.3 of this library, then you can include this library as follows,

libraryDependencies += "org.xyz" % "abc_2.12" % "1.2.3"

Note that we did not use %% to add the above dependency.

This %% is a special helper provided by sbt which automatically appends the major scala version to artifact name.

So, the above example can be re-written as

scalaVersion := "2.12.14"

libraryDependencies += "org.xyz" %% "abc" % "1.2.3"

sbt will automatically convert abc to abc_2.12.

So, for using any library, you need to first check whether it's available for your Scala version or not.

Lets look at cats-core. We can check maven central for available versions,

https://mvnrepository.com/artifact/org.typelevel/cats-core

enter image description here

We can see that cats-core version 2.6.1 is available for Scala 2.12, 2.13 and 3. We will be able to use it with Scala 3.

Now, look at mongo-scala-driver. We can check maven central for available versions,

https://mvnrepository.com/artifact/org.mongodb.scala/mongo-scala-driver

Maven Central - mongo-scala-driver

As you can see that the latest version 4.2.3 is only publised for Scala 2.11, 2.12 and 2.13. Not yet available for Scala 3. So, you can not use it with Scala 3.

Scala 3 has major differences from Scala 2.12 and Scala 2.13. mongo-scala-driver heavily uses macros which were dropped in Scala 3, so it will require a major rewrite to supports Scala 3 and thus it is unlikely to get Scala 3 copatibiltiy in near future.

If any of your dependeices is missing in Scala 3, then you should just stay with Scala 2.12 or Scala 2.13.

For this particular case, you can use mongo-java-driver with any version of Scala (as it does not depend on Scala at all).

libraryDependencies += "org.mongodb.scala" % "mongo-java-driver" % "4.2.3"
like image 189
sarveshseri Avatar answered Sep 05 '25 15:09

sarveshseri