Skip to content
Tutorials Center
My learning

Topics

Lesson 2 of 36 ยท Getting Started

Installing D and Your First Program

Install a D compiler on Windows, macOS or Linux, write and compile hello world, run scripts with rdmd, and understand what each part of a first D program does.

Before you can run D, you need a compiler. This lesson shows the installation options, walks through a first program line by line, and explains the three ways to run it: compile then execute, run directly with rdmd, or create a project with dub. Remember that D code on this site is read-only; follow along on your own machine, and use the C stand-ins here to rehearse the steps.

Choosing and installing a compiler

D has three production compilers that all accept the same language:

Compiler Back end Strength
dmd Own back end Fastest compile times, reference implementation
ldc2 LLVM Best optimized code, many targets
gdc GCC Integrates with the GCC toolchain

For learning, install dmd: it compiles quickly, so the edit-run cycle stays short. For release builds, many teams switch to ldc2. The usual routes are:

  • Windows: download the installer from dlang.org, or use a package manager such as Chocolatey.
  • macOS: brew install dmd (or brew install ldc).
  • Linux: the official install script (install.sh), your distribution's package (apt install dmd or ldc), or a snap.

After installing, open a new terminal and check:

Terminal
dmd --version
dub --version

The first prints the compiler version; the second confirms that dub, the build and package tool, was installed alongside it. If the command is not found, the installer's bin directory is not on your PATH.

Hello, world

Create a file called hello.d:

D
import std.stdio;

void main()
{
    writeln("Hello, D!");
}

Compile and run it:

Terminal
dmd hello.d
./hello

On Windows the executable is hello.exe. The program prints Hello, D!.

Line by line:

  • import std.stdio; makes the module std.stdio from the standard library (Phobos) visible. writeln lives there.
  • void main() is the entry point. It may also be written int main(string[] args) to read command-line arguments and return an exit code.
  • writeln prints its arguments and a newline. write prints without the newline, and writefln takes a format string like printf.

Three ways to run a D program

flowchart TD
    S["hello.d"] --> A["dmd hello.d: compile to an executable"]
    S --> B["rdmd hello.d: compile and run in one step"]
    S --> P["dub run: build and run a project"]
    A --> E["./hello"]

The same source can go through any of these paths.

  1. dmd file.d compiles to a native executable you can run again and again.
  2. rdmd file.d compiles to a temporary location and runs immediately, caching the result. It also finds imported local modules automatically. It is ideal for scripts and experiments. A first line of #!/usr/bin/env rdmd turns a D file into an executable script on Unix.
  3. dub init myproject creates a project folder with a dub.json or dub.sdl; dub run builds and runs it, downloading dependencies from the package registry when needed. You will meet dub in the compilers and build lesson.

Command-line arguments and exit codes

The longer form of main receives the arguments:

D
import std.stdio;

int main(string[] args)
{
    writeln("Program: ", args[0]);
    writeln("Argument count: ", args.length - 1);
    foreach (i, a; args[1 .. $])
    {
        writefln("  %s: %s", i + 1, a);
    }
    return 0;
}

args[0] is the program name, args[1 .. $] is a slice of everything after it ($ means "the length" inside an index), and the returned int becomes the process exit code, where 0 means success. If you run ./args one two, the argument count is 2.

Rehearsing in C

Real D is not available on the site, but the idea of "program name plus arguments plus exit code" is the same as in C. This stand-in feeds a fixed list of arguments into the same logic, so you can see how the slice args[1 .. $] corresponds to starting a loop at index 1.

C C stand-in: main with arguments

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

The output lists the program name, a count of 2 and the two arguments. In C the array length must travel in a separate argc; in D, args.length is built in. That difference, arrays that know their length, comes back in arrays and slices.

Common first-day problems

  • "module std.stdio is not found": the compiler cannot locate Phobos, usually because the installation is incomplete or an old PATH entry points to another compiler.
  • Output appears late or not at all: the standard output buffer is flushed on exit; calling stdout.flush() forces it earlier.
  • Mixing up dmd options: dmd -run hello.d compiles and runs, dmd -O -release hello.d optimizes, and dmd -c compiles without linking.
  • Missing semicolons: every statement needs one, but a function or class body does not take a trailing one.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which command compiles and runs a D file in one step?

    rdmd compiles to a cache and runs the program immediately.
  2. 2. In D, what does args[1 .. $] mean?

    Index 0 is the program name; the slice starts at 1 and runs to the length.
  3. 3. Which module do you import to use writeln?

    writeln, write and writefln are in std.stdio.
  4. 4. What does returning 0 from int main(string[] args) mean?

    By convention a zero exit code means success.

Summary

  • Install dmd for fast iteration, ldc2 for optimized builds, or gdc if you use the GCC toolchain.
  • dmd file.d builds an executable, rdmd file.d runs directly, and dub manages whole projects.
  • main may take string[] args and return an int exit code.
  • Arrays in D carry their length, unlike C's argv plus argc.

Next: Compilers, flags and the build process