Basics

Crystal Shards

Using Crystal Shards

Crystal shards are packages managed with shards.yml.

What Are Crystal Shards?

Crystal shards are libraries or packages used to enhance the functionality of Crystal applications. They are managed using a file named shards.yml, which defines the dependencies for your project. Shards can include libraries for various purposes, such as database access, HTTP requests, or testing frameworks.

Creating a Shards File

To start using shards in your Crystal project, you need to create a shards.yml file in the root directory of your project. This file specifies the dependencies your project relies on, as well as their respective versions.

Adding Dependencies

Inside the shards.yml file, you can add dependencies under the dependencies section. Each dependency requires the name, the source (e.g., GitHub repository), and the version or version constraints. For example, to add the Kemal web framework:

Installing Shards

Once you have added your dependencies in the shards.yml file, you can install them by running the shards install command in your terminal. This command will download all the specified shards and place them into the lib directory of your project.

Updating Shards

To update your dependencies to their latest compatible versions, you can use the shards update command. This command checks for newer versions of the shards based on the constraints specified in shards.yml and updates them accordingly.

Using Installed Shards

After installing the shards, you can use them in your Crystal application by requiring them in your code. For example, if you installed the Kemal shard, you can include it in your application as follows:

Best Practices for Managing Shards

  • Version Constraints: Use version constraints like ~> to ensure compatibility with future updates.
  • Review Dependencies: Regularly review the shards.yml file to manage and update dependencies efficiently.
  • Check for Security: Keep an eye on security advisories for the shards you use to ensure your application remains secure.
Previous
Modules
Next
puts