Programming languages form the foundation for any developer or programmer. It is one of the first things to learn if you’re going on the development path. With a multitude of new programming languages entering the market, choosing the right one can be difficult. Most people just stick to the popular languages like Java, C++, or Python. And for people who like to experiment and try new things, Crystal can be a good option. It is relatively new.
Installing Crystal on Linux involves a series of steps that depend on your specific Linux distribution. First, ensure you have root or sudo permissions and a stable internet connection. Update your system and install required dependencies like libssl-dev and libxml2-dev. You can then proceed to install Crystal through various methods such as using package managers like apt, dnf, and pacman, or via snap. You can also download and install it directly from the official GitHub releases. Once installed, verify the installation by checking the version using the ‘crystal –version’ command.
Key Features of Crystal:
- It is statically typed, which means the data type is known as compile time.
- Being a compiled language, Crystal boasts faster performance than interpreted languages
- The type inference lets the compiler decide the data type without explicitly assigning it.
- The syntax is user-friendly, human-readable, and clean just like in Ruby language.
- The compiler checks for null references at compile time.
- It supports concurrency by using green threads called fibers which communicate through channels.
- Crystal is an object-oriented language, supporting OOP concepts such as classes, inheritance, interfaces, abstraction, and polymorphism
- Crystal can effortlessly call native libraries using its dedicated syntax
Preparation: What You Need Before Installing Crystal on Linux
You should take care of the following prerequisites before installing Crystal on Linux:-
- You need sudo or root permissions to your system. Installing Crystal requires administrative privileges.
- You should have a stable internet connection for downloading Crystal.
- You should update your system and package manager’s repositories as well.
- Install the required system dependencies such as libssl-dev, libxml2-dev, libyaml-dev, libgmp-dev, and libz-dev.
Step-by-Step Guide: Installing Crystal on Linux
Various methods exist for installing Crystal on Linux, including using default package managers like apt, dnf, pacman, and snap, as well as direct package files. Below we’ve listed the steps of installation for these methods.
Installing Crystal via the Standalone file
The .tar.gz zip file of Crystal package can be installed from the official GitHub releases page. You can download it and then open the terminal in the directory where the file is downloaded. Extract the file using the following command:-
tar xvzf <filename.tar.gz>
Create a symbolic link to make it easier to use by the command:-
ln -s /full/path/to/bin/crystal /usr/local/bin/crystal
To check if it is installed use the command:-
Installing Crystal on Linux via snap
If snap is installed in your system then use the following command to install Crystal:-
sudo snap install crystal --classic
If you want the latest nightly build you can use the following command:-
sudo snap install crystal --classic --edge
How to Install Crystal on Ubuntu/Debian-based Systems
You can use the official deb or rpm repository for Crystal to install it in your system. The command to do so is:-
curl -fsSL https://crystal-lang.org/install.sh | sudo bash
Run the installer and go through the installation process.
You can also add additional arguments to install a specific version or use a specific channel. The arguments with their values are as follows:-
--version with major.minor or latest
--channel with stable, unstable, or nightly
How to Install Crystal on openSUSE-based Systems
We’ll first configure the Crystal repository in the Zypper package manager. To do so use the following command:-
For OpenSUSE Tumbleweed :
sudo zypper ar -f https://download.opensuse.org/repositories/devel:/languages:/crystal/openSUSE_Tumbleweed/devel:languages:crystal.repo
For OpenSUSE Leap 15.4:
sudo zypper ar -f https://download.opensuse.org/repositories/devel:/languages:/crystal/15.4/devel:languages:crystal.repo
After configuring you can install Crystal via:-
sudo zypper --gpg-auto-import-keys install crystal
Setting Up Crystal on Arch Linux Systems
The Crystal package can be installed through the default pacman package manager using the following command:-
sudo pacman -S crystal shards
Getting Started: A Quick Crystal Tutorial
Crystal is just like any other programming language. In this tutorial, we’ll print Hello World.
The print command in Crystal is puts. So type the following code to print Hello World.
puts "Hello World!"
To run it, open the terminal and use the command
$ crystal hello_world.cr
You can store data in the variable as follows:-
Variable_name = data.
message = "Hello Penny!"
The data type in this case is string.
For a deeper dive into the language, we highly recommend exploring the official Crystal documentation.
Conclusion: Why Choose Crystal?
Crystal is quite new to the market as compared to other established languages. It brings a lot of new features incorporated from recent languages. As of August 2023, the latest Crystal release is v1.9.2, launched on July 19, 2023. The update brought some minor improvements and features to the language. With Crystal’s growing popularity and new features, isn’t it time to consider whether this language could be the next step in your development journey?