Le Nguyen The Dat bio photo

Le Nguyen The Dat

~ Data Science & Engineering

Twitter Facebook LinkedIn Github
Update: for OS X, do have a look at http://ghcformacosx.github.io/

As a (somewhat) newcomer to Haskell, it's super hard for me to fully understand about these things. To be honest, at first, I wanted to just write Haskell code, but that plan didn't really work, because I didn't know how to properly install a package, how to resolve problem when doing so, how to build my Haskell projects...

So, in order to help me remember, and help other newcomers, below is a list of instructions on how to deal with many of those problems:
(For the purpose of this blog post, I had spinned off a fresh (micro small) Ubuntu 12.04 64 bit on AWS.)
  1. Installing (latest) GHC
    • Go to http://www.haskell.org/ghc/ to download your latest GHC release. For example, for my Ubuntu 12.04 64 bit:
    • $ wget http://www.haskell.org/ghc/dist/7.6.3/ghc-7.6.3-x86_64-unknown-linux.tar.bz2
      $ tar -xvf ghc-7.6.3-x86_64-unknown-linux.tar.bz2
      $ sudo apt-get update
      $ sudo apt-get install libgmp3c2 build-essential # for libgmp and gcc
      $ cd ghc-7.6.3/
      $ ./configure
      $ sudo make install
    • You should have the latest GHC by now (you might need to open a new terminal):
    • $ which ghc
      $ ghc --version
      The Glorious Glasgow Haskell Compilation System, version 7.6.3
  2. Installing (latest) Cabal
    • Starting by installing and updating cabal's package list:
    • $ sudo apt-get install cabal-install
      $ cabal update
      $ sudo apt-get install zlib1g-dev # for the below step
      $ cabal install cabal cabal-install # updating cabal version
      $ cabal --version # might need to open a new terminal
    • Remember to fix your PATH variable:
    • $ export PATH=$HOME/.cabal/bin:$PATH # you might want to add this into your ~/.bashrc
    • Note: Skip this step if you already have the latest cabal version needed from the above commands. For the latest Cabal (currently 1.19x) and cabal-install (1.19x), clone the cabal Repository and install from source:
    • $ sudo apt-get install git # incase you don't have (g)it yet.
      $ git clone https://github.com/haskell/cabal.git
      $ cd cabal/
      $ cabal install Cabal/ cabal-install/
      $ cabal --version
      cabal-install version 1.19.2
      using version 1.19.2 of the Cabal library
  3. Building your first Haskell Project
    • I prefer to use Cabal Sandbox for this purpose.
    • Starting with "cabal sandbox init" in your project folder:
    • $ cd /path/to/project/folder
      $ cabal sandbox init

    • Continue development if needed and use runhaskell for testing / debugging purpose. (Note: there is a bug that prevents runhaskell to work properly within Cabal Sandbox folder, you will need to export GHC_PACKAGE_PATH): (ghc-pkg list for your global package database location, in my case, it's /usr/local/lib/ghc-7.6.3/package.conf.d)
    • $ export GHC_PACKAGE_PATH=$(pwd)/.cabal-sandbox/x86_64-linux-ghc-7.6.3-packages.conf.d:/usr/local/lib/ghc-7.6.3/package.conf.d
      $ runhaskell main.hs
    • If a new package / dependency needs to be installed:
    • $ unset GHC_PACKAGE_PATH
      $ cabal install <dependency>
    • Ready to build:
    • $ cabal init # fill in your project details, update main class in .cabal file, add LICENSE file
      $ cabal install
    • Running your program:
    • $ .cabal-sandbox/bin/>executable>
Note: cabal install sometimes fails when looking for executables located in ~/.cabal/bin/

For example:

setup: The program happy version >=1.17 is required but it could not be found.

To install those (without touching global package database), you can follow the below steps:

  $ mkdir tmp
$ cd tmp/
$ cabal sandbox init
$ cabal install happy --prefix=<PATH_TO_HOME_DIRECTORY>/.cabal/ # Note, you can not use tilde (~) for the path
$ happy --version