Getting Started with Cairo
Welcome to Cairo! This tutorial will help you quickly set up your Cairo development environment, write your first Cairo program, and get directions for the next steps in your Cairo journey.
You can also try Cairo without installing anything on your computer in the Cairo playground.
Installing Cairo
Cairo can be installed by simply installing Scarb, which bundles the Cairo compiler and the Cairo language server together in an easy-to-install package.
Scarb is heavily inspired by Cargo, Rust’s build system and package manager, with the goal of making programmers used to writing Rust feel at home. Similar to Cargo, Scarb bundles all common actions into a single command, including:
- Adding packages from scarbs.xyz with scarb add
- Building lean binaries using scarb build
- Running tests with scarb test
- Generating documentation using scarb doc
It is highly recommended to install Scarb via the asdf version manager. Each Scarb version is associated with a specific Cairo version, and using asdf allows you to switch between different compiler versions conveniently.
After installing asdf (and making sure you added asdf to your ~/.bashrc or ~/.zshrc files as detailed in the installation instructions), run the following commands to install Scarb:
asdf plugin add scarb
asdf install scarb latest
asdf global scarb latest
To verify that Scarb was installed successfully, run:
Writing your first Cairo program
Now that we have a Cairo installed, let’s write our first Cairo program! You can use Scarb to create a new Cairo package by running:
and choosing to set up Cairo Test as your test runner The above will create the following directory package:
hello_cairo
|- Scarb.toml
|- src
|- lib.cairo
Similar to Cargo, Scarb.toml is the manifest file for Cairo (where you keep your project metadata and dependencies), while src/lib.cairo is where you write your code. scarb new creates a default template that calculates the 16’th Fibonacci number:
fn main() -> felt252 {
fib(16)
}
fn fib(mut n: felt252) -> felt252 {
let mut a: felt252 = 0;
let mut b: felt252 = 1;
while n != 0 {
n = n - 1;
let temp = b;
b = a + b;
a = temp;
};
a
}
#[cfg(test)]
mod tests {
use super::fib;
#[test]
fn it_works() {
assert(fib(16) == 987, 'it works!');
}
}
Indeed, we can see that the above code has a function called fib for calculating the n’th Fibonacci number and a main function that returns fib(16).
Additionally, we have a test module with a single test function that asserts that the value returned by fib(16) is 987. For those familiar with Rust, the similarity between the languages is evident (but if you’re unfamiliar with Rust, don’t let it discourage you!). To execute the main function, simply run:
If the template project is not changed, this should print:
Run completed successfully, returning [987]
You can also execute all the test functions (functions annotated with #[test] inside modules annotated with #[cfg(test)]) by running:
If the template project is not changed, this should print:
testing hello_cairo ...
running 1 tests
test hello_cairo::tests::it_works ... ok (gas usage est.: 33260)
test result: ok. 1 passed; 0 failed; 0 ignored; 0 filtered out;
Taking your next steps
Congratulations, you’ve just written your first provable lines of code!
This is just the tip of the pyramid, though: If you’re interested in learning more about Cairo, the best next stop is the Cairo book.
Inspired by the Rust book, the Cairo book gives a thorough overview of Cairo, including its key application – writing Starknet smart contracts.
So pour yourself a cup of tea, find a comfy corner, and let the pages of the Cairo book whisk you away to the next chapter of your Cairo adventure!