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:
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.gpgAdd 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:
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 dartThe 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:
brew tap dart-lang/dart
brew trust dart-lang/dart
brew install dartOn 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:
dart --versionCheck 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:
cd ~
dart create hello_dart
cd ~/hello_dart
ls -R bin lib test
cat pubspec.yamlDart 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.dartis the entry point: the file that holdsmain. Programs you run live inbin/.lib/hello_dart.dartis library code thatbin/and tests can import. The template puts acalculate()function here.test/hello_dart_test.dartis a unit test forcalculate().pubspec.yamlnames 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.yamlturns on the recommended lint rules.
Run the template:
cd ~/hello_dart
dart runWith 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:
cd ~/hello_dart
cat > bin/hello_dart.dart <<'DART'
void main() {
print('Hello, world!');
}
DART
dart runYou 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.voidmeans it returns nothing.mainis 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.printis built intodart: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:
cd ~/hello_dart
dart compile exe bin/hello_dart.dart -o hello
ls -lh hello
./hellodart compile execompiles the program and a small Dart runtime into one native executable for the machine you are on.-o hellonames the output file. Without it, Dart writesbin/hello_dart.exe, with.exeon every platform. On Windows, use-o hello.exe../helloruns it. The./tells the shell to look in the current folder.
Now time both ways of running the same program:
cd ~/hello_dart
time dart run bin/hello_dart.dart
time ./hellotime 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:
cd ~/hello_dart
cat > bin/hello_dart.dart <<'DART'
void main(List<String> arguments) {
final String? name = arguments.firstOrNull;
print('Hello, ${name ?? 'world'}!');
}
DARTThe 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 ornull. PlainStringcan never benull, and the compiler holds you to that.arguments.firstOrNullgives the first argument, ornullwhen the list is empty.arguments.firstwould crash on an empty list instead.finalmeans the variable is set once and never reassigned.${...}inside a string inserts the value of an expression.name ?? 'world'is the if-null operator: usenameif it is notnull, otherwise'world'.
Run it with and without a name, then rebuild the executable and try that too:
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:
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.dartThe 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.
cd ~/hello_dart
printf "void main() {\n print('hi')\n}\n" > bin/oops.dart
dart analyze bin/oops.dartThe 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:
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 analyzeOther mistakes beginners hit:
- Hyphens in the project name.
dart create hello-dartfails because the name is not a valid package name. Use underscores. arguments[0]with no arguments. Indexing an empty list throws aRangeErrorat run time.firstOrNullwith??avoids it.- Forgetting to rebuild.
./hellokeeps running the old code until you rundart compile exeagain;dart runalways 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:
cd ~/hello_dart
dart testTo 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:
cd ~
rm -rf ~/hello_dartKeep 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
- Use the same language to build an app with a screen: install Flutter and run your first app, where hot reload shows the JIT side of Dart at work.
- Compare Dart with the native language for Android: Hello World in Kotlin.
- Compare it with the native language for iPhone apps: Hello World in Swift.
Part of these paths
Related tutorials
- How to Install Flutter 3.50 and Run Your First AppYou will install the Flutter 3.50 SDK on macOS or Linux, check your setup with flutter doctor, create an app, run it in a browser or on a device, change it with hot reload, replace the counter with your own screen, and write a widget test.
- Hello World in Kotlin: kotlinc, a runnable JAR and null safetyYou will install the Kotlin 2.4 command-line compiler, build Hello World into a self-contained JAR that runs with java -jar, and extend it to read a name safely when no input arrives.
- Hello World in Swift on Linux, macOS and Windows with the Swift package managerYou will install Swift 6.4 with swiftly, run a one-line script, create and run an executable package with swift run, and greet the reader by name using an optional from readLine().
Need this done for you?
Padding does this work for growing companies, from the first diagnosis to a system that keeps working.
Get new tutorials