Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

Whenever I install a program via MacPorts, it gets and installs lots of dependencies. When I uninstall said program, how do I make MacPorts recursively remove all unused dependencies with it?

share|improve this question
add comment

6 Answers

up vote 59 down vote accepted

MacPorts 1.9.0 added the sqlite portdbformat and MacPorts 2.0.0 dropped the old flat format. The sqlite port DB format is the default for new installations; old installations that were upgraded to 1.9.x will continue to use the flat format1. You can convert a 1.9.x flat system to the sqlite format by upgrading to MacPorts 2.0 or by changing the portdbformat value in /opt/local/etc/macports/macports.conf and then issuing a port command as root (e.g. sudo port installed).

One of the features of the new format is that it keeps track of “requested” versus “unrequested” port installations. An unrequested port is one that was only installed because some other port depends on it. The leaves pseudo-portname expands to all the unrequested ports upon which no other installed port depends. You can use this to “clean up” unneeded ports even if you did not originally uninstall them with sudo port uninstall --follow-dependencies portname (which will do what you want, but only if you remember to use it every time you uninstall something).

You should examine your existing leaves before uninstalling any of them.

port echo leaves

Some common leaves (automake, gperf, libtool, pkgconfig) are build-time dependencies of common ports, so you may want to “request” them (sudo port setrequested port1 port2 port3 …) to avoid uninstalling them just to have to reinstall them later.

You can uninstall any remaining leaves quite easily:

sudo port uninstall leaves

Note: Before pruning your leaves, you may also want to uninstall old versions of ports that are no longer “active”. This may reveal a few more leaves (i.e. ports that are dependencies of ports that are installed, but inactive):

sudo port uninstall inactive

There are several sections in the MacPorts Guide that also describe the process of using leaves to uninstall unneeded ports.

share|improve this answer
Thanks. This is quite helpful. It seems that you may have to run echo leaves and uninstall leaves back to back multiple times to make sure that all leaves are cleaned. I ran both commands at least 4 times before echo leaves return empty result –  Antony Apr 26 '12 at 20:23
You could say something like while sudo port uninstall leaves; do :; done to automate that. –  Tom Anderson Oct 6 '12 at 10:30
@ChrisJohnsen sudo port uninstall --follow-dependents portname is incorrect, because --follow-dependents will "recursively uninstall all ports that depend on the specified port". The correct option here is --follow-dependencies, which will "recursively uninstall all ports that the specified port depended on. This will not uninstall dependencies that are marked as requested or that have other dependents." –  TachyonVortex Oct 26 '13 at 15:51
add comment

From the docs:

The uninstall action will uninstall an installed port.

%% sudo port uninstall vile Note

To also recursively uninstall the ports that the given port depends on, use the ‑‑follow‑dependencies flag. This will not uninstall dependencies that are marked as requested or that have other dependents.

To recursively uninstall all ports that depend on the given port before uninstalling the port itself, use the ‑‑follow‑dependents flag.

If a port is a dependency of another installed port, uninstall will not remove it unless you remove the dependent port(s) first. To override this behavior, use the -f (force) switch. This will obviously break the dependents. Don't force uninstall ports unless you know what you are doing.

%% sudo port -f uninstall vile

Note the major caveat in the last paragraph. Dependencies are exactly that—dependencies—and different apps can (will) require the same dependencies.

share|improve this answer
I don't think this answers the question. The OP wants to remove a program and the things that it requires, not remove a program and the things that require it. –  user588 Mar 13 '11 at 16:25
@mankoff: ‑‑follow‑dependencies vs. ‑‑follow‑dependents; the quoted passage describes both, but only the first applies to the question –  Chris Johnsen Mar 14 '11 at 6:27
Personally, I think ti is great that Philip explains both parameters, so we are fully aware of the parameters and their implications upon issuing such command –  Antony Apr 26 '12 at 20:40
add comment

The command to remove a port and its dependents is:

sudo port uninstall foo --follow-dependents

However, if the port you are uninstalling has dependents it will not uninstall without the -f (force) flag.

Of course you should start by reading over the MacPorts Guide specifically looking into the Uninstall guide section.

share|improve this answer
add comment

There's also a script in MacPorts' contrib directory called port_cutleaves you can run to remove unneeded dependencies: http://trac.macports.org/browser/contrib/. It'll ask you for each to-be-uninstalled port and allows keeping some, should you want to.

share|improve this answer
add comment

I think the other answers, and the --follow-dependents flag, do not do what you want. If you install A, and A requires B, then thanks to the wonderful package management systems, B will be installed for you. You can uninstall B and the --follow-dependents flag will be aware of and remove A. But what if you uninstall A, which is more likely, because A is what you manually installed in the first place? In this case, B is left behind.

If you use fink, the debfoster package is designed to clean out your system and remove unused dependencies.

Debfoster will help you get rid of packages (libraries for example) get left behind on your system when the program that required it was removed or upgraded to a version that doesn't have the dependency.

While debfoster will not work for MacPorts, there appears to be ports of debfoster, for example, portsfoster, although it seems to be discontinued...

Perhaps no solution currently exists for MacPorts.

share|improve this answer
But isn't debfoster for deb files on Debian based system? How do I use it for MacPorts ports on Mac OS X? –  hpy Mar 13 '11 at 16:44
It has been ported to OS X and MacPorts and fink are based on the apt system which is used by Debian. I don't use MacPorts, but I did a fink install debfoster and it worked just fine for me. –  user588 Mar 13 '11 at 19:08
MacPorts is not related to the apt system and does not use deb files. The idea of MacPorts is related to FreeBSD ports or NetBSD pkgsrc, but the implementation is very different. –  Chris Johnsen Mar 14 '11 at 3:56
OK. Thanks for the correction. Regardless, debfoster has been ported to work fine through fink. Further searching shows that it does not work with MacPorts, but there are clones, for ex, see portsfoster –  user588 Mar 14 '11 at 4:22
add comment

I have written some scripts to automate port cleanup: https://github.com/vasi/macports-tools

The 'macportsfoster' script will output a list of all ports not required by anything in the requested-list, ordered such that 'port deactivate $(macportsfoster)' will remove all of them in one shot.

share|improve this answer
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.