Installing ghcjs on Debian 7 Wheezy

2015-04-10

ghcjs is somewhat of a pain to get installed. It has improved recently, but it still requires specific versions of ghc, cabal, and other Haskell libraries. This blog post walks you through getting ghcjs up and running on Debian 7 Wheezy. Some of these instructions were taken from the ghcjs README.

We will be installing the following versions of the software below:

  • ghc 7.8.4
  • cabal 1.22.2.0 (hash: 5d1f226)
  • ghcjs 0.1.0 (hash: 35a5974)
  • ghcjs-prim 0.1.0 (hash: bd83ce2)
  • nodejs 0.12.2

NOTE: Before doing any of the following steps, you may want to delete your ~/.cabal/ directory.

$ rm -rf ~/.cabal

Install Node.js

Node.js is used by ghcjs for running Template Haskell. We install the Linux binaries provided by nodejs.org.

$ wget \
    'http://nodejs.org/dist/v0.12.2/node-v0.12.2-linux-x64.tar.gz'
$ tar xf node-v0.12.2-linux-x64.tar.gz

This creates the directory node-v0.12.2-linux-x64/ which contains a bin/ directory. Make sure node-v0.12.2-linux-x64/bin is on your PATH.

Install Other Dependencies for ghcjs

ghcjs needs the libtinfo and autoconf Debian packages in order to build. Install these with apt-get.

$ apt-get install autoconf libtinfo5 libtinfo-dev

Install GHC 7.8.4

We now install GHC 7.8.4 from the GHC download page.

$ wget 'https://www.haskell.org/ghc/dist/7.8.4/ghc-7.8.4-x86_64-unknown-linux-deb7.tar.xz'
$ tar xf ghc-7.8.4-x86_64-unknown-linux-deb7.tar.xz
$ cd ghc-7.8.4
$ ./configure --prefix=/opt/ghc-7.8.4
$ sudo make install

Be sure to put /opt/ghc-7.8.4/bin on your PATH.

Install cabal-install

Next, install cabal-install.

$ git clone https://github.com/haskell/cabal.git
$ cd cabal/cabal-install
$ git checkout cabal-install-v1.22.2.0
$ ./bootstrap.sh
$ ~/.cabal/bin/cabal update

This puts cabal in ~/.cabal/bin, so be sure to put ~/.cabal/bin on your PATH as well.

We will install alex and happy. The additional constraints are discussed below.

$ cabal install alex happy \
    --constraint=haddock-api==2.15.0.2 \
    --constraint=transformers==0.3.0.0

Install ghcjs

Now we will actually install ghcjs.

$ git clone https://github.com/ghcjs/ghcjs-prim.git
$ git clone https://github.com/ghcjs/ghcjs.git
$ (cd ghcjs-prim && git checkout bd83ce2)
$ (cd ghcjs && git checkout 35a5974)
$ cabal install ./ghcjs ./ghcjs-prim/ -j \
    --constraint=haddock-api==2.15.0.2 \
    --constraint=transformers==0.3.0.0
$ ghcjs-boot --dev

ghcjs will be installed into ~/.cabal/bin. The constraints are needed because ghcjs seems to fail to compile without them. The same constraints are used when installing alex and happy to make sure that alex and happy don't pull in libraries that will eventually be overwritten when installing ghcjs.

Now Some Fun

Now that we've got everything installed, let's play around with it.

Lets try using ghcjs by itself.

$ mkdir ghcjs-test
$ cd ghcjs-test
$ echo 'main = print "Hello JS"' > Main.hs
$ ghcjs Main.hs

Running ghcjs creates the directory Main.jsexe/. Inside this directory there are two files of interest, all.js and index.html. The all.js can be run with node.

$ node Main.jsexe/all.js
"Hello JS"

The index.html file can be used to run our code in a browser.

Conclusion

Now that you have ghcjs working, you may want to play around with FRP in the browser using something like Sodium + ghcjs-jquery, or reflex-dom.

EDIT: Ryan Trinkle (the author of reflex and reflex-dom) emailed me to make sure I point out that there are the try-reflex and ghcjs-setup projects on Github that use the Nix package manager to give you an environment where ghcjs and reflex-dom are already installed for you. If you just want to play around with ghcjs, this is probably the easiest way to get started.

tags: haskell