How can I uninstall a version of a Cabal package?

HaskellGhcCabal

Haskell Problem Overview


Happstack Lite is breaking on me because it's getting blaze-html version 0.5 and it wants version 0.4. Cabal says that both versions 0.4.3.4 and 0.5.0.0 are installed. I want to remove the 0.5.0.0 and use just the older version. But cabal does not have an "uninstall" command, and when I try ghc-pkg unregister --force blaze-html, ghc-pkg says my command has been ignored.

What do I do?

UPDATE: Don't believe it. Although ghc-pkg claims to ignore the command, the command isn't ignored. And with Don Stewart's accepted answer you can remove exactly the version you wish to eliminate.

Haskell Solutions


Solution 1 - Haskell

You can ghc-pkg unregister a specific version, like so:

$ ghc-pkg unregister --force regex-compat-0.95.1

That should be sufficient.

Solution 2 - Haskell

If you are outside a sandbox:

ghc-pkg unregister --force regex-compat-0.95.1

If you are inside a cabal sandbox:

cabal sandbox hc-pkg -- unregister attoparsec --force

The first -- is the argument separator for hc-pkg. This runs ghc-pkg in a sandbox aware manner.

Solution 3 - Haskell

There is also the cabal-uninstall package which provides a cabal-uninstall command. It unregisters the package and deletes the folder. It is worth mentioning though that it passes --force to ghc-pkg unregister so it can break other packages.

Solution 4 - Haskell

Here's a shell script I use to uninstall a package. It supports multiple installed versions of GHC and also wipes relevant files (but is provided without warranty, don't blame me if you hose your installation!)

#!/bin/bash -eu
# Usage: ./uninstall.sh [--force | --no-unregister] pkgname-version

# if you set VER in the environment to e.g. "-7.0.1" you can use
# the ghc-pkg associated with a different GHC version
: ${VER:=}

if [ "$#" -lt 1 ]
then
        echo "Usage: $0 [--force | --no-unregister] pkgname-version"
        exit 1
fi

if [ "$1" == "--force" ]
then force=--force; shift; # passed to ghc-pkg unregister
else force=
fi

if [ "$1" == "--no-unregister" ]
then shift # skip unregistering and just delete files
else
        if [ "$(ghc-pkg$VER latest $1)" != "$1" ]
        then
                # full version not specified: list options and exit
                ghc-pkg$VER list $1; exit 1
        fi
        ghc-pkg$VER unregister $force $1
fi

# wipe library files
rm -rfv -- ~/.cabal/lib/$1/ghc-$(ghc$VER --numeric-version)/

# if the directory is left empty, i.e. not on any other GHC version
if rmdir -- ~/.cabal/lib/$1 
then rm -rfv -- ~/.cabal/share/{,doc/}$1 # then wipe the shared files as well
fi

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNorman RamseyView Question on Stackoverflow
Solution 1 - HaskellDon StewartView Answer on Stackoverflow
Solution 2 - Haskellmusically_utView Answer on Stackoverflow
Solution 3 - HaskellDavorakView Answer on Stackoverflow
Solution 4 - HaskellBen MillwoodView Answer on Stackoverflow