85

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.

Norman Ramsey
  • 198,648
  • 61
  • 360
  • 533
  • 2
    `ghc-pkg list blaze-html` ? Are you sure you're running it as the right user? Maybe explicitly state the version to be unregistered? – ivanm May 14 '12 at 02:38
  • 1
    @ivanm thanks for asking. Turns out `ghc-pkg` *lied* to me! – Norman Ramsey May 14 '12 at 03:46
  • Sort of a duplicate of http://stackoverflow.com/questions/7252193/can-cabal-not-un-install-packages, but I'm reluctant to flag it because this one's better :) – Ben Millwood Sep 22 '12 at 18:37
  • 1
    [cabal-delete](https://github.com/iquiw/cabal-delete) is pretty nice for finding and removing orphaned packages. – Tobu Jan 03 '15 at 14:50
  • @Tobu What about `cabal-uninstall` [mentioned in answer below](http://stackoverflow.com/a/14248672/94687)? Is cabal-delete more powerful? Can it work with a cabal sandbox (will `cabal exec -- cabal-delete` work correctly and delete packages from the sandbox)? Why not make this an answer, too? It looks like a nice tool. – imz -- Ivan Zakharyaschev Feb 13 '15 at 17:43

4 Answers4

96

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

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

That should be sufficient.

Gavin
  • 4,458
  • 3
  • 24
  • 37
Don Stewart
  • 137,316
  • 36
  • 365
  • 468
23

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.

Sean Leather
  • 1,182
  • 1
  • 9
  • 25
musically_ut
  • 34,028
  • 8
  • 94
  • 106
20

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.

Theodore Norvell
  • 15,366
  • 6
  • 31
  • 45
Davorak
  • 7,362
  • 1
  • 38
  • 48
  • 1
    `cabal uninstall` results in `cabal: unrecognised command: uninstall (try --help)` – Steven Shaw Apr 30 '14 at 11:40
  • 2
    @StevenShaw - The link I provided goes to a hackage package that you need to install in order to use. I would recommend Don's answer, that is what I use. – Davorak Apr 30 '14 at 12:18
  • What about [cabal-delete](https://github.com/iquiw/cabal-delete) from a comment above by @Tobu? Is it better or more powerful than cabal-uninstall? – imz -- Ivan Zakharyaschev Feb 13 '15 at 17:42
7

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
Ben Millwood
  • 6,754
  • 24
  • 45