Skip to main content
Version: 2.x

Getting Started with ZIO

This page takes you from an empty directory to a running ZIO program. The pages that follow in this section explain each idea the program uses.

Installationโ€‹

ZIO runs on Java 11 or later and supports Scala 2.12, 2.13 and 3.x. To use it, add the zio artifact to your build.sbt file:

// ZIO core: effects, fibers, layers, and concurrency primitives
libraryDependencies += "dev.zio" %% "zio" % "2.1.27"

ZIO publishes more artifacts for streaming, testing and other needs. Modules lists them all.

Create a Projectโ€‹

An sbt project needs a build.sbt file at its root and the Scala sources under src/main/scala. A minimal build.sbt for a ZIO application looks like this:

scalaVersion := "2.13.18"

// ZIO core: effects, fibers, layers, and concurrency primitives
libraryDependencies += "dev.zio" %% "zio" % "2.1.27"

Next, we write the program in src/main/scala/MyApp.scala. It asks for the user's name and greets them:

import zio._
import zio.Console._

object MyApp extends ZIOAppDefault {

def run = myAppLogic

val myAppLogic =
for {
_ <- printLine("Hello! What is your name?")
name <- readLine
_ <- printLine(s"Hello, ${name}, welcome to ZIO!")
} yield ()
}

Finally, we run it from the project directory:

sbt run

sbt compiles the project, finds MyApp and runs it. If you prefer to work in an IDE, the tutorials for IntelliJ IDEA and VS Code walk through the same steps there, and the Hello World quickstart is a ready-made project you can clone.

How the Program Runsโ€‹

ZIOAppDefault gives MyApp a main method, so sbt, an IDE or the java command can start it. The only thing we write is run: the effect that describes the whole program. myAppLogic does nothing when it is defined; it is a description that runs only when main hands it to the ZIO runtime.

The effect returned by run may fail with any error, and it may succeed with any value. When the program ends, the outcome decides the exit code of the process:

Outcome of runWhat happensExit code
Succeeds with any valueThe program ends0
Fails with an error or a defectThe runtime logs the full cause of the failure1
Calls ZIOApp#exit(code)The process exits at once, skipping finalizerscode

ZIOApp#exit has no effect inside an sbt run that is not forked. On the JVM, when the user presses Ctrl+C, the runtime interrupts run, waits for its finalizers to finish and then exits. Running Effects covers ZIOAppDefault in more detail, and shows how to run effects from code that has no ZIO entry point, such as an existing application or framework, with a Runtime.

Consoleโ€‹

ZIO provides a Console service for interacting with the console.

If you need to print text to the console, you can use Console.print and Console.printLine:

import zio._

// Print without trailing line break
Console.print("Hello World")

// Print string and include trailing line break
Console.printLine("Hello World")

If you need to read input from the console, you can use Console.readLine:

import zio._

val echo = Console.readLine.flatMap(line => Console.printLine(line))

Next Stepsโ€‹

Now that you've got ZIO installed and running, the next step is to learn about the ZIO data type.

ZIO Chat

Hi, fellow coder! ๐ŸŒŸ Welcome to your ZIO adventure with the ZIO Chat Bot. ๐Ÿš€ Let's master functional Scala together. From referential transparency to concurrent prowess, I've got your back. Ready to dive in? Your questions, my insights โ€“ let's conquer ZIO! ๐Ÿ’ก๐Ÿค–