April 6, 2021

927 words 5 mins read

Installing Haskell quick and easy

There are several ways to install Haskell, pick one and let’s waste no time:

Using stack

On Linux or Mac

Open a terminal an paste the following:

curl -sSL https://get.haskellstack.org/ | sh

Follow the script instructions if any, then setup a Haskell compiler like so:

$ stack setup

If you get a message like “stack command not found” or something along the lines, then probably you need to close an re-open your terminal first.

Now check you have a Haskell compiler, you can verify with:

$ stack ghc --version

If you see something like:

The Glorious Glasgow Haskell Compilation System, version 8.10.4

Then you’re set!

On Windows

Grab the Windows installer at the official doc page. Or here is the direct link to download (but don’t trustme blindly on that!).

How to uninstall?

On Linux or Mac

Just delete the ~/.stack folder and any ~/.stack-work folders in your projects, example:

$ rm -rf ~/.stack

You may need to delete any line mentioning stack in file ~/.bashrc.

On windows

Same as any other windows program, look for “uninstall programs” after pressing the Windows key.

Using ghcup

On Linux or Mac

Open a terminal and run the following:

$ curl --proto '=https' --tlsv1.2 -sSf https://get-ghcup.haskell.org | sh

Follow the script instructions, read carefully, if you get asked to install GHC, Cabal or Haskell Language Server, say `Yes please!`. After installation finishes you might need to close and reopen your terminal.

Check your installation with:

$ ghcup list

You should have checkmarks on the recommended versions of GHC, Cabal and Haskell Language Server, but if not, then install them like so:

$ ghcup install ghc 8.10.3   # This installs GHC
$ ghcup install cabal        # This installs Cabal
$ ghcup install hls          # This installs the Haskell Language Server

Check again:

$ ghcup list

You should now be all set!

On Windows

ghcup does not work on Windows! (AFAIK). Try Windows Subsystem for Linux (WSL) or installing Linux in a VM.

How to uninstall?

On Linux or Mac

Should be sufficient to delete ~/.cabal and ~/.ghcup folders:

$ rm -rf ~/.cabal ~/.ghcup

You may need to delete any line mentioning ghcup in file ~/.bashrc.

On windows

I don’t know 🤷

Using nix

On Linux or Mac

NOTE If you’re on MacOS Catalina or newer (10.15 or higher) the installation is a little bit more tricky, read the Nix manual section on “macOS Installation” carefully.

For the ones using Linux or MacOS Mojave or older (10.14 or lower) keep following:

$ sh <(curl -L https://nixos.org/nix/install) --daemon

Follow the script instructions carefully, after it finishes make sure to close and re-open your terminal so that your shell profile is sourced.

Now you can install Haskell either locally (only for a project) or globally (for your whole user system), you can have both or just one as you wish

Haskell locally for a project

In the folder containing a project (or an empty folder if you’re getting started) create a file called shell.nix with the following contents:

let
  pkgs = import <nixpkgs> {};
  ghc = pkgs.haskellPackages.ghcWithHoogle (hpks:
    with hpks; [
      mtl
      text
      time
      aeson
      bytestring
      containers
      scientific
      unordered-containers
    ]);
in
  pkgs.stdenv.mkDerivation {
    name = "wealth-manager-haskell";

    # List non-haskell dependencies as build inputs
    buildInputs = [
      ghc
      pkgs.haskellPackages.cabal-install
      pkgs.haskellPackages.haskell-language-server
    ];

    shellHook = ''
      eval $(egrep ^export ${ghc}/bin/ghc)
      echo "Recommended env vars have been loaded loaded:"
      egrep ^export ${ghc}/bin/ghc
    '';
  }

Now, in your terminal head to that folder using cd and once you’re there open a nix shell that will load the file we just created:

$ nix-shell

It will start to download a working GHC version, along with Cabal and the Haskell Language Server, once it finishes, you can verify like this:

$ cabal --version
$ ghc --version

Once you’re finished playing with Haskell in that project exit the Nix shell with:

$ exit  # Or Ctrl+D also works

When you want to use Haskell again, just execute nix-shell again from your project directory, only the first time takes longer, subsequent executions are faster since everything will be already set up on your environment.

  • How to uninstall?

    If you don’t want anything Haskell related but you still want Nix around, just exit the Nix shell, delete your shell.nix and run:

        $ rm shell.nix          # This is deleting the file we created
        $ nix-collect-garbage
        

    If you don’t want anything, not even Nix around, then:

        $ sudo rm -rf /nix
        $ rm -rf ~/.nix*
        

    You may need to delete any line mentioning Nix in file ~/.bashrc.

Haskell globally for your system

In this case we don’t need a Nix shell, all we have to do is to install the packages like this:

$ nix-env -f "<nixpkgs>" -iA haskellPackages.ghc haskellPackages.cabal-install haskellPackages.haskell-language-server

You should now have GHC, Cabal and the Haskell Language Server installed, if not, try closing and re-opening your terminal:

$ cabal --version
$ ghc --version

You’re set!

  • How to uninstall?

    If you don’t want anything Haskell related but you still want Nix around, just uninstall the packages like this:

        $ nix-env -f "<nixpkgs>" --uninstall haskellPackages.ghc haskellPackages.cabal-install haskellPackages.haskell-language-server
        $ nix-collect-garbage
        

    If that doesn’t work, try removing haskellPackages. prefix on each package.

    If you don’t want anything, not even Nix around, then:

        $ sudo rm -rf /nix
        $ rm -rf ~/.nix*
        

    You may need to delete any line mentioning nix in file ~/.bashrc.

On Windows

nix does not work on Windows! (AFAIK). Try Windows Subsystem for Linux (WSL) or installing Linux in a VM.

Sources