Skip to main content

Hello World (first programs) · Beginner · 16 min

Hello World in Haskell: GHCup, ghci and IO

By Padding · Updated 18 Nov 2026
Debian 12 (haskell:9.14 image) · GHC 9.14.1 · cabal 3.16

You will install GHC 9.14 with GHCup, try expressions in ghci, run and compile Hello World, and write a program that keeps a pure greet function apart from the IO that reads your name.

On this page

Haskell is a functional language with three ideas that most languages do not combine. It is pure: an ordinary function cannot print, read files or change a variable; given the same input it always returns the same output. It is lazy: nothing is worked out until something needs the result, so you can describe an endless list and take only what you use. And it has type inference: every value has a fixed type that the compiler checks, but you rarely have to write the types yourself, because the compiler works them out.

Purity raises an obvious question: if functions cannot print, how does Hello World print anything? The answer is the IO type. A value of type IO () is a description of an action, such as "write this line to the screen". Your program's main is one such action, and running the program carries it out. The type tells you, and the compiler, exactly which parts of a program talk to the outside world. You will see that split in the last program of this tutorial.

The compiler is GHC (the Glasgow Haskell Compiler). It comes with ghci, an interactive prompt, and runghc, which runs a file without leaving a compiled program behind. cabal builds projects and downloads libraries from Hackage, the Haskell package archive. GHCup installs and switches between versions of all of them. This tutorial was written for GHC 9.14.1 and cabal 3.16.

Step 1 · Install GHCup and GHC 9.14

GHC calls a C toolchain to link programs and needs a few system libraries, including GMP for its big-number arithmetic. Install them first:

Bash
sudo apt update
sudo apt install -y build-essential curl libffi-dev libgmp-dev libncurses-dev

Now run the official GHCup installer as your normal user, not with sudo:

Bash
curl --proto '=https' --tlsv1.2 -sSf https://get-ghcup.haskell.org | sh

The script asks a few questions. Press Enter to accept the defaults, and answer yes when it offers to add GHCup to your shell's startup file. Everything goes into ~/.ghcup in your home folder, so you never need sudo for Haskell itself. When it finishes, open a new terminal, or load the path into this one:

Bash
source ~/.ghcup/env

GHCup installs the version of GHC it currently recommends, which can be an older, well-tested series. Install GHC 9.14.1 and make it the default, then make sure cabal is current:

Bash
ghcup install ghc 9.14.1
ghcup set ghc 9.14.1
ghcup install cabal latest
ghcup list

ghcup list shows every tool and version GHCup knows about, with a tick against installed ones and a double tick against the one in use. If you prefer menus, ghcup tui opens a full-screen version of the same list.

GHC is a large download, hundreds of megabytes once unpacked. On metered or prepaid data, run the install on Wi-Fi if you can, and avoid installing several GHC versions you do not need: each one is a separate download.

On macOS, run the same curl command in Terminal; it asks you to install Apple's command line tools if they are missing. On Windows, GHCup has a PowerShell installer on haskell.org/ghcup that also installs MSYS2, a small Unix-like environment GHC uses for linking. Run it in PowerShell as a normal user, not as Administrator.

Check it worked

ghcup list shows ghc 9.14.1 with a double tick, meaning it is installed and set as the default, and cabal with a tick. In a new terminal, which ghc prints a path inside ~/.ghcup/bin.

Troubleshooting

ghc: command not found after installing means the shell has not loaded GHCup's path. Run source ~/.ghcup/env or open a new terminal. A link error mentioning -lgmp means libgmp-dev is missing; install it with the apt command above.

Step 2 · Check the tools

Make a throwaway folder for this tutorial and ask each tool for its version:

Bash
mkdir -p ~/haskell-demo
cd ~/haskell-demo
ghc --version
ghc --numeric-version
cabal --version

Check it worked

ghc --version prints The Glorious Glasgow Haskell Compilation System, version 9.14.1, and ghc --numeric-version prints only 9.14.1. cabal --version prints a version starting cabal-install version 3.

Step 3 · Try expressions in ghci

Start the interactive prompt:

Bash
ghci

It prints a version banner and then a ghci> prompt. Type each line below and press Enter. Lines starting with : are commands to ghci itself, not Haskell code:

Text
2 + 3 * 4
length "Hello"
take 5 [1..]
:type not
:type putStrLn
double x = x * 2
double 21
:type double
:quit

Here is what each one shows you:

  • 2 + 3 * 4 prints 14. Normal arithmetic rules apply: multiplication before addition.
  • length "Hello" prints 5. Calling a function needs no brackets or commas: write the function name, a space, then its argument.
  • take 5 [1..] prints [1,2,3,4,5]. [1..] is the list of every whole number from 1 upwards, which never ends. In most languages building it would run until memory ran out. Because Haskell is lazy, it only works out the five numbers take asks for. This is laziness in one line.
  • :type not prints not :: Bool -> Bool. Read :: as "has type". not takes a Bool (True or False) and returns a Bool.
  • :type putStrLn prints putStrLn :: String -> IO (). It takes a String and returns an IO (): an action that, when run, does something in the outside world and gives back (), pronounced "unit", which means "nothing interesting". This is the type that makes printing possible.
  • double x = x * 2 defines a function called double with one argument, x. You wrote no types at all.
  • double 21 prints 42.
  • :type double prints double :: Num a => a -> a. GHC worked out the type itself: double works for any type a that is a number (Num a =>), takes one and returns one of the same type. That is type inference.
  • :quit leaves ghci. Ctrl+D does the same.

Check it worked

Your ghci session showed 14, 5, [1,2,3,4,5], 42, and the three types listed above. If you typed [1..] on its own by mistake, ghci starts printing numbers forever; press Ctrl+C to stop it.

Step 4 · Write Hello World and run it with runghc

Create hello.hs in ~/haskell-demo. .hs is the extension for Haskell source files:

Bash
cd ~/haskell-demo
cat > hello.hs <<'HS'
main :: IO ()
main = putStrLn "Hello, World!"
HS
runghc hello.hs

The program is two lines:

  • main :: IO () is the type signature. It says main has type IO (): an action that interacts with the outside world and returns nothing useful. Every Haskell program starts by running the action called main. GHC could infer this line, but writing signatures for top-level definitions is the normal style, and they make error messages clearer.
  • main = putStrLn "Hello, World!" defines main. The = means "is defined as", not "assign": main is the action you get by giving putStrLn the text "Hello, World!". putStrLn prints the text followed by a newline.

There is no return, no semicolon and no curly braces. A Haskell file is a list of definitions, and main is the one the program runs.

runghc compiles the file in memory, runs it and leaves nothing behind. It is handy for scripts and quick experiments.

Check it worked

runghc hello.hs prints Hello, World!, and ls shows only hello.hs in the folder: runghc created no other files.

Step 5 · Compile it with ghc

Now compile the same file into a native program:

Bash
ghc hello.hs
ls
./hello

GHC prints something like [1 of 2] Compiling Main ( hello.hs, hello.o ) and [2 of 2] Linking hello. It leaves three new files:

  • hello is the finished program. It runs on its own: copy it to another Linux machine with the same processor type and it works there with no GHC installed, because the Haskell libraries it uses are built into it.
  • hello.o is the compiled machine code for the Main module, before linking.
  • hello.hi is an interface file, a summary of what the module defines. GHC reads it when other modules import this one, so it can skip recompiling unchanged files.

Run ghc hello.hs a second time and it skips the compile step, because nothing changed. For programs where speed matters, add -O2 to turn on GHC's optimiser; it compiles more slowly and the program runs faster.

Check it worked

ls shows hello, hello.hi, hello.hs and hello.o, and ./hello prints Hello, World!.

Step 6 · Read a name and keep IO apart from pure code

This version asks for your name and greets you. The greeting is built by a pure function; only main touches the keyboard and the screen. Create greet.hs:

Bash
cat > greet.hs <<'HS'
greet :: String -> String
greet "" = "Hello, whoever you are!"
greet name = "Hello, " ++ name ++ "!"

main :: IO ()
main = do
  putStrLn "What is your name?"
  name <- getLine
  putStrLn (greet name)
HS
echo Amara | runghc greet.hs
echo "" | runghc greet.hs

Line by line:

  • greet :: String -> String says greet takes a String and returns a String. There is no IO in its type, so the compiler guarantees it cannot print, read input or do anything except compute text.
  • greet "" = ... and greet name = ... are two equations for one function. This is pattern matching: Haskell tries them from the top. If the argument is the empty string, the first equation applies; otherwise name matches anything and the second one applies.
  • ++ joins two strings (in Haskell, a String is a list of characters, and ++ joins lists).
  • main = do starts a do block, a way to write a sequence of IO actions one per line. The lines inside must be indented by the same amount; indentation is part of Haskell's syntax.
  • putStrLn "What is your name?" prints the question.
  • name <- getLine runs getLine, which reads one line from the keyboard, and gives the result the name name. getLine has type IO String: an action that, when run, produces a String. The arrow <- is how you get the String out of the action, and you can only use it inside a do block of IO actions.
  • putStrLn (greet name) calls the pure function and prints its result. The brackets matter: without them Haskell would read putStrLn greet name as giving putStrLn two arguments.

The echo ... | lines type the answer for you. Run runghc greet.hs on its own to type a name yourself.

Now see why the IO type matters. Try to skip the <- and give getLine straight to greet:

Bash
cat > broken.hs <<'HS'
greet :: String -> String
greet name = "Hello, " ++ name ++ "!"

main :: IO ()
main = putStrLn (greet getLine)
HS
ghc broken.hs 2> errors.txt || echo "Build failed, as expected"
cat errors.txt

GHC refuses to compile it. The message points at broken.hs:5: and says, in words like Couldn't match type, that greet expects a String but getLine is an IO String. An action that will produce a string is not a string, and the compiler will not let you mix them up. The message also carries an error code in square brackets, such as [GHC-83865]; errors.haskell.org explains each code with examples.

Check it worked

The first run prints What is your name? and then Hello, Amara!. The second, with an empty line, prints Hello, whoever you are!. The broken build prints Build failed, as expected, and errors.txt mentions both String and IO String.

Troubleshooting

If a name with accents or non-Latin letters, such as Adébáyọ̀ or Ọlá, fails with invalid byte sequence, your terminal's locale is not UTF-8. GHC reads input using the locale's encoding. Run locale; if it shows POSIX or C, set export LANG=C.UTF-8 and try again.

Step 7 · Make a cabal project

Single files are fine for learning. Real programs use a cabal project, which records the program's name, its source folder and the libraries it depends on. Create one in a new folder:

Bash
cd ~/haskell-demo
mkdir greeter
cd greeter
cabal init --non-interactive --exe
ls

cabal init names the package after the folder, greeter. It writes greeter.cabal, the project description, and app/Main.hs, a starter program. Open greeter.cabal and find the executable greeter section: main-is: Main.hs names the entry file, hs-source-dirs: app says where it lives, and build-depends lists libraries, only base (the standard library) for now.

Replace the starter program with your greeter and run it:

Bash
cat > app/Main.hs <<'HS'
module Main where

greet :: String -> String
greet "" = "Hello, whoever you are!"
greet name = "Hello, " ++ name ++ "!"

main :: IO ()
main = do
  putStrLn "What is your name?"
  name <- getLine
  putStrLn (greet name)
HS
echo Kofi | cabal run greeter

module Main where names the module; GHC requires the program's entry module to be Main. cabal run compiles the project into a dist-newstyle folder and runs it. If you have never run cabal update, cabal may print a warning that the package list for Hackage does not exist. That is fine here, because the project only needs base, which came with GHC. Run cabal update when you first add a library from Hackage; it downloads the package index, which is a sizeable download on metered data.

Check it worked

ls shows greeter.cabal and an app folder, among others. cabal run prints build progress lines and then What is your name? and Hello, Kofi!. A second cabal run skips the build because nothing changed.

Common beginner mistakes

  • Mixed tabs and spaces, or uneven indentation in a do block. GHC reports parse error on the line after the problem. Indent every line of the block with the same number of spaces.
  • Forgetting brackets around a function call used as an argument. putStrLn greet name is read as putStrLn with two arguments, and the error mentions applying putStrLn to too many arguments. Write putStrLn (greet name).
  • Using = instead of <- for input. name = getLine inside a do block does not read anything; it makes name another name for the action. Use name <- getLine.
  • Using putStr for a prompt and seeing nothing. Output to a terminal is sent a line at a time, so putStr "Name: " without a newline may not appear before getLine waits. Use putStrLn, or add import System.IO and call hFlush stdout after putStr.
  • Expecting variables to change. x = x + 1 is not an update; it defines x in terms of itself, and evaluating it never finishes. Haskell programs build new values instead of changing old ones.

Step 8 · Know where Haskell is used and clean up

Haskell's strengths are a strict type checker and code whose behaviour you can work out from its types and equations, which is why it turns up where mistakes are expensive:

  • Compilers and language tools: GHC is written in Haskell, as are the compilers for Elm and PureScript. ShellCheck, the widely used linter for shell scripts, is too.
  • Document tools: Pandoc, which converts between Markdown, Word, HTML, LaTeX and many other formats, is a Haskell program.
  • Finance and fintech: banks and payment companies use Haskell for pricing, risk and transaction systems, where a type error caught at compile time is cheaper than one found in production.
  • Blockchain: the Cardano node and its smart-contract tooling are written in Haskell.

When you are done, remove the tutorial folder:

Bash
cd ~
rm -rf ~/haskell-demo

To update later, run ghcup list to see newer versions, then ghcup install ghc <version> and ghcup set ghc <version>. To remove GHCup and everything it installed, including every GHC version, cabal and the ~/.ghcup folder, run:

Bash
ghcup nuke

ghcup nuke does not remove cabal's own folders. Delete ~/.cabal (or ~/.config/cabal and ~/.cache/cabal on newer setups) by hand if you want those gone too.

Check it worked

ls ~/haskell-demo reports No such file or directory. ghc --numeric-version still prints 9.14.1, because deleting a project does not touch the compiler.

Next steps

  • Try another typed functional language with a fast native compiler: Hello World in OCaml.
  • See Haskell's ideas applied to web front ends, in a compiler written in Haskell: Hello World in Elm.
  • Meet a functional-first language on .NET: Hello World in F#.
  • Keep going with the language: add import Data.Char (toUpper) to greet.hs and change greet to shout the name with map toUpper name. It stays pure, and main does not change.

Need this done for you?

Padding does this work for growing companies, from the first diagnosis to a system that keeps working.

Platform Engineering

Get new tutorials

New tutorials, about twice a month.