Dart Get Started
You can run every example in this course right in the browser. Sooner or later, though, you will want Dart on your own machine. This lesson shows how to install the SDK, create a project and run it.
Install the Dart SDK
The SDK contains the compiler, the virtual machine, the core libraries and the dart command-line tool.
| System | Command |
|---|---|
| Windows | choco install dart-sdk (or download the ZIP from dart.dev) |
| macOS | brew tap dart-lang/dart then brew install dart |
| Linux (Debian/Ubuntu) | add Google's apt repository, then sudo apt install dart |
| Already have Flutter? | Nothing to do — Flutter bundles Dart (flutter/bin/dart) |
Check that it works:
dart --versionDart SDK version: 3.7.3 (stable) on "linux_x64"
Note: The terminal examples in this lesson are simulated — they show what you would see on your own computer.
Create a project
dart create generates a ready-to-run project. The default template is a command-line app.
dart create greeterCreating greeter using template console...
.gitignore
analysis_options.yaml
CHANGELOG.md
pubspec.yaml
README.md
bin/greeter.dart
lib/greeter.dart
test/greeter_test.dart
Running pub get...
Resolving dependencies...
Changed 46 dependencies!
Created project greeter in greeter! In order to get started, run the following commands:
cd greeter
dart run
The important files:
| Path | Purpose |
|---|---|
pubspec.yaml |
Project name, SDK constraint and dependencies |
bin/greeter.dart |
The entry point containing main |
lib/ |
Reusable library code, imported by bin/ and test/ |
test/ |
Unit tests run with dart test |
analysis_options.yaml |
Lint rules used by the analyzer |
A minimal pubspec.yaml looks like this:
name: greeter
description: A sample command-line application.
version: 1.0.0
environment:
sdk: ^3.7.0
dev_dependencies:
lints: ^5.0.0
test: ^1.24.0Run the program
Inside the project folder, dart run compiles and runs bin/<project>.dart:
cd greeter
dart runBuilding package executable...
Built greeter:greeter.
Hello world: 42!
You can also run any single file directly — handy for quick experiments:
dart run hello.dartHere is a script you could save as hello.dart. It reads command-line arguments through the optional args parameter of main:
void main(List<String> args) {
final name = args.isEmpty ? 'world' : args.first;
print('Hello, $name!');
print('You passed ${args.length} argument(s).');
}Hello, world!
You passed 0 argument(s).
Running it as dart run hello.dart Lan would print Hello, Lan! and You passed 1 argument(s).
The everyday toolbox
| Command | What it does |
|---|---|
dart run |
Run a program with the JIT VM (fast startup while developing) |
dart compile exe bin/app.dart |
Build a self-contained native executable (AOT) |
dart compile js / dart compile wasm |
Compile for the browser |
dart format . |
Format all files in the official style |
dart analyze |
Report errors, warnings and lint hints |
dart fix --apply |
Automatically apply suggested fixes |
dart test |
Run the tests in test/ |
dart pub add <package> |
Add a dependency from pub.dev |
dart compile exe bin/greeter.dart -o greeter
./greeterGenerated: /home/lan/greeter/greeter
Hello world: 42!
The executable runs without the SDK installed — perfect for distributing CLI tools.
Editors
Install the official Dart extension for VS Code or the Dart plugin for IntelliJ/Android Studio. You get completion, inline errors, quick fixes, formatting on save and a debugger.
Tip: Turn on "format on save".
dart formatsettles all style debates, and every Dart codebase you read will look the same.
Test yourself
Which command creates a new Dart console project?
- dart new app
- dart init
- dart create app
- pub create app
Which file lists a package's dependencies?
- package.json
- pubspec.yaml
- analysis_options.yaml
- dart.toml
What does dart compile exe produce?
- A JavaScript bundle
- A self-contained native executable
- A .jar file
- Bytecode for the JIT