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:
sudo apt update
sudo apt install -y build-essential curl libffi-dev libgmp-dev libncurses-devNow run the official GHCup installer as your normal user, not with sudo:
curl --proto '=https' --tlsv1.2 -sSf https://get-ghcup.haskell.org | shThe 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:
source ~/.ghcup/envGHCup 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:
ghcup install ghc 9.14.1
ghcup set ghc 9.14.1
ghcup install cabal latest
ghcup listghcup 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:
mkdir -p ~/haskell-demo
cd ~/haskell-demo
ghc --version
ghc --numeric-version
cabal --versionCheck 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:
ghciIt 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:
2 + 3 * 4
length "Hello"
take 5 [1..]
:type not
:type putStrLn
double x = x * 2
double 21
:type double
:quitHere is what each one shows you:
2 + 3 * 4prints14. Normal arithmetic rules apply: multiplication before addition.length "Hello"prints5. 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 numberstakeasks for. This is laziness in one line.:type notprintsnot :: Bool -> Bool. Read::as "has type".nottakes aBool(True or False) and returns aBool.:type putStrLnprintsputStrLn :: String -> IO (). It takes aStringand returns anIO (): 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 * 2defines a function calleddoublewith one argument,x. You wrote no types at all.double 21prints42.:type doubleprintsdouble :: Num a => a -> a. GHC worked out the type itself:doubleworks for any typeathat is a number (Num a =>), takes one and returns one of the same type. That is type inference.:quitleaves 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:
cd ~/haskell-demo
cat > hello.hs <<'HS'
main :: IO ()
main = putStrLn "Hello, World!"
HS
runghc hello.hsThe program is two lines:
main :: IO ()is the type signature. It saysmainhas typeIO (): an action that interacts with the outside world and returns nothing useful. Every Haskell program starts by running the action calledmain. 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!"definesmain. The=means "is defined as", not "assign":mainis the action you get by givingputStrLnthe text"Hello, World!".putStrLnprints 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:
ghc hello.hs
ls
./helloGHC prints something like [1 of 2] Compiling Main ( hello.hs, hello.o ) and [2 of 2] Linking hello. It leaves three new files:
hellois 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.ois the compiled machine code for theMainmodule, before linking.hello.hiis 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:
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.hsLine by line:
greet :: String -> Stringsaysgreettakes aStringand returns aString. There is noIOin its type, so the compiler guarantees it cannot print, read input or do anything except compute text.greet "" = ...andgreet 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; otherwisenamematches anything and the second one applies.++joins two strings (in Haskell, aStringis a list of characters, and++joins lists).main = dostarts 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 <- getLinerunsgetLine, which reads one line from the keyboard, and gives the result the namename.getLinehas typeIO String: an action that, when run, produces aString. The arrow<-is how you get theStringout 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 readputStrLn greet nameas givingputStrLntwo 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:
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.txtGHC 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:
cd ~/haskell-demo
mkdir greeter
cd greeter
cabal init --non-interactive --exe
lscabal 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:
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 greetermodule 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 erroron 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 nameis read asputStrLnwith two arguments, and the error mentions applyingputStrLnto too many arguments. WriteputStrLn (greet name). - Using
=instead of<-for input.name = getLineinside a do block does not read anything; it makesnameanother name for the action. Usename <- getLine. - Using
putStrfor a prompt and seeing nothing. Output to a terminal is sent a line at a time, soputStr "Name: "without a newline may not appear beforegetLinewaits. UseputStrLn, or addimport System.IOand callhFlush stdoutafterputStr. - Expecting variables to change.
x = x + 1is not an update; it definesxin 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:
cd ~
rm -rf ~/haskell-demoTo 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:
ghcup nukeghcup 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)togreet.hsand changegreetto shout the name withmap toUpper name. It stays pure, andmaindoes not change.
Related tutorials
- Hello World in OCaml: opam, utop and a dune projectYou will install OCaml 5.5 with opam, run Hello World as a script, compile it to bytecode and native code, and build a dune project with a greet function whose type OCaml works out for you.
- Hello World in Elm: a web page with no runtime exceptionsYou will install Elm 0.19.3, compile a Hello World page with elm make, build a greeter that updates as you type using The Elm Architecture, and read and fix a compiler type error.
- Hello World in F#: dotnet, printfn and F# scriptsYou will create and run an F# 10 console app with the .NET 10 SDK, run the same idea as an F# script with dotnet fsi, and extend it with a greet function, the pipeline operator and pattern matching on the command-line arguments.
Need this done for you?
Padding does this work for growing companies, from the first diagnosis to a system that keeps working.
Get new tutorials