Wednesday, September 21, 2011

Installing Haskell in Ubuntu

We can install Haskell Compiler like GHC and Haskell Platform using synaptic package manager in Ubuntu. But to get the latest version, we might have to install it manually.

1. Download the Glasgow Haskell Compiler binary.
2. Decompress the downloaded file and go into the ghc directory.
3. Now you need to configure the ghc binary. You can give the path of installation using the prefix flag.
./configure --prefix=/usr/local/ghc/7.0.3
Here I have downloaded v7.0.3. Since I wanted to keep different version of the compiler, I keep folders with version number under the ghc folder. Accordingly, you give the path to the configure script.
4. Install. It is already compiled, because you downloaded the binary.
sudo make install
5. Now you need to add the ghc directory to your path so that your bash shell can find it. There are numerous ways to do it. Create a file say "" in "/etc/profile.d"
sudo vim /etc/profile.d/
# Add the below contents to the file, save and quit.

# ghc-7.0.3 path
Log out and log in. Open your terminal (Ctrl + Alt + T) and type "ghc --version" to ensure ghc is available.
6. Now you have a working Haskell Compiler. It is highly recommended to install Haskell-Platform which are a set of libraries for Haskell. For that you go to Haskell-Platform page and download the tar.gz file under the "Build from source" section. Decompress the downloaded file.
7. There are dependency files that you need to have installed before proceeding to compile the Haskell-Platform. So, let us install aptitude, which is a ncurses front-end to APT. Then we shall install 'happy', 'alex' and 'darcs' using 'aptitude' which are haskell libraries.
sudo apt-get install aptitude
sudo aptitude install happy alex darcs
Now we need to install C dependencies. To find that we will use a script. In general, the C dependencies are 'libedit-dev', 'libbsd-dev', 'libgmp3-dev', 'zlib1g-dev', 'freeglut3-dev'. You will need to run the below script to find out.
Let us call the below script 'depcheck'. Put it in 'bin' folder under your home directory. The 'bin' folder is added to you bash path. So bash can find all the scripts inside that folder.
# check dependency of a package. The libs with -dev need to be installed
# before building

strace -f -o /tmp/log ./configure
# or make instead of ./configure, if the package doesn't use autoconf
for x in `dpkg -S $(grep open /tmp/log|\
perl -pe 's!.* open\(\"([^\"]*).*!$1!' |\
grep "^/"| sort | uniq|\
grep -v "^\(/tmp\|/dev\|/proc\)" ) 2>/dev/null|\
cut -f1 -d":"| sort | uniq`; \
do \
echo -n "$x (>=" `dpkg -s $x|grep ^Version|cut -f2 -d":"` "), "; \
Set executable permission for the depcheck script.
chmod +x depcheck
Now go to the haskell-platform directory which you decompressed in step 6 and run depcheck. (Just type depcheck and press enter).
It will print out a lot of library names. You need to install all the ones which ends with a '-dev'. So the libraries shown to me are 'libedit-dev', 'libbsd-dev', 'libgmp3-dev', 'zlib1g-dev', 'freeglut3-dev' and 'libglu1-mesa-dev'. (Just in case, if your installation aborted saying that it is missing some library, then you can install them and continue the installation).
sudo apt-get install libedit-dev libbsd-dev libgmp3-dev zlib1g-dev freeglut3-dev libglu1-mesa-dev
8. Now that you have got all your dependencies installed, you can proceed with the Haskell-Platform. (You are now in the haskell-platform directory).
./configure --prefix=/usr/local/haskell-platform/2011.2.0.1
sudo make install
It will take a while to complete.
9. After the successful installation of Haskell-Platform, you need to add the directory to your path. Like in step 5, you create a file say '' in '/etc/profile.d/' folder.
sudo vim /etc/profile.d/
# Add the following contents to the file, save and quit.

# Haskell-Platform 2011.2.0.1
Log out and log in.
10. All done! Now do a cabal update to get the latest list of all the packages in hackage, which will take a while. Now you can install libraries (eg: hscurses) using cabal.
cabal update
cabal install hscurses #ncurses binding for haskell