37

I want to upgrade all packages, not just a specific one with cabal install --upgrade-dependencies.

gatoatigrado
  • 16,580
  • 18
  • 81
  • 143
  • 1
    You know cabal upgrade was removed for a reason, right? This will likely break all your packages, sooner or later. – Carl Aug 02 '11 at 00:51
  • 3
    You should just use cabal-dev, http://hackage.haskell.org/package/cabal-dev – John L Aug 02 '11 at 07:37
  • @John L yep, it's gotten quite nice. – acfoltzer Aug 02 '11 at 16:08
  • For reference, `cabal`s new integrated `sandbox` function should be able to replace `cabal-dev` for almost all usecases. See [this blog post](http://coldwa.st/e/blog/2013-08-20-Cabal-sandbox.html) – Jacob Wang May 02 '14 at 11:23

2 Answers2

52

This bit of shell hackery works for me on OS X:

cabal list --simple-output --installed | awk '{print $1}' | uniq | xargs -I {} cabal install {} --reinstall

EDIT: Now forces a reinstall, and avoids installing a package more than once when more than one version is present. Thanks for the comments!

EDIT YEARS LATER: Now that Cabal sandboxes and Stack exist, I strongly recommend against trying to upgrade packages in place. You'll end up with far fewer headaches if you instead can just wipe out an existing sandbox and reinstall up-to-date dependencies.

acfoltzer
  • 5,588
  • 31
  • 48
  • 2
    Don't forget the `--reinstall` flag on that last `cabal install` command. – Thomas M. DuBuisson Aug 01 '11 at 22:52
  • 4
    Shouldn't there be a `uniq` filter after the `awk` step? At least in my environment it would call `cabal install` multiple times with the same argument... also, it might not be desirable to upgrade the system libraries bundled with GHC, such as `base` – hvr Aug 01 '11 at 22:56
  • @ThomasM.DuBuisson, @acfoltzer. Should the `--reinstall` in the command change to `--force-reinstall`? I currently have cabal with version 1.16.0.2. When I upgrade all packages, it constantly reminds that "The following packages are likely to be broken by the reinstalls...... Use --force-reinstalls if you want to install anyway." – Chris.Q Aug 04 '14 at 15:16
  • 2
    As far as I understand, `cabal install` lies. The better approach would be to use `ghc-pkg list --user`, so you never touch GHC global packages. – Leon Mergen Sep 11 '14 at 02:32
  • 1
    Is this still a valid approach? [Is there a better alternative?](http://stackoverflow.com/q/32077856/656912) It seems odd (stupid actually) that Cabal provides no way to keep package uptodate! – orome Aug 18 '15 at 16:26
  • @raxacoricofallapatorius at this point, I would recommend using Cabal sandboxes or Stack, which are easy to wipe and start over, rather than reinstalling new versions in existing package databases – acfoltzer Aug 20 '15 at 17:46
7

The .cabal/world file contains a list of every package you installed explicitly (listed in a cabal install command, as opposed to pulled through dependencies). Trim it to remove packages that are only useful as dependencies, packages that are deprecated, and version-locked packages that you'd rather upgrade.

Cabal doesn't know how to clean-up after itself, but you can remove almost everything. The next command will reinstall from .cabal/packages (a tarball cache):

cp -t bin .cabal/bin/cabal
rm -rf .cabal/{bin,lib,share} .ghc/*-*-*/
ghc-pkg check |&egrep -- '^[A-Za-z0-9-]+-[0-9]' |xargs -n1 --no-run-if-empty ghc-pkg unregister

Now reinstall everything that was manually installed:

cabal install world --upgrade-dependencies --force-reinstalls
Tobu
  • 24,771
  • 4
  • 91
  • 98