Run a local development environment

Get set up with everything you need to integrate existing smart contracts with the blockchain in a local testing environment.

You will learn how to:

  • Set up a new project
  • Create a simple Clarity contract to deploy to devnet
  • Spin up a local blockchain using the Clarinet CLI

Create a new project

To demonstrate how to run a local blockchain, we'll need a project to work with. If you don't already have one, we can create a simple hello-world project below.

Terminal
clarinet new hello-world

This will create a new directory with our project already configured.

Create a new contract

Before we spin up our local blockchain, we should deploy something to it - let's go ahead and change directories into our project and create a hello contract.

Terminal
cd hello-world
clarinet contract new hello

In our new hello.clar file, let's add a few functions: say-hi, echo-number, and check-it.

hello.clar
;; A read-only function that returns a message
(define-read-only (say-hi)
  (ok "Hello World")
)

;; A read-only function that returns an input number
(define-read-only (echo-number (val int))
  (ok val)
)

;; A public function that conditionally returns an ok or an error
(define-public (check-it (flag bool))
  (if flag (ok 1) (err u100))
)

Run a local blockchain with clarinet devnet start

We're now ready to start up our local blockchain and deploy our hello-world contract. Run the following command and let's walkthrough what's happening under the hood.

Terminal
clarinet devnet start

At this point, clarinet will start creating a deployment plan for your devnet network and begin spinning up all the necessary services required and deploying our hello contract.

Common errors

If you are getting error: clarinet was unable to create network, make sure you have Docker (or a similar service) installed and running locally. Check out the Docker installation guide for more details.

Next step: Connect your local devnet to a web wallet

Now that you have a local devnet running on your machine, it would be nice to be able to interact with your local contracts deployed to that network from a frontend application. This step-by-step guide has you covered.

More resources