How to limit download speed for apt with trickle

I think many a Debian user has felt the need to somehow tell apt to stop hogging all the bandwidth during one of those long dist-upgrade’s.

Unfortunately, apt doesn’t feature a built-in bandwidth limiter. Fortunately, a userspace bandwidth shaper exists, and it’s called trickle.

If you run a command from the console through trickle, it will preload the network socket-related libraries for that application with its own, and will enforce a maximum download and upload rate.

Example:

trickle -s -d 20 apt-get dist-upgrade

…and now apt-get will kindly stick to a 20 KB/s download rate, give or take 1 KB/s. Very nice. Now I can finally let dist-upgrade do its job without having to stop using the Internet for the duration.