osa1 github about atom

GHC + Cabal installation guide for starters

September 12, 2014 - Tagged as: haskell, en.

I see a lot of starters having problems with installing latest GHC and Cabal, and then keeping their package repository in a sane state(e.g. no thousands of broken packages because of one re-install, no installation problems because of conflicts). I came up with a simple method several years ago, and today when combined with sandboxes, that works really well. I suggest every new Haskeller to do the same. Here’s how I do my GHC/Cabal installation up-to-date and sane:

NEVER USE HASKELL PALTFORM. When I first started, it caused just too much pain. I don’t know how it is today, but I presume same problems should still apply. (globally installing packages)

Most important thing is to keep Cabal and cabal-install up-to-date. You can easily remove a GHC, or install other versions and keep them togerher once you have a working and relatively new(so that it supports sandboxes) Cabal. In fact, I currently have 3 versions of GHC installed, for different projects, and I have no problems at all. Starting from a system with no GHC/Cabal is installed, here’s the way to install latest Cabal and cabal-install:

  1. Install whatever GHC and Cabal you have in your package manager.
  2. If the GHC you installed from package manager is not latest one, install latest pre-compiled binary from GHC webpage. Set your $PATH(or move executables to $PATH) and remove the GHC you installed using the package manager.
  3. Run cabal update && cabal install Cabal cabal-install, then remove Cabal and cabal-install installed using the package manager, and add ~/.cabal/bin to $PATH. After doing that, you’ll have latest GHC and Cabal installed. Also, Cabal will be installed locally, so you can update it very easily using cabal udpate && cabal install Cabal cabal-install whenever you want. Updating for newer GHC is similarly easy, just installed pre-compiled binary from the link above and move it wherever you want. You can use multiple GHC installations at the same time without any problems, Cabal just keeps separate repositories for different GHC versions.

The worst thing that can happen is that your package manager may not have Cabal at all. In that case you may need Haskell Platform temporarily, for booting GHC and Cabal. Once you have Haskell Platform(which includes Cabal) and run cabal update && cabal install Cabal cabal-install, just remove Haskell Platform and download latest GHC as mentioned above and go from there.

Occasionally you may want to remove some directories in ~/.cabal/lib. Here’s what I have right now:

➜  lib  pwd
/home/omer/.cabal/lib
➜  lib  ls | xargs du -hs
107M    x86_64-linux-ghc-7.6.3
341M    x86_64-linux-ghc-7.8.3
170M    x86_64-linux-ghcjs-0.1.0_ghc-7.8.2
34M     x86_64-linux-ghcjs-0.1.0_ghc-7.8.3

I have 170M of libraries installed in GHCJS compiled with GHC 7.8.2, but I updated my GHCJS installation and I won’t be using that version anymore, so it’s safe to remove that directory. Similarly you may want to remove versions you won’t be using anymore.

Once you have GHC and Cabal installed, you should be very careful with global installations. Basically all you need to do is to use sandboxes as much as possible. You may want or need to have some programs installed globally, like alex and happy and those are fine since they have almost no dependencies at all.

In all other cases, just create a ~/bin directory and add it to your $PATH. Now whenever you need a Haskell program in your path, install it in a sandbox, and symlink it to ~/bin.

Another very useful tip: You may still have some installation problems because of dependency conflicts. In that cases always try to install with cabal install --allow-newer. Most of the time this sandbox approach + --allow-newer should solve all your problems.

As a last thing, if you still have problems because you installed some libs globally for some reason, you may want to reset your whole Cabal state. In that case, just copy ~/.cabal/bin/cabal to somewhere else and remove ~/.cabal. Then using copied cabal executable, run cabal update && cabal install Cabal cabal-install again. Now you have a fresh Cabal state and you can remove copied cabal executable and go with the one just installed at ~/.cabal/bin.

I hope this helps.