LinuxCommandLibrary

crystal

Compile and run Crystal programming language code

TLDR

Run a Crystal file

$ crystal [path/to/file.cr]
copy

Compile a file and all dependencies to a single executable
$ crystal build [path/to/file.cr]
copy

Read Crystal source code from the command-line or stdin, and execute it
$ crystal eval '[code]'
copy

Generate API documentation from inline docstrings in Crystal files
$ crystal docs
copy

Compile and run a Crystal specification suite
$ crystal spec
copy

Start a local interactive server for testing the language
$ crystal play
copy

Create a project directory for a Crystal application
$ crystal init app [application_name]
copy

Display help
$ crystal help
copy

SYNOPSIS

crystal [global options] <command> [command options] [arguments]

Common commands:
crystal build <source_file> [options]
crystal run <source_file> [options]
crystal init <project_type> <project_name>
crystal deps
crystal spec [options]
crystal tool <tool_command>
crystal eval <code_snippet>
crystal help [command]
crystal version

PARAMETERS

build <source_file>
    Compiles the specified Crystal source file(s) into an executable binary. This command is used to create standalone applications.

run <source_file>
    Compiles and immediately executes the specified Crystal source file(s). This is convenient for rapid development and scripting.

init <project_type> <project_name>
    Initializes a new Crystal project with a specified type (e.g., app for an application, lib for a library) and structure.

deps
    Installs project dependencies defined in the shard.yml file, leveraging the Shards package manager to resolve and fetch libraries.

spec
    Runs the project's test suite (known as 'specs') using Crystal's built-in testing framework.

tool <tool_command>
    Provides access to various development tools, such as format for automatic code formatting, env for environment information, or docs for generating API documentation.

eval <code_snippet>
    Evaluates a given Crystal code snippet directly from the command line, similar to a REPL but for single expressions.

help [command]
    Displays general help information for the crystal command or detailed help for a specific subcommand.

version
    Displays the installed Crystal compiler version and related build information.

--release
    A common option (e.g., for build or run) that compiles with aggressive optimizations, producing faster and often smaller executables suitable for production.

--debug
    A common option (e.g., for build or run) that compiles with debug information included, which is invaluable for debugging with tools like GDB.

-o <path>, --output <path>
    Specifies the output path and filename for the compiled executable when using the build command.

-v, --verbose
    Enables verbose output, displaying more detailed information about the compilation and execution process.

DESCRIPTION

The crystal command invokes the Crystal language compiler and its associated toolchain. Crystal is a general-purpose, object-oriented programming language that aims to combine the developer productivity and elegant syntax of scripting languages like Ruby with the raw performance and type safety of compiled languages like C or Go. It features a powerful type inference system that typically eliminates the need for explicit type annotations, while still being statically typed. Crystal compiles directly to efficient native code, ensuring high execution speed. Key features include built-in concurrency primitives (fibers and channels), powerful compile-time macros for advanced metaprogramming, and automatic garbage collection. It is commonly used for web applications, command-line tools, and various other general-purpose software development tasks where a balance of speed, safety, and developer experience is desired.

CAVEATS

While the Crystal ecosystem is growing rapidly, it is still smaller and less mature than those of older, more established languages like Ruby or Python. Compilation times for large projects can be noticeable, especially in debug mode. Although stable, the language and its tooling are subject to continuous development and improvements.

SHARDS

Shards is Crystal's official dependency manager, analogous to RubyGems for Ruby or npm for Node.js. It manages project dependencies defined in a shard.yml file, allowing developers to easily fetch, install, and manage libraries (also called 'shards') from various sources, typically GitHub.

MACROS

Crystal supports powerful compile-time macros, which enable advanced metaprogramming capabilities. These macros allow developers to generate code dynamically at compile time, reducing boilerplate, extending the language's expressiveness, and improving performance without incurring runtime overhead.

CONCURRENCY

Crystal includes built-in primitives for highly efficient concurrent programming. It utilizes lightweight fibers (similar to goroutines in Go) for concurrent execution and channels for safe communication and synchronization between these fibers, simplifying the development of parallel and concurrent applications.

HISTORY

Crystal development began around 2011 at Manas Technology Solutions (later spun off as Crystal Language team), driven by the desire to merge the developer-friendliness and elegant syntax of Ruby with the performance characteristics of compiled languages. The project rapidly gained community interest and evolved over the years, with a strong focus on type inference and native compilation. A significant milestone was the release of Crystal 1.0.0 in March 2021, signifying its stability and readiness for widespread adoption in production environments.

SEE ALSO

ruby(1), go(1), rust(1), shards(1)

Copied to clipboard