Installing ghc-mod for newer GHC versions

December 27, 2018

For GHC versions >= 8.2, getting a working ghc-mod is difficult. Lucky for us, Alan Zimmerman has done the hard work of getting ghc-mod working with GHC 8.2, 8.4, and 8.6 as part of the Haskell IDE Engine project.

There’s also news of a successor to ghc-mod, Happy Haskell Programming being in the works by Kazu himself.

For those of us using tooling which relies on ghc-mod, here are some steps to getting a working ghc-mod for GHC >= 8.2 thanks to Alan’s fork.

First, either clone Alan’s fork or add the fork as a remote to your existing ghc-mod clone -

% git clone
% cd ghc-mod

Next, checkout the appropriate branch for the desired GHC version. Note that ghc-mod only works for a given project when both ghc-mod and the project are compiled with the same version of GHC.

% git checkout ghc-8.4-hie

You’ll need the cabal binary (a.k.a. cabal-install). Get it via whichever means you need. I generally use stack, so I’ll just use that for now.

% stack install cabal-install

% cabal --version
cabal-install version
compiled using version of the Cabal library

Now we need to initialize our cabal build. I recommend using a sandbox so your ghc-mod build doesn’t affect global package state.

% cabal update
% cabal sandbox init

Next, we need to tell cabal about the ghc-mod-core dependency, which is located in the same repo under the core subdirectory.

% cabal sandbox add-source core

We’ll need the happy binary used by haskell-src-exts during its parser generation.

% cabal install happy

Now we can install the ghc-mod binary into our sandbox.

% cabal install

Optionally, you can copy the ghc-mod binary to somewhere easily accessible and namespaced for the GHC version.

cp .cabal-sandbox/bin/ghc-mod ~/bin/ghc-mod-8.4

And you’re done! I recommend running a test command from the terminal to confirm that it works properly with your project.

% cd path/to/your/project
% ghc-mod-8.4 check File.hs

At this point I did run into an issue with integer-gmp on OSX -

lookupSymbol failed in relocateSection (RELOC_GOT)
/Users/me/.stack/programs/x86_64-osx/ghc-8.4.3/lib/ghc-8.4.3/integer-gmp- unknown symbol `___gmp_rands'
unable to load package `integer-gmp-'

To get around this I simply hid the object file based on recommendations from this GHC ticket -

% cd /Users/me/.stack/programs/x86_64-osx/ghc-8.4.3/lib/ghc-8.4.3/integer-gmp-

% mv {HSinteger-gmp-{,_DISABLE_GHC_ISSUE_15105}