Skip to content
Tutorials Center
My learning

Topics

Lesson 2 of 57 ยท Getting started

Installing GHC and Using GHCi

Install the Haskell toolchain with GHCup, then learn the GHCi commands :t, :i, :r and :l, how to use runghc, and what Cabal and Stack are for.

To run Haskell on your own machine you need a compiler and, ideally, a build tool. The compiler everyone uses is GHC, the Glasgow Haskell Compiler. It comes with an interactive shell called GHCi, which is the best place to experiment while you learn.

This lesson shows how to install the toolchain, how to use the handful of GHCi commands you will type every day, and how GHC, Cabal and Stack fit together. Everything here is read-only: the commands run in a terminal on your computer, not on this site. A short JavaScript stand-in at the end imitates a GHCi session so you can feel the workflow.

Installing with GHCup

The recommended installer is GHCup, a tool that installs and switches between versions of GHC, Cabal, Stack and the Haskell Language Server. Follow the one-line instructions on the official GHCup page for your system (Linux, macOS, Windows). The installer asks which tools you want; the defaults are fine for this course.

When it finishes, open a new terminal and check:

Terminal
ghc --version
ghci --version
cabal --version
ghcup list

ghcup list shows the installed and available versions. ghcup tui opens a menu where you can pick a different GHC version. Because GHCup keeps versions side by side, you can try a newer compiler without breaking old projects.

Use the "recommended" GHC marked in ghcup list. It is the version most libraries are tested against.

What each tool does

The Haskell toolbox

GHC The compiler. Turns .hs files into programs.
GHCi The interactive shell for trying code.
Cabal Builds projects and downloads packages.
Stack An alternative build tool with curated package sets.

You need GHC and GHCi now. Cabal and Stack come later.

1 / 2

Starting GHCi

Type ghci in a terminal. You get a prompt (it may read ghci> or Prelude>) where you can type expressions and see results immediately:

HASKELL
ghci> 2 + 3
5
ghci> "Hello, " ++ "world"
"Hello, world"
ghci> let x = 10
ghci> x * x
100
ghci> length [1, 2, 3, 4]
4

Every line is an expression that GHCi evaluates and prints. A let creates a name for the session. To leave, type :quit or press Ctrl+D.

The commands you need

Commands that control GHCi itself start with a colon. Most have a short form:

Command Short What it does
:type expr :t Show the type of an expression
:info name :i Show information about a function, type or class
:load File.hs :l Load a source file
:reload :r Reload the current file after you edit it
:browse Module List what a module exports
:help :? List all commands
:quit :q Leave GHCi

Asking for types

:t is the command you will use most. It teaches you Haskell by showing how the compiler sees your code:

HASKELL
ghci> :t True
True :: Bool
ghci> :t 'a'
'a' :: Char
ghci> :t "hi"
"hi" :: String
ghci> :t not
not :: Bool -> Bool
ghci> :t length
length :: Foldable t => t a -> Int

The last line is a preview of type classes: length works on any container t that is Foldable, such as a list. Do not worry about it yet; type classes explain it.

Loading your own file

Create a file Greet.hs:

HASKELL
greet :: String -> String
greet name = "Hello, " ++ name ++ "!"

shout :: String -> String
shout s = s ++ "!!!"

Then, in the same folder, start GHCi and load it:

HASKELL
ghci> :l Greet.hs
[1 of 1] Compiling Main             ( Greet.hs, interpreted )
Ok, one module loaded.
ghci> greet "Ada"
"Hello, Ada!"

You can also start with ghci Greet.hs. After editing the file, type :r to reload it. This edit, reload, test loop is the heart of learning Haskell. If the file has a type error, GHCi prints it and keeps the old definitions until you fix the file.

Getting information

:i tells you about a name, including where it is defined and its instances:

HASKELL
ghci> :i Bool
data Bool = False | True
  	-- Defined in 'GHC.Types'
instance Eq Bool
instance Ord Bool
instance Show Bool
-- (more instances follow)

The exact list of lines depends on your GHC version, so do not expect to match it character for character.

Running a file without compiling

runghc interprets a file and runs its main:

Terminal
runghc Hello.hs

To produce a native executable, call the compiler:

Terminal
ghc Hello.hs
./Hello          # on Windows: Hello.exe

GHC writes .o and .hi files next to your source; they are build leftovers that you can delete. Add -Wall (ghc -Wall Hello.hs) to turn on helpful warnings. We write a complete program in the next lesson.

Cabal and Stack in one minute

Once a program uses packages from the internet, or has more than a couple of files, you move to a project tool:

  • Cabal reads a .cabal file that lists your modules and dependencies. cabal init makes a new project, cabal build compiles it, cabal run runs it and cabal repl opens GHCi with your project loaded.
  • Stack reads stack.yaml and uses curated package sets called snapshots, so a project builds the same way every time. stack new, stack build, stack run and stack ghci mirror the Cabal commands.

Both download packages from Hackage, the central Haskell package archive. You can use either one; this course uses plain GHC and GHCi until Cabal, Stack and packages.

A stand-in for a GHCi session

This is not GHCi. It is a tiny JavaScript model of the loop you just read about: a file defines functions, and a "session" evaluates expressions using them. It helps you see the workflow without installing anything.

JS A tiny stand-in session

Press Run (or Ctrl + Enter) to see the console output here.

No output.

JavaScript has no static types, so the stand-in stores the type text by hand. Real GHCi works out the types itself.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which tool installs and switches between versions of GHC?

    GHCup is the toolchain installer and version manager. Hackage is the package archive.
  2. 2. What does the GHCi command :t not show?

    :t (short for :type) prints the type of an expression.
  3. 3. You edited Greet.hs while GHCi is open. What do you type to pick up the changes?

    :r (:reload) recompiles the currently loaded files.
  4. 4. What is the difference between runghc and ghc?

    runghc Hello.hs runs the program straight away, while ghc Hello.hs produces an executable you start yourself.

Summary

  • Install the toolchain with GHCup; it gives you GHC, GHCi, Cabal and Stack.
  • GHCi evaluates expressions interactively. Use :t for types, :i for info, :l to load a file and :r to reload it.
  • runghc runs a file directly; ghc compiles it to an executable.
  • Cabal and Stack manage projects and packages from Hackage.

Next: Your First Haskell Program.