Installing Cairo in your local Machine

Installing Cairo in your local Machine

It has never been any easier!

ยท

2 min read

As the official Cairo blog post suggests, "Cairo is the first production-grade platform for generating STARK proofs for general computation. It is Turing complete and highly effective". Cairo is essentially of great help for having large-scale executions on Ethereum at really low gas costs. This low-level language comes with some powerful syntactic sugar to allow for writing efficient and maintainable code.

This short article will walk you through Cairo's installation process on your local system in two easy steps. Let's get started!

P.S: If you are a windows user, using WSL instead of PowerShell or command prompt is highly recommended. You can install WSL on your system from here

Step 1: Installing python3.9

It is highly recommended to install and use python3.9 as Cairo was tested with python3.9

Check if your system has python3.9 already configured by executing:

python3.9 --version.

You should get something like this:

You can skip to Step 2 if you have python3.9 already configured on your system.

Run these commands in your terminal/WSL to get python3.9 on your local machine:

sudo apt update

sudo apt install software-properties-common

sudo add-apt-repository ppa:deadsnakes/ppa

sudo apt install python3.9

To verify for correct installation, run the following command in your terminal/WSL

python3.9 --version

You should get something like this:

Step 2: Installing Cairo

To create and enter the python virtual environment

python3.9 -m venv ~/cairo_venv
source ~/cairo_venv/bin/activate

You should see (cairo-venv) in the command-line prompt

Install some necessary dependencies like ecdsa , fastecdsa, sympy etc

For Linux and WSL

sudo apt install -y libgmp3-dev

For Mac

brew install gmp

Install the cairo-lang python package

pip3 install cairo-lang

Verifying Cairo installation

Create a file hello.cairo with the following content:

%builtins output

from starkware.cairo.common.serialize import serialize_word

func main{output_ptr: felt*} (){
    tempvar x = 10;
    serialize_word(x);
     return();
}

Note: Run all the commands only in virtual environment !

Execute the following command to compile the above code block:

cairo-compile hello.cairo --output hello_compiled.json

To run the compiled code:

cairo-run --program=hello_compiled.json \
    --print_output --layout=small

You should get the following output:

Congratulations! ๐Ÿคฉ You have successfully installed Cairo in your local system.

Best of luck at the beginning of your Cairo learning journey and see you for part two of the series.

ย