Installing GHC on FreeBSD
Recently, I discovered that the GHC developers classify the platforms that they support into different tiers. Tier 1, somewhat surprisingly, includes four distinct operating systems: Windows, OS X, Linux, …and FreeBSD. That last one surprised me, since they don’t even advertise their FreeBSD support that much on the Haskell Platform website. (You have to go to the Linux section to find the FreeBSD information.)
This got me curious as to how easy to use GHC is on FreeBSD, since some other OSes have some kinks that need ironed (*cough*Windows*cough*). Since I found the process of installing GHC and cabal
on FreeBSD to be somewhat nontrivial, I’ll post the exact steps I took here.
Prerequisites
- A FreeBSD distribution. You can get the real deal here, or you can get a noob-friendlier derivative such as PC-BSD. I used the latter.
- Internet access
There are two paths you can go by
There are two common ways of installing GHC and its associated bells and whistles. One can install the Haskell Platform, which is a collection of GHC, the cabal
build system, several prebuilt packages, and other utilities. Installing the Haskell Platform is quicker and less painful, but it is updated less frequently. If you prefer this, go to the Haskell Platform section.
Alternatively, you can install a binary distribution of GHC and then build all of the needed tools yourself. This requires more heavy lifting, but you gain the ability to use whatever packages you want (instead of whatever the Haskell Platform is bundled with). If you prefer this, go to the Binary distribution section.
Haskell Platform
Luckily, the Haskell Platform exists as a FreeBSD port. At the time of this post, the latest version includes GHC 7.6.3. You can install the Platform by doing one of the following:
- Opening a terminal and running
pkg install hs-haskell-platform
as root - (On PC-BSD) Go to App Cafe. Click Browser View > Raw Packages. Then search for “Haskell Platform” and install it.
If you don’t want the full Haskell Platform, you can also individually install the ghc
and hs-cabal-install
packages.
Either way, once everything you want is installed, you should edit ~/.cshrc
and add ~/.cabal/bin
to your PATH
so that you can run Haskell executables. Make sure that you put it before /usr/local/bin
so that more recently installed versions of cabal
take precedence over the Haskell Platform’s version.
Binary distribution
GHC
First, download the latest GHC binary archive (version 7.8.3 at time of writing) and cabal-install
source archive (version 1.20.0.3 at time of writing).
Next, we need to install some GNU software, since BSD distributions tend to spurn it. According to the FreeBSD Haskell devs, you need at least this much installed:
compat8x
(if using FreeBSD 9 or later)gcc
(4.7 or later)gmake
perl5
libiconv
On PC-BSD, I already had gcc47
, perl5
, and libiconv
installed, so I only needed to install the following:
You might be tempted to use
clang
, which is now FreeBSD’s default compiler, instead ofgcc
, but I had nothing but problems using it. The FreeBSD Haskell devs also advise not usingclang
until it is further tested.
Now we can install GHC:
Note that this won’t configure GHC’s manpages correctly. If you want them, make sure to run
cp /usr/local/share/man/man1/ghc.1 /usr/local/man/man1/
as root after installing GHC.
cabal-install
Extract the cabal-install
archive and open bootstrap.sh
in your favorite editor. As of version 1.20.0.3, line 208 of this file, namely:
won’t work correctly on FreeBSD. To fix it, change this line to:
Also, bootstrap.sh
will try to look for gcc
, but it is probably named gcc47
or something similar instead. To help ease boostrap.sh
’s burden, make a symbolic link with ln -s /usr/local/bin/gcc47 /usr/local/bin/gcc
as root. Then install cabal-install
by running ./boostrap.sh
.
Finally, you should edit ~/.cshrc
and add ~/.cabal/bin
to your PATH
so that you can run cabal
and other Haskell executables.
But wait
Regardless of how you installed GHC, you’ll notice some problems if you try to install packages that require nontrivial build tools such as autoconf
or hsc2hs
. The problems might manifest themselves as errors such as runProcess: runInteractiveProcess: exec: permission denied (Permission denied)
. This hints at the problem: a build tool is trying to execute a file in /tmp
, but /tmp
has the noexec
filesystem option.
In PC-BSD, this can be fixed by going to the PC-BSD Control Panel > Hardware > Disk Manager. Under the ZFS Filesystems tab, right-click tank/tmp
and click “Edit properties”. Change exec
to “on” and click “Apply.”
That should clear the way for cabal
to do its thing. Now you can get the very latest stuff: