Skip to content

Haskell Stack Guide — Stack Build Tool, Project Management, and Dependencies

DodaTech Updated 2026-06-28 2 min read

In this tutorial, you will learn about Haskell Stack Guide. We cover key concepts, practical examples, and best practices to help you master this topic.

Haskell Stack is a build tool that manages GHC installations, project dependencies via stack.yaml snapshots, and provides sandboxed, reproducible builds -- with commands like stack build, stack test, and stack exec for project lifecycle management.

Creating a Project

# Create new project from template
stack new my-project
cd my-project

# Project structure:
#   app/Main.hs        - executable entry point
#   src/MyLib.hs        - library modules
#   test/Spec.hs        - test suite
#   my-project.cabal    - package descriptor
#   stack.yaml          - Stack configuration
#   package.yaml        - hpack configuration

Building and Running

# Build project
stack build

# Run executable
stack run
stack exec my-project-exe

# Run with arguments
stack run -- --arg1 --arg2

# Build and run tests
stack test

# Run specific test
stack test --test-arguments="-m Pattern"

GHC Management

# List installed GHC versions
stack ghc -- --version

# Install specific GHC
stack setup 9.6.5
stack setup --resolver lts-22.0

# Use different resolver
stack --resolver nightly-2024-01 build

# Clean and rebuild
stack clean
stack build --force-dirty

Dependencies

# stack.yaml
resolver: lts-22.0
packages:
  - .
extra-deps:
  - megaparsec-9.5.0
  - lens-5.2.3
# package.yaml
dependencies:
  - base >= 4.7 && < 5
  - text
  - containers
  - mtl

Common Mistakes

1. Committing .stack-work

Add .stack-work to .gitignore. It contains local build artifacts and GHC installations.

2. Mismatched resolver and dependencies

Not all packages work with every snapshot. Check Stackage for compatible versions.

3. Forgetting to update stack.yaml

When adding dependencies, update both package.yaml (or .cabal) and stack.yaml if using extra-deps.

Practice Questions

1. How do you create a new Stack project? stack new project-name creates from the default template. Use --template for custom templates.

2. How do you add a dependency? Add to package.yaml under dependencies, and to stack.yaml extra-deps if not in the snapshot.

3. What is a resolver in Stack? A curated set of package versions guaranteed to work together. LTS (Long Term Support) and nightly are available.

FAQ

{{< faq question="What is the difference between Stack and Cabal?" >}} Stack manages GHC versions and provides curated snapshots for reproducibility. Cabal is more flexible but requires manual dependency resolution. {{< /faq >}}

{{< faq question="How do I upgrade Stack?" >}} stack upgrade downloads the latest Stack binary. which stack shows the current version. {{< /faq >}}

{{< faq question="What is hpack?" >}} A tool that generates .cabal files from package.yaml. Newer Stack projects default to hpack for cleaner configuration. {{< /faq >}}

What's Next

Now learn about Cabal build tool.

Topic Description Link
Cabal Cabal build tool {{< ref "26-cabal" >}}
GHCi Interactive GHC {{< ref "27-ghci" >}}

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro