Setting up Emacs and Haskell on Linux
I'm curious about how functional programming feels with Haskell
so I recently invested a few hours to install it and prepare my
Emacs configuration to support it.
To spare your precious time I recorded the steps I took. It
should work without hassle on Debian based distros.
We'll start with the Haskell platform
- To install it issue in a terminal
sudo apt-get install haskell-platform.
- This will also include Cabal,
the widely used build system. Since it uses a local package list
you'll first have to update it before you can install
anything. Use this command:
- I learned that Emacs support for Haskell often consists of two parts: Emacs
Elisp packages stored somewhere in ~/.emacs.d which
are backed by some executables stored in ~/.cabal/bin. To
begin with, we need structured-haskell-mode
and ghc-mod, and
we can install those using Cabal:
cabal install structured-haskell-mode ghc-mod.
This takes some minutes, so you might want to read another
section of the friendly introduction
Learn You a Haskell for Great Good.
- Finally, you should edit your ~/.profile to add the ~/.cabal/bin
path. You can a add a line like this:
PATH="$PATH:$HOME/.cabal/bin". Make sure
the path is in effect before you try to work on Haskell in Emacs. For this, you can do a
source ~/.profile and start Emacs from the same terminal.
Now, let's turn to Emacs. In case you don't have Emacs you
can install the latest Emacs like this:
- Add the snapshot PPA
sudo apt-add-repository ppa:ubuntu-elisp/ppa in a terminal window.
- To actually install Emacs type
sudo apt-get install emacs-snapshot emacs-snapshot-el
Please note that there exist comprehensive and well maintained
Emacs configurations out there, some of which include Haskell
support, one example is
If you like to start with my settings you'll find my
~/.emacs.d contents on GitHub
to get it use
git clone https://github.com/friemen/emacsd.git ~/.emacs.d
. Please note that this
will overwrite stuff in any existing ~/.emacs.d folder. Afterwards
start Emacs, wait until install is complete and restart
Emacs. You'll find my favorite keyboard shortcuts in the
README on GitHub
We start with a project around our first lines of Haskell code:
- In a terminal create a new directory and cd into it:
mkdir helloworld && cd helloworld.
- Create a .cabal file:
cabal init. Use defaults (hit Enter), except where you are
asked for Library or Executable. Choose Executable.
- Open helloworld.cabal and add
the line that starts with
-- main-is:. Remove
uncomment the line.
- Create a new file helloworld/Main.hs with contents like below.
- Start the REPL (which is an interactive GHC) using
C-c C-l. Answer
y and hit Enter twice. Now your code is compiled and ready
to be executed. You can call
- In order to create an executable you have to first issue
cabal configure on
the command line.
C-c C-c in Emacs will execute
cabal build which creates
an executable file somewhere in the
- If you changed some code you can always hit
C-c C-l to recompile it
and make it available for the REPL.
That's it. Oh, and here's the example code I used:
module Main where
main :: IO ()
main = putStrLn $ hello "World"
hello :: String -> String
hello x = "Hello " ++ x
Now you're ready to dive in. Have fun!