How can I uninstall a version of a Cabal package?

42,279

Solution 1

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

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

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

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
Share:
42,279

Related videos on Youtube

Norman Ramsey
Author by

Norman Ramsey

Every time I see a question about "strong" or "weak" typing, I kill a kitten.

Updated on October 06, 2020

Comments

  • Norman Ramsey
    Norman Ramsey over 3 years

    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.

    • ivanm
      ivanm almost 12 years
      ghc-pkg list blaze-html ? Are you sure you're running it as the right user? Maybe explicitly state the version to be unregistered?
    • Norman Ramsey
      Norman Ramsey almost 12 years
      @ivanm thanks for asking. Turns out ghc-pkg lied to me!
    • Ben Millwood
      Ben Millwood over 11 years
      Sort of a duplicate of stackoverflow.com/questions/7252193/…, but I'm reluctant to flag it because this one's better :)
    • Tobu
      Tobu over 9 years
      cabal-delete is pretty nice for finding and removing orphaned packages.
    • imz -- Ivan Zakharyaschev
      imz -- Ivan Zakharyaschev about 9 years
      @Tobu What about cabal-uninstall mentioned in answer below? 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.
  • Steven Shaw
    Steven Shaw about 10 years
    cabal uninstall results in cabal: unrecognised command: uninstall (try --help)
  • Davorak
    Davorak about 10 years
    @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.
  • imz -- Ivan Zakharyaschev
    imz -- Ivan Zakharyaschev about 9 years
    What about cabal-delete from a comment above by @Tobu? Is it better or more powerful than cabal-uninstall?
  • Erik Kaplun
    Erik Kaplun about 9 years
    once unregistered, are there any files lying around somewhere that should be pruned?
  • CMCDragonkai
    CMCDragonkai about 9 years
    Comments on other places mention that ghc-pkg leaves folders around?
  • pyrrhic
    pyrrhic over 8 years
    I just tried this on Mac and it does not seem to work.
  • Leahcim
    Leahcim over 7 years
    have a look in your ~/.cabal/ folder for information about where pkgs and binaries are