Contents

Get started: command-line and server apps

Follow these steps to start using the Dart SDK to develop command-line and server apps. First you’ll play with the Dart language in your browser, no download required. Then you’ll install the Dart SDK, write a small program, and run that program using the Dart VM. Finally, you’ll use an AOT (ahead of time) compiler to compile your finished program to native machine code, which you’ll execute using the Dart runtime.

1. Play with Dart code in DartPad

With DartPad you can experiment with the Dart language and APIs, no download necessary.

For example, here’s an embedded DartPad that lets you play with the code for a small Hello World program. Click Run to run the app; output appears in the console view. Try editing the source code — perhaps you’d like to change the greeting to use another language.

void main() {
  print('Hello, World!');
}

More information:

2. Install Dart

Once you’re ready to move beyond DartPad and develop real apps, you need the Dart SDK.

Use Chocolatey to install a stable release of the Dart SDK:

  C:\> choco install dart-sdk

You can use Aptitude to install the Dart SDK on Linux.

  1. Perform the following one-time setup:
    $ sudo apt-get update
    $ sudo apt-get install apt-transport-https
    $ sudo sh -c 'curl https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -'
    $ sudo sh -c 'curl https://storage.googleapis.com/download.dartlang.org/linux/debian/dart_stable.list > /etc/apt/sources.list.d/dart_stable.list'
    
  2. Install the Dart SDK:
    $ sudo apt-get update
    $ sudo apt-get install dart
    

With Homebrew, installing Dart is easy.

  $ brew tap dart-lang/dart
  $ brew install dart

3. Get more command-line developer tools

Install stagehand, which gives you templates for creating Dart apps:

$ pub global activate stagehand

Note that although these instructions feature the command line, many IDEs support Dart development. Those IDEs use Stagehand behind the scenes when you create new Dart projects.

More information:

4. Create a small app

Create a command-line app:

$ mkdir cli
$ cd cli
$ stagehand console-full

These commands create a small Dart app that has the following:

  • A main Dart source file, bin/main.dart, that contains a top-level main() function. This is the entrypoint for your app.
  • An additional Dart file, lib/cli.dart, that contains the functionality of the app and is imported by the main.dart file.
  • A pubspec file, pubspec.yaml, that contains the app’s metadata, including information about which packages the app depends on and which versions of those packages are required.

5. Get the app’s dependencies

Use the pub command to get the packages that the app depends on:

$ pub get

6. Run the app

To run the app from the command line, use the Dart VM by running the dart command:

$ dart bin/main.dart
Hello world: 42!

If you wish run the app with debugging support, see DevTools.

7. Modify the app

Let’s customize the app you just created.

  1. Edit lib/cli.dart to calculate a different result. For example, divide the previous value by two (for details about ~/, see Arithmetic operators):

    int calculate() {
      return 6 * 7 ~/ 2;
    }
  2. Save your changes.

  3. Rerun the main entrypoint of your app:

    $ dart bin/main.dart
    Hello world: 21!
    

More information: Write command-line apps

8. Compile for production

The steps above used the Dart VM (dart) to run the app. The Dart VM is optimized for fast, incremental compilation to provide instant feedback during development. Now that your small app is done, it’s time to AOT compile your Dart code to optimized native machine code.

Use the dart2native tool to AOT compile the program to machine code:

$ dart2native bin/main.dart -o bin/my_app

Notice how the compiled program starts instantly, completing quickly:

$ time bin/my_app
Hello world: 21!

real	0m0.016s
user	0m0.008s
sys	0m0.006s

What next?

Check out these resources:

If you get stuck, find help at Community and support.