安装

只需下载 Scarb,即可安装 Cairo。Scarb 将 Cairo 编译器和 Cairo 语言服务器捆绑在一个易于安装的软件包中,这样你就可以立即开始编写 Cairo 代码了。

Scarb同样是Cairo的软件包管理器,在很大程度上受到Cargo的启发,Rust的构建系统和软件包管理器。

Scarb会为你处理很多任务,比如构建你的代码(纯Cairo或Starknet合约),为你下载你的代码所依赖的库并构建他们,以及为VSCode Cairo 1扩展提供LSP支持。

如果你使用 Scarb 启动项目,管理外部代码和依赖关系就会容易得多。

Starknet Foundry is a toolchain for Cairo programs and Starknet smart contract development. It supports many features, including writing and running tests with advanced features, deploying contracts, interacting with the Starknet network, and more.

Let's start by installing Scarb and Starknet Foundry.

Installing Scarb

Requirements

Scarb需要PATH环境变量里有一个Git可执行文件。

安装

To install Scarb, please refer to the installation instructions. We strongly recommend that you install Scarb via asdf, a CLI tool that can manage multiple language runtime versions on a per-project basis. This will ensure that the version of Scarb you use to work on a project always matches the one defined in the project settings, avoiding problems related to version mismatches.

Please refer to the asdf documentation to install all prerequisites.

Once you have asdf installed locally, you can download Scarb plugin with the following command:

asdf plugin add scarb

This will allow you to download specific versions:

asdf install scarb 2.9.1

and set a global version:

asdf global scarb 2.9.1

Otherwise, you can simply run the following command in your terminal, and follow the onscreen instructions. This will install the latest stable release of Scarb.

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

In both cases, you can verify installation by running the following command in a new terminal session, it should print both Scarb and Cairo language versions, e.g:
$ scarb --version
scarb 2.9.1 (aba4f604a 2024-11-29)
cairo: 2.9.1 (https://crates.io/crates/cairo-lang-compiler/2.9.1)
sierra: 1.6.0

Installing Starknet Foundry

To install Starknet Foundry, please refer to the installation instructions. We also recommend that you install it via asdf.

Once installed, you can run the following command to see the version:

$ snforge --version
snforge 0.33.0

We'll describe Starknet Foundry in more detail in Chapter 10 for Cairo programs testing and in Chapter 17 when discussing Starknet smart contract testing and security in the second part of the book.

Installing the VSCode Extension

Cairo has a VSCode extension that provides syntax highlighting, code completion, and other useful features. You can install it from the VSCode Marketplace. Once installed, go into the extension settings, and make sure to tick the Enable Language Server and Enable Scarb options.