DEV Community

Cover image for A very simple tutorial of the Crystal programming language

A very simple tutorial of the Crystal programming language

andrewrgarcia profile image Andrew Garcia ・3 min read

Alt Text

Want to learn a pretty new, cool and easy-to-use programming language? Crystal is a compiled language with the syntax ease of ruby and computational efficiency of C. Yes that's right, C.

YouTube here if you prefer the video format.


right click "Open Link in New Tab" here

For Ubuntu:

On a Terminal, Copy-paste the following (3) lines:

curl -sL "" | sudo apt-key add -

echo "deb crystal main" | sudo tee /etc/apt/sources.list.d/crystal.list

sudo apt-get update

This will declare the files to your apt install. Then type:

sudo apt install crystal

Also copy-paste these to install additional dependencies that you will most likely need later on to build projects:

sudo apt install libssl-dev # for using OpenSSL
sudo apt install libxml2-dev # for using XML
sudo apt install libyaml-dev # for using YAML
sudo apt install libgmp-dev # for using Big numbers
sudo apt install libz-dev # for using crystal play

To upgrade is as simple as writing these 2 lines:

sudo apt update
sudo apt install crystal

Hello World

open a Notepad or Text Editor and Type the following in it:

puts "Hello World!"

Save the file / give it a name. You can name it .cr is the extension for crystal.

Open a Terminal on the same path where you have saved. If on Ubuntu and your file is on your Desktop, your path would be ~/Desktop

Then while on such path type:

crystal run

This shall print Hello World!

Make a project which uses shards!

Alt Text

There are basically 2 types of projects you can create with Crystal: an application (app) or a library (lib). We will make an app. For that, go to the folder or loc where you want to create the project and on a Terminal type the following:

crystal init app firstapp

This should basically create a folder with all required files for your project. Open the created firstapp folder and you'll find something like this:

Alt Text

We are going to use as the example shard. Num is a core shard used for scientific computing in Crystal

Alt Text

If you had already clicked on shard links, you'll see that you need to add the following lines to your shard.yml file to make it work:

github: crystal-data/

Do it and save:

Alt Text

So now you need to install those declared dependencies to your project. Simply cd to your project on the terminal (cd firstapp) and then while inside firstapp, type:

shards install

If you get a red warning, you may need to install clang:

sudo apt install clang

Alt Text

Now go to the src folder and open the .cr file inside it.
In the first line of the file you're gonna type the following:

require "num"

This calls/imports the shard which you've declared in the shards.yml file and built with the shards install command.

After that, you can add to the file some interesting matrix objects and operations, and a lot more which the shard has to offer by going over its well-written documentation, and/or get your feet wet by copy-pasting my code here into your file and figuring it all out.

Happy coding πŸ¦„ 🌈


Discussion (0)

Editor guide