Getting started

Hi and welcome to Crystal’s Reference Book!

First, let’s make sure to install the compiler so that we may try all the examples listed in this book.

Once installed, the Crystal compiler should be available as crystal command.

Let’s try it!

Crystal version

We may check the Crystal compiler version. If Crystal is installed correctly then we should see something like this:

  1. $ crystal --version
  2. --8<-- "crystal-version.txt"

Great!

Crystal help

Now, if we want to list all the options given by the compiler, we may run crystal program without any arguments:

  1. $ crystal
  2. Usage: crystal [command] [switches] [program file] [--] [arguments]
  3. Command:
  4. init generate a new project
  5. build build an executable
  6. docs generate documentation
  7. env print Crystal environment information
  8. eval eval code from args or standard input
  9. play starts Crystal playground server
  10. run (default) build and run program
  11. spec build and run specs (in spec directory)
  12. tool run a tool
  13. help, --help, -h show this help
  14. version, --version, -v show version
  15. Run a command followed by --help to see command-specific information, ex:
  16. crystal <command> --help

More details about using the compiler can be found on the manpage man crystal or in our compiler manual.

Hello Crystal

The following example is the classic Hello World. In Crystal it looks like this:

```crystal title=”hello_world.cr” puts “Hello World!”

  1. We may run our example like this:
  2. ```console
  3. $ crystal hello_world.cr
  4. Hello World!

note The main routine is simply the program itself. There’s no need to define a “main” function or something similar.

Next you might want to start with the Introduction Tour to get acquainted with the language.

Here we have two more examples to continue our first steps in Crystal: