Dependencies

Add a dependency

Hackage hosts the packages. Adding one means one line in build-depends, and Cabal resolves the rest:

build-depends: base >=4.20 && <5 , text >=2.0 && <3 , containers >=0.6 && <0.8 , aeson >=2.2 && <3

text gives packed strings, containers gives maps and sets, and aeson reads and writes JSON. Those four plus base cover most command line tools.

Update the index

Cabal keeps a local copy of the package index. When a version you need is missing, refresh it:

cabal update

Downloading the latest package list from hackage.haskell.org Package list of hackage.haskell.org has been updated. The index-state is set to 2026-11-10T20:58:27Z.

Then build. The solver prints the plan before it builds; add --dry-run to see the plan without compiling anything. With aeson the plan is long, and the first real build compiles every dependency, which can take several minutes:

cabal build --dry-run

Build profile: -w ghc-9.10.3 -O1 In order, the following would be built (use -v for more details): - OneTuple-0.4.3 (lib) (requires build) - StateVar-1.2.2 (lib) (requires build) ... - todo-0.1.0.0 (exe:todo)

Version bounds

A bound says which versions your code was tested with. Too tight and the project blocks every update; too loose and a breaking release compiles by accident:

-- reasonable: one major version window text >=2.0 && <3 -- too loose: any version, including the next rewrite text

The convention is a lower bound on the version you actually used and an exclusive upper bound on the next major release. The cabal check command flags missing bounds before you upload a package.

Freeze and lock

A build that worked today should work next year. Freezing records the exact versions in a plan file, and a project file pins the compiler and index state:

cabal freeze

Wrote freeze file: /home/ada/todo/cabal.project.freeze

Commit cabal.project.freeze for an application and leave it out for a library. Applications want reproducible builds, libraries want to be testable against new dependency versions.

The code on this page was run with GHC 9.10.3 and cabal-install 3.16.0.0 on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev