Install GHC and cabal
What you install
Two programs are enough to follow this course, and the first one carries the second half of the toolchain inside it:
- ghc is the compiler. It also brings ghci, the interactive shell, and runghc, which interprets and runs a file in one step.
- cabal is the build tool and the package manager. It reads a project description, resolves dependencies and calls ghc with the right flags.
A fresh install is checked with two commands, and both print a version:
ghc --version
cabal --version
The Glorious Glasgow Haskell Compilation System, version 9.10.3
cabal-install version 3.16.0.0
compiled using version 3.16.0.0 of the Cabal library (in-tree)
Those three lines are the real output of the machine this page was written on, an Alpine image with ghc-9.10.3-r2. The rest of the page explains how that install got there, and how to get the same thing on your own machine.
The package per system
Every distribution packages GHC, and the package name is almost always ghc with cabal-install next to it. These are the commands to run; none of them was run for this page, because the compiler was already in the image.
- Debian and Ubuntu: sudo apt install ghc cabal-install
- Fedora: sudo dnf install ghc cabal-install
- Alpine, the system of this container: apk add ghc cabal-install. The install in the image is this one, and the package list proves it:
apk list -I ghc
ghc-9.10.3-r2 x86_64 {ghc} (BSD-3-Clause) [installed]
On macOS the same two names come from Homebrew:
brew install ghc cabal-install. On Windows the
shortest path is winget install --id Haskell.GHC, or
the ghcup installer of the next chapter, which handles the PATH entry for
you.
A distribution version can lag a year or two behind the newest GHC. That is
fine for learning: every program in this course is ordinary Haskell 2010
and compiles on any GHC from 9.2 upwards. The lessons name the version they
were tested with, 9.10.3, and a warning text may differ in an older one.
ghcup, in words
ghcup is not a compiler and not a build tool: it is a
small program that downloads and switches between several versions of both.
Worth it when you want 9.10 next to 9.6, or when a project pins a version.
The install is one shell script, which asks what to install and where to put
it:
curl --proto '=https' --tlsv1.2 -sSf https://get-ghcup.haskell.org | sh
It writes everything under ~/.ghcup and offers to add
one line to your shell profile, so that ~/.ghcup/bin
comes first in PATH. That order matters: the first
ghc in PATH is the one that
runs, and a second copy from the system package is the usual reason two
terminals disagree about the version.
After that the commands are self-explanatory:
ghcup install ghc 9.10.3
ghcup set ghc 9.10.3
ghcup install cabal recommended
ghcup list
ghcup list prints a table with a column of installed
versions and a marker on the active one; ghcup set
changes the marker. The version this course uses, 9.10.3, is the one to
install if you have no reason for another.
Pending run: ghcup is not installed in this container, and the image has
no network, so the four commands above have no output on this page. The
two commands of the next chapter do have one.
Check the install
Version numbers answer the first question. The second is where the libraries are, and ghc --print-libdir prints the folder that holds the standard library and the package database:
ghc --print-libdir
ghc-pkg field base version
/usr/lib/ghc-9.10.3/lib
version: 4.20.2.0
base is the package that holds Prelude and everything a program uses without an import. Its version moves with the compiler, not with the release: GHC 9.10 ships base-4.20. The packages that are installed next to it are the boot libraries, and ghc-pkg list prints all of them:
ghc-pkg list --simple-output | tr ' ' '\n' | wc -l
ghc-pkg list --simple-output | tr ' ' '\n' | sort | head -12
42
Cabal-3.12.1.0
Cabal-syntax-3.12.1.0
array-0.5.8.0
base-4.20.2.0
binary-0.8.9.3
bytestring-0.12.2.0
containers-0.7
deepseq-1.5.0.0
directory-1.3.8.5
exceptions-0.10.9
filepath-1.5.4.0
ghc-9.10.3
Forty-two packages is what a bare GHC gives you: text, bytestring, containers, stm, parsec, time, process and the rest. A program that imports one of them needs nothing extra; a program that imports QuickCheck or aeson needs a package the compiler does not ship, and that is where cabal comes in.
The version inside a program
A program can ask the compiler what it was built with, which is exactly what a bug report wants at the top. System.Info holds four strings: the compiler name, its version, the OS and the architecture.
module Main (main) where
import Data.Version (showVersion)
import System.Info (arch, compilerName, compilerVersion, os)
main :: IO ()
main = do
putStrLn ("compiler: " ++ compilerName ++ " " ++ showVersion compilerVersion)
putStrLn ("platform: " ++ os ++ "/" ++ arch)
ghc -Wall -O1 -o info Info.hs
./info
[1 of 2] Compiling Main ( Info.hs, Info.o )
[2 of 2] Linking info
compiler: ghc 9.10
platform: linux/x86_64
Two lines, and the second one is the reason a compiled language feels
different: the binary knows the machine it was built for, because it was
translated into the instructions of that machine.
The first line says 9.10 and not
9.10.3, which looks like a mistake and is not.
compilerVersion is a structured value of major,
minor and patch numbers, and showVersion prints only
the components that are not zero. The patch level, 3, is dropped. The
ghc --version output of the first chapter prints
the full string. Both describe the same compiler.
Exercises
- Run ghc --version and cabal --version on your machine and write both lines down. If a tutorial you follow was written for another version, this is the number that explains a difference later.
- Run which -a ghc. Two paths mean two installs, and the first one is the one that runs.
- Add putStrLn ("base: " ++ show (5 :: Int)) to Info.hs and rebuild. Which part of the page told you that show would be needed?
- Remove the Data.Version import and print compilerVersion directly. Read the error, then put the import back.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
Which two programs are enough to start writing and building Haskell code?
Answer
ghc and cabal
ghc compiles, and cabal drives ghc for a project; ghci and runghc come inside ghc, and stack is a separate tool this course does not need.
-
What does ghcup do?
Answer
It downloads and switches between versions of GHC and cabal
ghcup installs several GHC and cabal versions under ~/.ghcup and sets which one is active; it neither compiles nor builds.
-
Why does the order of the entries in PATH matter after a ghcup install?
Answer
Because the first ghc in PATH is the one that runs
The shell runs the first matching program in PATH, so ~/.ghcup/bin has to come before a system copy of ghc.
-
What does this command print?
ghc --print-libdirAnswer
The folder that holds the standard library and the package database
ghc --print-libdir prints the library directory, /usr/lib/ghc-9.10.3/lib here; ghc-pkg lists packages and ghc --version prints the version.
-
A program built on this machine prints this line. Which part of it came from System.Info.os and arch?
platform: linux/x86_64Answer
linux is os, x86_64 is arch
os is the operating system name and arch the processor architecture; Data.Version only formats the compiler version.
-
Which module holds compilerName, compilerVersion, os and arch?
Answer
System.Info
System.Info holds the four strings a program can ask for; Data.Version only has showVersion, which formats the version value.
-
What does this program print?
module Main (main) where main :: IO () main = print (length ["base", "text"])Answer
2
length counts the elements of a list, and the list has two strings in it, whatever their lengths.
-
What does this program print?
module Main (main) where main :: IO () main = putStrLn (show (42 :: Int))Answer
42
show turns the number into the string "42", and putStrLn prints that string without its quotes.
-
A package such as QuickCheck is missing after a bare GHC install. What is the reason?
Answer
GHC ships only its boot libraries, and QuickCheck is not one of them
GHC ships base, containers, text and the other boot libraries; anything else is a package that a project adds and cabal fetches.
-
What is the first line of the compiler output for this program?
module Main (main) where import System.Info (compilerName) main :: IO () main = putStrLn (compilerVersion)Answer
error: [GHC-88464]
compilerVersion is used but not imported, so GHC reports that the variable is not in scope, which is GHC-88464; the unused compilerName is only a warning.
The code on this page was built with GHC 9.10.3 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev