Skip to main content

Hello World (first programs) · Beginner · 14 min

Hello World in Dart: dart create, dart run and a compiled executable

By Padding · Updated 18 Nov 2026
Ubuntu 26.04 · Dart 3.14.1 · dart:3.14 image (Debian)

You will install Dart 3.14, create a console app with dart create, run it with dart run, compile it to a native executable, compare start-up times and greet a name from the command line with null-safe code.

On this page

Dart is the language you write Flutter apps in. One Dart codebase becomes an Android app, an iOS app, a web app and a desktop app, which is why it shows up in mobile teams at banks, fintechs and start-ups everywhere. Dart also runs on its own, for command-line tools and servers, and that is how you meet it here: no Flutter, no emulator, only the Dart SDK and a terminal.

The useful thing to understand early is that Dart runs your code in two ways. While you develop, dart run starts a virtual machine that compiles your code just in time (JIT), so changes show up immediately; Flutter's hot reload is built on this. For release, Dart compiles ahead of time (AOT) to native machine code, so the program starts fast and needs no Dart installed. You will use both in this tutorial and time the difference.

Dart is also null safe: a variable of type String can never hold null, and the compiler enforces it before your program runs. You will see that rule stop a build, and write code that handles a missing value properly.

You work in a throwaway folder, ~/hello_dart, and delete it at the end.

Step 1 · Install the Dart SDK

If you already installed Flutter, skip this step: the Flutter SDK includes the full Dart SDK, and dart lives in Flutter's bin folder.

On Ubuntu, Dart comes from Google's apt repository. First install the tools apt needs, then add Google's signing key so apt can check the packages it downloads:

Bash
sudo apt-get update && sudo apt-get install -y apt-transport-https gpg wget
wget -qO- https://dl-ssl.google.com/linux/linux_signing_key.pub \
  | sudo gpg --dearmor -o /usr/share/keyrings/dart.gpg

Add the repository. dpkg --print-architecture fills in your processor type (amd64 on most PCs, arm64 on ARM servers and a Raspberry Pi 4 or 5), then install Dart:

Bash
echo "deb [signed-by=/usr/share/keyrings/dart.gpg arch=$(dpkg --print-architecture)] https://storage.googleapis.com/download.dartlang.org/linux/debian stable main" \
  | sudo tee /etc/apt/sources.list.d/dart_stable.list
sudo apt-get update && sudo apt-get install -y dart

The SDK download is a few hundred megabytes. If you pay for data by the bundle, run this on Wi-Fi. To upgrade later, run the last line again.

On macOS, use Homebrew:

Bash
brew tap dart-lang/dart
brew trust dart-lang/dart
brew install dart

On Windows, open PowerShell and run winget install --id=Google.DartSDK -e, or choco install dart-sdk from an elevated PowerShell if you use Chocolatey. Open a new terminal afterwards. WSL users follow the Ubuntu commands.

Check the version:

Bash
dart --version

Check it worked

dart --version prints a line starting Dart SDK version: 3.14.1 (stable) and ending with your platform, such as on "linux_x64" or on "macos_arm64". If the shell says dart: command not found, open a new terminal; on Linux, check that /usr/lib/dart/bin exists and add it to your PATH if dart still is not found.

The first time you run a dart command it may print a note about anonymous usage statistics. dart --disable-analytics turns them off.

Step 2 · Create a console app with dart create

dart create writes a ready-to-run project from a template. The default template is a console app:

Bash
cd ~
dart create hello_dart
cd ~/hello_dart
ls -R bin lib test
cat pubspec.yaml

Dart package names use lowercase letters and underscores, so the folder is hello_dart, not hello-dart. After writing the files, dart create runs dart pub get, which downloads the packages the project lists. That needs an internet connection the first time.

What you got:

  • bin/hello_dart.dart is the entry point: the file that holds main. Programs you run live in bin/.
  • lib/hello_dart.dart is library code that bin/ and tests can import. The template puts a calculate() function here.
  • test/hello_dart_test.dart is a unit test for calculate().
  • pubspec.yaml names the package, the Dart versions it supports and its dependencies. pub is Dart's package manager, and pub.dev is where packages are published.
  • analysis_options.yaml turns on the recommended lint rules.

Run the template:

Bash
cd ~/hello_dart
dart run

With no file name, dart run runs bin/<package name>.dart.

Check it worked

ls -R lists hello_dart.dart under bin and lib, and hello_dart_test.dart under test. dart run prints Hello world: 42! (the 42 comes from calculate(), which returns 6 * 7). If you see Could not find a file named "pubspec.yaml", you are not inside ~/hello_dart.

Step 3 · Write Hello World yourself

Replace the template's entry point with the smallest Dart program:

Bash
cd ~/hello_dart
cat > bin/hello_dart.dart <<'DART'
void main() {
  print('Hello, world!');
}
DART
dart run

You can type the same three lines into your editor instead. Line by line:

  • void main() { declares the function Dart calls when the program starts. Every Dart program has one. void means it returns nothing. main is a top-level function: it does not need to sit inside a class, which is how Dart has always worked.
  • print('Hello, world!'); writes the text and a newline to the terminal. print is built into dart:core, the library every Dart file gets without an import. Strings can use single or double quotes; the Dart style guide prefers single. The ; ends the statement and is required.
  • } closes the function.

The lib/ file and the test are untouched. You no longer import calculate() from bin/, but the test still uses it, so leave lib/ as it is.

Check it worked

dart run prints Hello, world!. If it prints Hello world: 42!, the file was not saved; check that cat bin/hello_dart.dart shows your three lines.

Step 4 · Compile a native executable and compare start-up time

dart run starts the Dart VM, compiles your source and then runs it. That is fast enough for development and lets tools reload code while it runs. For something you ship, compile ahead of time instead:

Bash
cd ~/hello_dart
dart compile exe bin/hello_dart.dart -o hello
ls -lh hello
./hello
  • dart compile exe compiles the program and a small Dart runtime into one native executable for the machine you are on.
  • -o hello names the output file. Without it, Dart writes bin/hello_dart.exe, with .exe on every platform. On Windows, use -o hello.exe.
  • ./hello runs it. The ./ tells the shell to look in the current folder.

Now time both ways of running the same program:

Bash
cd ~/hello_dart
time dart run bin/hello_dart.dart
time ./hello

time prints three numbers after each run; real is the wall-clock time. Your numbers depend on your machine, but ./hello is many times faster to start, because there is nothing left to compile. That is the same reason a Flutter release build opens quickly on a low-cost Android phone: the app ships as AOT-compiled machine code.

You can copy hello to another computer with the same operating system and processor type and run it there without installing Dart.

Check it worked

ls -lh shows a file called hello of a few megabytes, and ./hello prints Hello, world!. In the timings, the real line for ./hello is clearly smaller than the one for dart run.

Step 5 · Greet a name from the command line with null safety

Now take a name from the command line, and handle the case where there is none. Replace the entry point:

Bash
cd ~/hello_dart
cat > bin/hello_dart.dart <<'DART'
void main(List<String> arguments) {
  final String? name = arguments.firstOrNull;
  print('Hello, ${name ?? 'world'}!');
}
DART

The new parts:

  • main(List<String> arguments) receives the words typed after the program name, as a list of strings. If there are none, the list is empty.
  • String? is a nullable string: it holds a string or null. Plain String can never be null, and the compiler holds you to that.
  • arguments.firstOrNull gives the first argument, or null when the list is empty. arguments.first would crash on an empty list instead.
  • final means the variable is set once and never reassigned.
  • ${...} inside a string inserts the value of an expression.
  • name ?? 'world' is the if-null operator: use name if it is not null, otherwise 'world'.

Run it with and without a name, then rebuild the executable and try that too:

Bash
cd ~/hello_dart
dart run bin/hello_dart.dart
dart run bin/hello_dart.dart Ada
dart compile exe bin/hello_dart.dart -o hello
./hello Adébáyọ̀

Dart strings are Unicode, so names with tone marks, or in Amharic or Arabic script, print as typed.

Check it worked

The three runs print Hello, world!, Hello, Ada! and Hello, Adébáyọ̀!. The last one came from the compiled hello, so the executable has the new code.

Step 6 · Let dart analyze and dart format catch mistakes

The Dart analyser checks your code without running it; editors with the Dart extension show the same messages as you type. Make two common mistakes on purpose so you recognise them.

Assigning a nullable value to a non-nullable variable. Drop the ? from the type:

Bash
cd ~/hello_dart
cat > bin/oops.dart <<'DART'
void main(List<String> arguments) {
  final String name = arguments.firstOrNull;
  print('Hello, $name!');
}
DART
dart analyze bin/oops.dart

The analyser reports something like A value of type 'String?' can't be assigned to a variable of type 'String', with the file, line and column, and the rule name invalid_assignment. dart run and dart compile refuse this file too. This is null safety doing its job: the missing-argument case has to be handled before the program can run.

A missing semicolon.

Bash
cd ~/hello_dart
printf "void main() {\n  print('hi')\n}\n" > bin/oops.dart
dart analyze bin/oops.dart

The message is something like Expected to find ';', labelled expected_token. Unlike Go or JavaScript, Dart never adds semicolons for you.

Untidy layout. Write valid but messy code, then let dart format fix it:

Bash
cd ~/hello_dart
printf "void main(){print( 'tidy' );}\n" > bin/oops.dart
dart format bin/oops.dart
cat bin/oops.dart
dart run bin/oops.dart
rm bin/oops.dart
dart analyze

Other mistakes beginners hit:

  • Hyphens in the project name. dart create hello-dart fails because the name is not a valid package name. Use underscores.
  • arguments[0] with no arguments. Indexing an empty list throws a RangeError at run time. firstOrNull with ?? avoids it.
  • Forgetting to rebuild. ./hello keeps running the old code until you run dart compile exe again; dart run always uses the current source.

Check it worked

You saw invalid_assignment and expected_token in your own terminal. After dart format, cat shows void main() { with print('tidy'); indented on its own line, and dart run bin/oops.dart prints tidy. The final dart analyze prints No issues found!.

Step 7 · Run the template's test with packages from pub

The test that dart create wrote uses package:test, which pub downloaded when the project was created. It is listed under dev_dependencies in pubspec.yaml, because it is needed for development but not by the finished program. Run it:

Bash
cd ~/hello_dart
dart test

To use a package from pub.dev in your own code, dart pub add <package> adds it to pubspec.yaml and downloads it; then you import it with import 'package:<package>/<package>.dart';.

Check it worked

dart test ends with All tests passed!. The test checks that calculate() returns 42, which still holds because you left lib/ unchanged.

Step 8 · Clean up

Delete the project folder:

Bash
cd ~
rm -rf ~/hello_dart

Keep Dart if you are moving on to Flutter. To remove it on Ubuntu, run sudo apt-get remove -y dart, delete /etc/apt/sources.list.d/dart_stable.list and /usr/share/keyrings/dart.gpg, and remove the ~/.dart* settings folders. Pub also keeps a download cache in ~/.pub-cache, which you can delete.

Check it worked

ls ~/hello_dart reports No such file or directory.

Next steps

Need this done for you?

Padding does this work for growing companies, from the first diagnosis to a system that keeps working.

Platform Engineering

Get new tutorials

New tutorials, about twice a month.