Installing Dart
Installing the Dart SDK
Before you can start writing Dart programs, you need to set up the Dart SDK (Software Development Kit) on your computer. This guide will walk you through the installation process on different operating systems and help you verify that everything is working correctly.
How to Install Dart
1. Installing the Dart SDK
The Dart SDK includes the Dart VM, compiler, and core libraries needed to run and develop Dart applications.
Windows Installation
- Download the installer (from the official Dart website: https://dart.dev/get-dart).
- Run the installer (and follow the instructions).
- Add Dart to PATH(the installer should do this automatically).
- Verify installation (by opening Command Prompt and running).
dart --version
You should see the installed Dart version (e.g., Dart SDK version: 3.4.1).
macOS Installation
You can install Dart using Homebrew:
- Open Terminal.
- Run:
brew tap dart-lang/dart
brew install dart
- Verify with:
dart --version
Linux Installation (Debian/Ubuntu)
- Add the Dart repository:
sudo apt-get update
sudo apt-get install apt-transport-https
sudo sh -c 'wget -qO- https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -'
sudo sh -c 'wget -qO- https://storage.googleapis.com/download.dartlang.org/linux/debian/dart_stable.list > /etc/apt/sources.list.d/dart_stable.list'
- Install Dart:
sudo apt-get update
sudo apt-get install dart
- Verify with:
dart --version
Running Your First Dart Program
Once Dart is installed, you can run Dart code in two ways:
- Using Dart CLI (Command Line)
- Using DartPad (Online Editor)
Method 1: Running Dart from the Command Line
- Create a file named hello.dart with the following code:
void main() {
print("Hello, Dart!");
}
- Save the file and run it using:
dart run hello.dart
- Output:
Hello, Dart!
Method 2: Using DartPad (No Installation Needed)
If you don’t want to install Dart yet, you can use DartPad, an online Dart editor.
- Go to https://dartpad.dev.
- Write the same code:
void main() {
print("Hello, Dart!");
}
Common Installation Mistakes & Fixes
- Dart Command Not Found
Problem:
After installing Dart, running dart --version gives an error like:
'dart' is not recognized as an internal or external command.
Solution:
- Windows: Ensure Dart is added to PATH during installation.
- macOS/Linux: Restart the terminal or run source ~/.bashrc (or source ~/.zshrc).
Outdated Dart Version
Problem:
Some features may not work if using an old Dart version.
Solution:
- Windows:Reinstall from the Dart website.
- macOS: Run brew upgrade dart.
- Linux: Run sudo apt-get upgrade dart.
Permission Errors (Linux/macOS)
Problem:
Installation fails due to permission issues.
Solution:
Use sudo for installation commands (e.g., sudo apt-get install dart).