Monday, November 10, 2008

Cleaning up MacPorts


I am running MacPorts on my laptop. Unfortunately, the laptop have a limited amount of space on the hard drive, so I constantly have to keep trimming away things that I do not need.

I noticed that my MacPort folder was growing quite a bit, and I wondered if it would be possible to trim what was in the MacPorts folder without removing any of the applications? After searching a little on the net, I discovered a few interesting things.

It seems as though MacPorts, when upgrading to a new version of an application, leaves the old version behind. After a while, this will eat up quite a bit of your precious hard disk space. So in order to see what you have installed on your machine, type in the following:

port installed

There will probably be a lot of ports installed, and only the ones marked "(active)" are used. The rest may be removed. Before doing so it might be fun to see just how much space MacPorts is using. I run the command

du -sh /opt

This will give you an aproximate value for the disk space occupied by MacPorts. On my machine (which already have been cleaned once) it reports 2.5G. The next step is now to remove all the old versions. I would start out by doing a "clean" for all the installed ports. You do this by typing

sudo port clean --all installed

This erases all temporary files in that are being generated during a build of an application. Only things that can be rebuilt are removed in this step. For fun you could run the "du" command again, to see what your savings are. It might not be much at the moment, but on my system I now ended up with 2.7G, a saving of 200MB.

Now for the actual removal of the old versions. This is accomplished by this command:

sudo port -f uninstall inactive

My first attempt was a bit longer, but it also did the trick:

port installed | grep -v "(active)" | while read i; do sudo port -f uninstall $i; done

This will give you an error message as the first line of output is "The following ports are currently installed:", and since there are no port with that name, it will fail, but continue. This is harmless, and I decided that creating a fix for this would make the command line more complicated that necessary. Anyway, after running the first command, the "du" command showed my /opt directory to be 2.6G. I had run this series of commands before, so my savings were not that grate this time around, but the first time I saved almost 1.5G.

I hope this was to some help for some of you.

20 comments:

Simen said...

After first publishing this post, I have been updating macports regularly. I just did a clean-up again, and removed 1.1G of inactive ports. So ports may really eat up your HD-space.

Anonymous said...

Great instructions every 6 months I decide to clean up my MacPorts and have to spend a couple hours reading through the man page. Now I just added a new bookmark.

Pete said...

Thanks for the instructions. The "installed" bit is missing from the MacPorts docs.

Matthew Platte said...

Ha! I just did a rude ruby script because the "installed bit is missing from MacPorts docs." Having read your article and comments, I guess I've cleaned up about as much as possible. Thanks.

fh.each {|line| system("port uninstall #{line}")}

Matt said...

THANK YOU!

123 123 said...

Nice post you got here. It would be great to read more about this matter.
BTW check the design I've made myself Young escorts

sundar said...

3GB resurrected :) Thanks for this.

peppoj said...

Can i make these commands to run automatically? Like every week?

Adler said...

Thank you, dude.

Anonymous said...

I saved 1.1GB, thanks

Anonymous said...

You can also take a look on the /opt/local/var/macports/distfiles directory as it contains the source tarballs used to build the macports softwares.
Unless you want to recompile some of your softwares with different options/variants afterward, offline or without redownloading it again, these subfolders of distfiles can be removed.

orion said...

to clean up all:

sudo port -f -p clean --all all

Also, if you use ports a lot, you might find Porticus interesting. Porticus is a GUI for the MacPorts package manager

http://porticus.alittledrop.com/

Diego said...

Great post!! Thanks :D

Diego said...

Great post!! Thanks a lot :D

Jeff (彭智寧) said...

To nuke inactive ports, the following is a bit shorter:

$ sudo port -u uninstall

You could also include -u while upgrading to do the cleanup without waiting until you get a pile of packages.

aver said...

To clean more space on MacOS you may also use CleanMyMac app (http://macpaw.com/cleanmymac/) or Snow Leopard Cache Cleaner

Niels R. said...

Thanks! Really helpful post (and comments)!

Diskspace on a SSD is almost as valuable as the KBs years ago on a XT :o)

Kyle said...

It's little posts like this that add up in a big way. I reduced my /opt footprint from 13 GB to 3.2 GB, thanks to these two commands. Thanks!

Randolph said...

Thanks! This gave me back 1.6GB.

Brian said...

THANKS HEAPS FOR 3GB!!! :-)