Intro to DUB
DUB is the official package manager for the D programming language, providing simple and configurable cross-platform builds. DUB is well integrated in various IDEs and can also generate configuration for third party build systems and IDEs.
Use the DUB registry website to discover packages and publish your own.
The CLI can be used to
- download programs and dependencies (dub fetch, dub upgrade)
- create projects (dub init, dub add)
- compile projects and external programs (dub build, dub run)
- test projects (dub test)
DUB is bundled with most D compilers' distributions. However, it's also possible to install it separately. See Installing DUB for details.
DUB Basics
DUB is a build tool similar to other modern languages build tools like Javascript's npm and Rust's cargo.
A recipe file called dub.sdl
(or dub.json
) is used to configure a DUB project.
SDL is a "Simple Declarative Language" that uses a familiar C syntax. Whether to use SDL or JSON for the DUB file is a matter of taste.
User/System-wide default settings can be specified in a settings file.
A recipe file may look like this:
name "myproject"
description "A minimal D application."
authors "My Name"
copyright "Copyright © 2024, My Name"
license "Boost"
dependency "libasync" version="~>0.9.5"
configuration "library" {
targetPath "target/lib"
}
configuration "unittest" {
dependency "tested" version="~>0.9.5"
dependency "dshould" version="~>1.7.1"
targetPath "target/test"
}
DUB Configurations are used to create different variations of a project.
In the above example, all configurations include a dependency on libasync
because that's declared at the top-level,
but only the unittest
configuration includes the dependencies tested
and dshould
.
To build a project, run dub build
(or just dub
).
As a project is built, DUB automatically resolves, downloads and builds its dependencies as needed.
The resolved dependency versions are stored in a file next to the recipe file, called dub.selections.json, which is similar to a lock file.
When running dub test
, all Unit Tests found in
sourcePaths are executed using the unittest
configuration by default.
To specify a configuration explicitly when building, use the --config
option:
DUB also uses the concept of build types to define what to build. Many build types are
pre-defined, but the most common ones are debug
and release
.
Hence, to build the release version of a library, the following command could be used:
Finally, to run the application, use dub run
.
Check Building for more details.
Next Steps
First Steps completes this overview of the basic DUB workflow.
The DUB Guide goes into more details about building, testing, configuring dependencies and registries, shared libraries, publishing packages and more.
More experienced users can use the DUB Reference and CLI Reference for a comprehensive list of the available options.
Created: September 1, 2023