A very simple tutorial of the Crystal programming language
Tags: codenewbie, ruby, opensource, tutorial
Published: 2020-11-05 22:08:01


[![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/9fwdxzxrdpuogz3pb2xg.png)](https://crystal-lang.org/)

Want to learn a pretty new, cool and easy-to-use programming language? **_Crystal_** is a [compiled language](https://www.freecodecamp.org/news/compiled-versus-interpreted-languages/) with the syntax ease of ruby and computational efficiency of C. Yes that's right, C.  


#Installation 
[right click "Open Link in New Tab" here](https://crystal-lang.org/install/)

### For Ubuntu: 

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

```
curl -sL "https://keybase.io/crystal/pgp_keys.asc" | sudo apt-key add -
echo "deb https://dist.crystal-lang.org/apt 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 **helloworld.cr** .cr is the extension for crystal.

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

Then while on such path type:

`crystal run helloworld.cr`

This shall print **Hello World!**

# Make a project which uses shards!
This is an example I've developed. You may fork [my Github repository](https://github.com/andrewrgarcia/crystal-firstapp) which shows the most updated version of this example if found useful for learning purposes.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/d7n0f1rcq15vxl7ecebk.jpg)

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](https://dev-to-uploads.s3.amazonaws.com/i/pk9rkp9r8qo74pgtut8f.png)


We are going to use [num.cr](https://github.com/crystal-data/num.cr) as the example shard. [Num](https://github.com/crystal-data/num.cr) is a core shard used for scientific computing in Crystal

[![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/cq6lm09elcse2776ctpk.png)](https://github.com/crystal-data/num.cr)

If you had already clicked on num.cr shard links, you'll see that you need to add the following lines to your **shard.yml** file to make it work:
```
dependencies:
   num:
      github: crystal-data/num.cr
```
Do it and save:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/hrmdltqwufqrp3lf13kt.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/pk9rkp9r8qo74pgtut8f.png)

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

`require "num"`

This calls/imports the **num.cr** shard which you've declared in the **shards.yml** file and built with the _shards install_ command.

After that, you can add to the **firstapp.cr** file some interesting matrix objects and operations, and a lot more which the num.cr shard has to offer by going over its well-written [documentation](https://crystal-data.github.io/num.cr/).

Happy coding :unicorn: :rainbow:

-Andrew