Cairo can be installed by simply downloading Scarb. Scarb bundles the Cairo compiler and the Cairo language server together in an easy-to-install package so that you can start writing Cairo code right away.


Scarb is also Cairo's package manager and is heavily inspired by Cargo, Rust’s build system and package manager.


Scarb handles a lot of tasks for you, such as building your code (either pure Cairo or Starknet contracts), downloading the libraries your code depends on, building those libraries, and providing LSP support for the VSCode Cairo 1 extension.


As you write more complex Cairo programs, you might add dependencies, and if you start a project using Scarb, managing external code and dependencies will be much easier.


Let's start by installing Scarb.


Installation


You can simply run the following command in your terminal, then follow the onscreen instructions. This will install the latest stable release.

curl --proto '=https' --tlsv1.2 -sSf https://docs.swmansion.com/scarb/install.sh | sh

Then restart your shell environment once more.

exec "$SHELL"

Run the following command to check if the installation was successful.

scarb --version


We have successfully installed Cairo and now it's time to print the Hello World code on the terminal, the traditional first step in the world of programming with Scarb.


Creating a New Project with Scarb:


Go back to the Projects directory. Then run the following:


scarb new hello_scarb


This creates a new directory and project called hello_scarb. Scarb creates files in a directory of the same name.


To go to the hello_scarb directory, use the command cd hello_scarb. You will see that Scarb creates a file called Scarb.toml and an src directory containing a file called lib.cairo.


This file is in TOML format, which is Scarb's configuration format. The first line, [package], is a section header indicating that you are configuring a package.


The other file Scarb creates is src/lib.cairo. Delete all its contents and add the following, the reason for which we will explain later:


mod hello_scarb;


Then create a new file called src/hello_scarb.cairo and add the following code:


use debug::PrintTrait;
fn main() {
'Hello World!'.print();
}


From the hello_scarb directory, compile your project with this command:


scarb cairo-run


If you installed Cairo correctly, you should see the following output:


[DEBUG] Hello, Scarb!


Anatomy of the Cairo Program


Let's examine this Hello World program in detail.


fn main() {

}


These lines define a function called main. The main function is the piece of code that always runs first in the Cairo program.


The function body is enclosed in { }. In Cairo, all function bodies are surrounded by curly braces.


Before the main function, we can use debug::PrintTrait; to import an element from another module. By doing this, we can use the print() method to print the code to the terminal. 


Defining Custom Scripts:


We can define Scarb scripts in the Scarb.toml file. Add the following line to your Scarb.toml file:


[scripts]
lib = "cairo-run --single-file src/lib.cairo"


You can use the following command to run the project:


scarb run run-lib