Salta ai contenuti

Stack

Installation

Linux

# Using curl (recommended)
curl -sSL https://get.haskellstack.org/ | sh

# Using apt (Ubuntu/Debian)
sudo apt update
sudo apt install haskell-stack

# Using dnf (Fedora)
sudo dnf install haskell-stack

# Using pacman (Arch Linux)
sudo pacman -S stack

# Verify installation
stack --version

macOS

# Using Homebrew
brew install haskell-stack

# Using curl
curl -sSL https://get.haskellstack.org/ | sh

# Verify installation
stack --version

Windows

# Using Chocolatey
choco install haskell-stack

# Using Scoop
scoop install stack

# Manual download
# Visit https://docs.haskellstack.org/en/stable/README/
# Download Windows installer and run

# Verify installation
stack --version

Update Stack

# Update to latest version
stack upgrade

# Update stack with specific version
stack upgrade --ghc-version 9.4.4

Project Setup

Creating New Projects

# Create new Haskell project with stack
stack new my-project

# Create with specific template
stack new my-project simple
stack new my-project yesod
stack new my-project scotty

# List available templates
stack templates

# Change to project directory
cd my-project

Project Structure

# Typical Stack project layout:
my-project/
├── stack.yaml              # Stack configuration
├── package.yaml            # Cabal package description
├── src/
   └── Lib.hs             # Main library module
├── app/
   └── Main.hs            # Executable entry point
├── test/
   └── Spec.hs            # Test specifications
└── Setup.hs               # Cabal setup

Building and Running

Build Commands

# Build the entire project
stack build

# Build with verbose output
stack build --verbose

# Build a specific component
stack build my-project:lib
stack build my-project:exe:my-exe

# Clean and rebuild
stack clean
stack build

# Build documentation
stack build --haddock
stack haddock

# Fast build (skip optimization)
stack build --fast

Running Executables

# Run compiled executable
stack exec my-exe

# Run with arguments
stack exec my-exe -- --arg1 value1 --arg2 value2

# Run from outside project directory
stack exec --cwd /path/to/project my-exe

# Build and run in one command
stack build && stack exec my-exe

REPL and Evaluation

# Open GHCi REPL
stack ghci

# Load specific module in REPL
stack ghci my-project:lib

# Evaluate expression without REPL
stack exec ghc -- -e "1 + 2"

# Load file in REPL
stack ghci
:load src/Lib.hs

Dependency Management

Managing Package Dependencies

# View project dependencies
stack list-dependencies

# Show dependency tree
stack list-dependencies --tree

# Add dependency to package.yaml
# Edit package.yaml and add to dependencies:
# - text
# - aeson

# Or use stack to add dependency
stack build --add-package text

# Update dependencies
stack update

# Freeze lock file (reproducible builds)
stack freeze

# Use specific resolver/GHC version
# Edit stack.yaml:
# resolver: lts-20.0
# compiler: ghc-9.2.2

Version Control

# Show installed GHC version
stack ghc --version

# Show Stack version
stack --version

# List all installed GHC versions
stack ls snapshots

# Install specific GHC version
stack setup ghc-9.4.4

# Switch to different resolver
# Edit stack.yaml: resolver: lts-21.0
stack build

Testing

Running Tests

# Run all tests
stack test

# Run specific test suite
stack test my-project:test

# Run with verbose output
stack test --verbose

# Run with flags
stack test -- --match "pattern"

# Generate coverage report
stack test --coverage

# View coverage
stack hpc report my-project

# Run specific test file
stack test my-project:my-suite -- --match "TestName"

Testing Patterns

# Example test in test/Spec.hs:
main = hspec $ do
  describe "Addition" $ do
    it "adds two numbers" $ do
      (1 + 2) `shouldBe` 3
    it "handles negative numbers" $ do
      (-1 + 1) `shouldBe` 0

# Run tests
stack test

# Run specific test
stack test -- --match "handles negative"

Advanced Features

Stack Configuration (stack.yaml)

# Stack configuration file
resolver: lts-20.0

packages:
- .

ghc-options:
  "$everything": -Wall -O2

extra-deps:
- package-1.2.3
- package-2.0.0@sha256:hash

flags:
  package-name:
    flag-name: true

allow-different-user: true
require-stack-version: ">=2.9"

Profiling

# Build with profiling support
stack build --profile

# Run with profiling
stack exec --profile my-exe -- +RTS -p

# View profiling report
cat my-exe.prof

# Build and profile
stack build --profile
stack exec my-exe -- +RTS -p -RTS

Benchmarking

# Create benchmark in bench/Main.hs
# Use criterion library

# Run benchmarks
stack bench

# Run specific benchmark
stack bench my-project:bench

# Show benchmark results
# Results saved to .benchmarks/

Common Commands

CommandDescription
stack new <project>Create new project
stack setupInstall appropriate GHC
stack buildBuild project and dependencies
stack exec <prog>Run compiled program
stack ghciOpen interactive REPL
stack testRun test suite
stack benchRun benchmarks
stack cleanDelete build artifacts
stack updateUpdate package list
stack uploadUpload to Hackage
stack --versionShow Stack version
stack --helpShow help information

Best Practices

  • Always include stack.yaml in version control for reproducibility
  • Use LTS resolvers for stable, well-tested GHC versions
  • Keep dependencies minimal and well-documented
  • Use type signatures for all top-level definitions
  • Write unit tests alongside code (test/Spec.hs)
  • Use haddock comments for public APIs (— |)
  • Enable compiler warnings (-Wall) in development
  • Use hlint for code quality checks: stack exec hlint -- src/
  • Pin critical dependencies to specific versions
  • Update dependencies regularly but test thoroughly

Resources


Last updated: 2026-03-30