Hello World (first programs) · Beginner · 15 min
Hello World in D with LDC: writeln, dub and UFCS
By Padding · Updated 18 Nov 2026
Ubuntu 26.04 · LDC 1.43.0 (D frontend 2.113)
You will install the LDC 1.43.0 D compiler on Ubuntu 26.04, compile and run Hello World with ldc2 and ldc2 -run, see a function run at compile time, chain calls with uniform function call syntax, and build a small greeter project with dub.
On this page
D is a compiled language with C-like syntax: curly braces, semicolons, int and char, and a main function where the program starts. If you have read any C, C++, Java or C# you can read D straight away. What it changes is the work around that syntax. Memory is managed by a garbage collector by default, so you do not free strings and arrays by hand. Arrays know their own length. A large standard library, Phobos, ships with every compiler. And D can run ordinary functions while it compiles your program, which it uses for checks and generated code that other languages need macros or separate tools for.
D has three compilers that share one frontend (the part that reads and checks D code):
- DMD, the reference compiler from the D Language Foundation, currently 2.114. It compiles very quickly but only generates code for x86 and x86-64 processors.
- LDC, which puts the same frontend on top of LLVM, the code generator Clang and Rust also use. It produces faster programs and runs on ARM64 as well. This tutorial uses LDC 1.43.0, whose frontend matches D 2.113.
- GDC, part of GCC.
Every LDC download also includes dub, D's package manager and build tool, so one archive gives you everything here.
You work in a throwaway folder, ~/hello-d-demo, and delete it at the end.
Step 1 · Download LDC and check its checksum
Ubuntu's own package of LDC tends to lag behind, so you download the official release archive from the LDC project on GitHub. LDC uses your system's C linker to produce the final program, so install gcc and the C library headers alongside the download tools:
sudo apt update
sudo apt install -y curl ca-certificates xz-utils gcc libc6-dev
VERSION=1.43.0
case "$(uname -m)" in
x86_64) ARCH=x86_64 ;;
aarch64) ARCH=aarch64 ;;
*) echo "Unsupported processor: $(uname -m)"; false ;;
esac
TARBALL="ldc2-${VERSION}-linux-${ARCH}.tar.xz"
mkdir -p ~/hello-d-demo/download
cd ~/hello-d-demo/download
curl -fSLO "https://github.com/ldc-developers/ldc/releases/download/v${VERSION}/${TARBALL}"
curl -fsSLO "https://github.com/ldc-developers/ldc/releases/download/v${VERSION}/ldc2-${VERSION}.sha256sums.txt"
grep " ${TARBALL}\$" "ldc2-${VERSION}.sha256sums.txt" | sha256sum -c -The case block picks the archive for your processor: uname -m prints x86_64 on most laptops and servers and aarch64 on ARM machines. The last line takes the expected SHA-256 checksum for your archive from the release's list and compares it with the file you downloaded.
On a prepaid or metered connection, download the archive once and keep the file; it installs on any Ubuntu machine with the same processor type.
Check it worked
The last line prints ldc2-1.43.0-linux-x86_64.tar.xz: OK (or aarch64). If it prints FAILED, delete the archive and download it again. Do not unpack a file that fails this check.
Step 2 · Install LDC in your home folder
Unpack the archive into ~/.local/opt and put its bin folder on your path:
mkdir -p ~/.local/opt
tar -xf "$TARBALL" -C ~/.local/opt
LDC_BIN="$HOME/.local/opt/ldc2-${VERSION}-linux-${ARCH}/bin"
echo "export PATH=\"$LDC_BIN:\$PATH\"" >> ~/.bashrc
export PATH="$LDC_BIN:$PATH"
ldc2 --version | head -n 3
dub --versionThe echo line adds the path to ~/.bashrc, so new terminals find ldc2 and dub; the export line makes it work in this one now. You add the whole bin folder rather than linking single programs because ldc2 finds its standard library through its own location, and dub looks for ldc2 on your path.
A compiler you unpack yourself gets no updates from apt. To upgrade, repeat these two steps with a newer VERSION, change the line in ~/.bashrc to the new folder, and delete the old folder from ~/.local/opt.
On Windows, download ldc2-1.43.0-windows-multilib.exe from the same release page and run the installer, or follow these steps inside WSL. On macOS, use the osx-arm64 or osx-x86_64 archive the same way, or Homebrew (brew install ldc).
Check it worked
ldc2 --version starts with LDC - the LLVM D compiler (1.43.0): and names the D and LLVM versions it is based on. dub --version prints a line starting DUB version. If a new terminal says ldc2: command not found, open ~/.bashrc and check the last line points at the folder you unpacked.
Step 3 · Write and compile hello.d
Create the source file:
mkdir -p ~/hello-d-demo
cd ~/hello-d-demo
cat > hello.d <<'D'
import std.stdio;
void main()
{
writeln("Hello, World!");
}
DEach line, in order:
import std.stdio;makes the standard input and output module of Phobos available. Modules are D's unit of code: one file is one module, andstd.stdiolives in a file calledstdio.din thestdfolder of the library. There are no header files and no preprocessor;importreads the module itself.void main()declares the function the program starts in.voidmeans it returns nothing; D then reports success (exit code 0) to the operating system for you.maincan also returnintor takestring[] argswhen you need them.{and}mark the body of the function, as in C.writeln("Hello, World!");writes the text followed by a newline to standard output. Thelnstands for line;writedoes the same without the newline. Both take any number of values of any type and convert them to text, sowriteln("Total: ", 3 + 4)works without a format string.
Compile it with ldc2 and run the result:
ldc2 hello.d
ls
./helloldc2 hello.d compiles the file to machine code and calls the linker, which joins it with the D runtime and Phobos into a program named after the source file. The D runtime is the small library that starts the garbage collector and calls your main.
Check it worked
./hello prints Hello, World!. ls shows hello (the program) and hello.d, and usually hello.o, the compiled object file the linker worked from. You can delete hello.o; ldc2 -of=greeting hello.d would name the program greeting instead.
Step 4 · Run in one step and see compile-time evaluation
While you are trying things out, ldc2 -run compiles to a temporary program, runs it and deletes it:
ldc2 -run hello.d-run must be the last compiler option, because everything after the file name is passed to your program as arguments.
Now see one of D's distinctive features. Create ctfe.d, which calls an ordinary function while the program compiles:
cat > ctfe.d <<'D'
import std.stdio;
int square(int n)
{
return n * n;
}
enum int atCompileTime = square(12);
static assert(atCompileTime == 144);
pragma(msg, "square(12) worked out while compiling: ", atCompileTime);
void main()
{
writeln("The program only prints the stored answer: ", atCompileTime);
}
D
ldc2 -run ctfe.dThe new lines:
int square(int n) { return n * n; }is a plain function. Nothing marks it as special.enum int atCompileTime = square(12);declares a manifest constant: a value that must be known when the program is compiled. To fill it in, the compiler runssquare(12)itself. This is compile-time function evaluation, usually shortened to CTFE.static assert(atCompileTime == 144);is a check the compiler makes. If it is false, compilation stops and no program is produced.pragma(msg, ...)tells the compiler to print a message while it compiles.
Check it worked
You see two lines. square(12) worked out while compiling: 144 comes from the compiler, before the program exists. The program only prints the stored answer: 144 comes from the program. Change 144 in the static assert line to 145 and run ldc2 -run ctfe.d again: compilation fails with an error that mentions static assert and the line number, and nothing runs. Change it back before you continue.
Step 5 · Read a name and chain calls with UFCS
Write a program that asks for a name:
cat > greet.d <<'D'
import std.stdio;
import std.string;
void main()
{
write("What is your name? ");
stdout.flush();
string name = readln().strip;
if (name.length == 0)
name = "world";
writeln("Hello, ", name.capitalize, "!");
}
D
ldc2 greet.d
printf 'ada\n' | ./greetThe new parts:
import std.string;brings in text functions such asstripandcapitalize.write(...)prints the question without a newline, andstdout.flush();makes sure it appears before the program waits for you to type.readln()reads one line from the keyboard, including the newline at the end.stringis D's text type: an array of characters that cannot be changed once made..stripremoves spaces and the newline from both ends. Without it, the greeting would break across two lines.name.length == 0checks for an empty answer. Arrays in D carry their length, so there is no counting up to a terminating zero as in C.name.capitalizemakes the first letter upper case and the rest lower case.
Look at readln().strip and name.capitalize. strip and capitalize are ordinary functions that take a string, normally called as strip(readln()) and capitalize(name). D lets you write any function call f(x) as x.f, and drop the empty brackets. This is uniform function call syntax, or UFCS. It turns nested calls into a chain you read left to right. Try it on its own:
cat > ufcs.d <<'D'
import std.stdio;
import std.string;
import std.algorithm : filter, map;
import std.range : iota;
void main()
{
writeln(capitalize("ada"));
"ada".capitalize.writeln;
iota(1, 11).filter!(n => n % 2 == 0).map!(n => n * n).writeln;
}
D
ldc2 -run ufcs.dThe first two lines of main do exactly the same thing. The third counts from 1 to 10 with iota, keeps the even numbers with filter, squares them with map and prints the result. n => n * n is a short anonymous function, and the ! passes it to map as a compile-time argument, so the compiler can build a version of map specialised for it.
Check it worked
printf 'ada\n' | ./greet prints What is your name? Hello, Ada!. Run ./greet on its own, type a name in lower case and press Enter: the greeting uses a capital first letter. ufcs.d prints Ada twice, then [4, 16, 36, 64, 100].
Names with accents or in other scripts work too, because D strings are UTF-8 and capitalize follows Unicode rules: chidi becomes Chidi, and émile becomes Émile.
Step 6 · Start a project with dub
For anything bigger than one file, D projects use dub. It holds the project's name and dependencies, downloads packages from the dub registry at code.dlang.org, and calls the compiler for you. Create a project without dub's questions:
cd ~/hello-d-demo
dub init greeter -n
cd greeter
ls -a . source-n (non-interactive) accepts the defaults instead of asking for a description, author and licence. dub writes a recipe file, dub.json, holding the package name greeter; source/app.d, the program; and a .gitignore.
Replace the generated program with your greeter, then build and run the project:
cp ../greet.d source/app.d
printf 'kwame\n' | dub run --compiler=ldc2
lsdub run compiles every file under source/, links them into a program named after the package and runs it. --compiler=ldc2 makes the choice of compiler explicit; without it, dub uses the first D compiler it finds on your path. To use a library, run dub add <package-name> in the project folder, and the next dub build downloads and compiles it.
Check it worked
dub prints a few lines about the build (the package name, debug build, ldc2), then What is your name? Hello, Kwame!. ls now shows a greeter program in the project folder, which you can run again with ./greeter without rebuilding.
Step 7 · Recognise the errors beginners hit
Make two common mistakes on purpose so you know them when they happen by accident.
A missing import. Remove the import std.stdio; line:
cd ~/hello-d-demo
grep -v 'import std.stdio' hello.d > noimport.d
ldc2 noimport.dThe compiler reports that writeln is not defined, with the file name and line number in brackets, something like ` noimport.d(4): Error: `writeln is not defined, perhaps import std.stdio; is needed? `. Removing the import moved `writeln from line 5 to line 4. D only knows names from modules you import; for common standard library functions the compiler suggests the import you probably forgot. Older compilers, and names it has no suggestion for, give undefined identifier instead.
A missing semicolon. Remove the ; after writeln(...):
sed 's/);/)/' hello.d > nosemi.d
ldc2 nosemi.dThe error says something like found } when expecting ;. The line number may point to the line after the real mistake, where the compiler noticed it; if the line it names looks fine, check the line above.
Three more you will meet:
Error: failed to invoke the linkerorcc: not found. LDC needs a C linker. Runsudo apt install gcc libc6-dev.- A greeting split over two lines. You used
readln()without.strip, so the newline you typed is part of the name. - Options after the file with
-run.ldc2 -run hello.d -Opasses-Oto your program, not to the compiler. Put compiler options first:ldc2 -O -run hello.d.
Check it worked
You have seen an is not defined error and an expecting error in your own terminal, and neither command produced a program.
Step 8 · Clean up
Delete the demo folder:
cd ~
rm -rf ~/hello-d-demoKeep LDC if you plan to carry on. To remove it too, delete the folder ~/.local/opt/ldc2-1.43.0-linux-x86_64 (or aarch64), the export PATH line you added at the end of ~/.bashrc, and dub's package cache in ~/.dub.
Check it worked
ls ~/hello-d-demo reports No such file or directory.
Next steps
D is used where people want C-level speed with less ceremony: storage software (the Weka file system is written in D), command-line data tools such as eBay's tsv-utils, game tooling and engines, and high-performance services. The garbage collector is the default, not a requirement: functions marked @nogc are checked by the compiler never to allocate from it, and -betterC mode drops the runtime entirely for code that must link into C projects or run on small devices.
- Compare D with the language it set out to improve on: Hello World in C++.
- See what compiling and linking look like one stage at a time: Hello World in C.
- Try a newer systems language that has no garbage collector at all: Hello World in Zig.
Related tutorials
- Hello World in Zig: zig run, zig build and zig ccYou will install a signature-checked Zig 0.17.1 on Ubuntu 26.04, write and run Hello World with the current I/O API, build a small project with zig build that handles errors explicitly, and use zig cc to compile a C program for Linux and for Windows.
- Hello World in x86-64 assembly with NASM and Linux system callsYou will write an x86-64 assembly program that prints a line using only Linux system calls, assemble it with NASM, link it with ld, check its exit status, then loop it three times and read its machine code with objdump.
- Hello World in WebAssembly text format with wabt and WasmtimeYou will write a WebAssembly module by hand in the text format, assemble it with wat2wasm, print Hello World through WASI in Wasmtime 50, and call an exported add function from both Wasmtime and Node.js.
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