7 The easiest and recommended way to install the ``cabal`` command-line tool
8 on Linux, macOS, FreeBSD or Windows is through `ghcup <https://www.haskell.org/ghcup/>`__.
9 It installs the “Haskell toolchain”, which includes Cabal,
10 the Haskell compiler `GHC <https://www.haskell.org/ghc/>`__
11 and optionally other useful Haskell tools.
13 Creating a new application
14 --------------------------
16 We create a minimal Haskell application to get a quick overview
17 of the ``cabal`` command-line tool:
19 1. How to initialize a Haskell package.
20 2. How files are organized inside a package.
21 3. How to compile Haskell files and run a resulting executable.
22 4. How to manage external dependencies.
24 Initializing an application
25 ^^^^^^^^^^^^^^^^^^^^^^^^^^^
27 To initialize a new Haskell application, run
29 .. code-block:: console
31 $ cabal init myapp --non-interactive
33 in a terminal. This generates the following files in a new ``myapp`` directory:
35 .. code-block:: console
45 The ``myapp.cabal`` file is a package description file, commonly referred to as a “Cabal file”:
57 build-depends: base ^>=4.19.0.0
59 default-language: Haskell2010
61 It contains metadata (package name and version, author name, license, etc.) and sections
62 to define package components. Components can be used to split large codebases into smaller,
63 more managable building blocks.
64 A component can be of one of several types (executable, library, etc.) and describes,
65 among other things, the location of source files and its dependencies.
66 The ``myapp.cabal`` file above defines a single component named ``myapp`` of the executable type.
67 Inside the ``executable`` section, the ``build-depends`` field lists the dependencies of this component.
70 The ``app/Main.hs`` file is where your executable's code lives:
72 .. code-block:: haskell
77 main = putStrLn "Hello, Haskell!"
80 To run the executable, switch into the application directory with ``cd myapp`` and run
82 .. code-block:: console
88 This command automatically determines if the executable needs to be (re)built
89 before running the executable. With only one executable component in the package,
90 ``cabal run`` (without a component name) is smart enough to infer it, so the name can be omitted.
92 If you just want to build the executable without running it, run:
94 .. code-block:: console
97 Resolving dependencies...
99 Building executable 'myapp' for myapp-0.1.0.0..
100 [1 of 1] Compiling Main ( app/Main.hs, /home/.../myapp/dist-newstyle/build/.../myapp-tmp/Main.o )
101 Linking /home/.../myapp/dist-newstyle/build/.../myapp
107 Next we'll add an external dependency to our application. `Hackage
108 <https://hackage.haskell.org/>`__ is the Haskell community's central `package`
109 archive of open source software.
111 In our application, we'll use a package called `haskell-say
112 <https://hackage.haskell.org/package/haskell-say>`__ to print text to the
113 terminal with some embellishment.
116 If you installed ``cabal`` a while ago but haven't used it recently you may
117 need to update the package index, you can do this by running ``cabal
120 In our ``myapp.cabal`` file, we will update the ``build-depends`` field of
121 the executable section to include ``haskell-say``:
123 .. code-block:: cabal
130 haskell-say ^>=1.0.0.0
132 default-language: Haskell2010
136 ``^>=1.0.0.0`` means use version 1.0.0.0 of the library or any more recent
137 minor release with the same major version. To put it simply, this means
138 use the latest version of the library that starts with ``1.0``.
140 Next we'll update ``app/Main.hs`` to use the ``HaskellSay`` library:
142 .. code-block:: haskell
146 import HaskellSay (haskellSay)
149 main = haskellSay "Hello, Haskell!"
151 ``import HaskellSay (haskellSay)`` brings the ``haskellSay`` function from the
152 module named ``HaskellSay`` into scope. The ``HaskellSay`` module is defined in
153 the ``haskell-say`` package that we added as a dependency above.
155 Now you can build and re-run your code to see the new output:
157 .. code-block:: console
160 ________________________________________________________
163 \____ _____________________________________________/
171 \ \ \ \ \-----------|
181 Running a single-file Haskell script
182 ------------------------------------
184 Cabal also supports running single-file Haskell scripts like
185 the following file named ``myscript``:
187 .. code-block:: haskell
193 haskell-say ^>=1.0.0.0
196 import HaskellSay (haskellSay)
199 main = haskellSay "Hello, Haskell!"
201 The necessary sections of a ``.cabal`` file are placed
202 directly into the script as a comment.
204 Use the familiar ``cabal run`` command to execute this script:
206 .. code-block:: console
210 On Unix-like systems, a Haskell script starting with ``#!/usr/bin/env cabal``, like the one above,
211 can be run directly after setting the execute permission (+x):
213 .. code-block:: console
217 ________________________________________________________
220 \____ ____________________________________________/
223 See more in the documentation for :ref:`cabal run`.
228 Now that you know how to set up a simple Haskell package using Cabal, check out
229 some of the resources on the Haskell website's `documentation page
230 <https://www.haskell.org/documentation/>`__ or read more about packages and
231 Cabal on the :doc:`What Cabal does <cabal-context>` page.