What apt incantations do I need to use to download the source packages for all the installed packages into a directory? (The use case is GPL compliance when giving an installed Ubuntu system to another person along with a computer.)

  • possible duplicate askubuntu.com/questions/28372/… – Tachyons Apr 5 '12 at 13:49
  • 2
    You do not need to bother download all the source code for the person to be GPL compliant. It is enough that the code are publicly available. – Fish Monitor Apr 5 '12 at 15:36
  • fossilet, [citation-needed] with references to both GPLv3 text and GPLv2 text. – hsivonen Apr 20 '12 at 11:29
  • Not a duplicate of the other question, since the other question is about getting the source of a single package. – hsivonen Apr 20 '12 at 11:30

Try this..

Create a directory where you want the source for all installed packages to be downloaded, and enter it.

mkdir source; cd source

Create a file named getsource.sh


dpkg --get-selections | while read line
        package=`echo $line | awk '{print $1}'`
        mkdir $package
        cd $package
        apt-get -q source $package
        cd ..

Make it executable.

chmod a+x getsource.sh

Execute it..


And go grab a cup of coffee :)

  • Glad to be of assistance (: – SirCharlo Apr 20 '12 at 14:32
  • For start thanks for your answer... But I tried to use it on a Debian 9 system and had some problems. Here is the answer that worked for me (unix.stackexchange.com/a/423280/237337). – koleygr Feb 10 '18 at 21:05

An alternative for you might be to just hand out the source CDs:

  • Thanks. I was browsing from releases.ubuntu.com and failed to locate those images. This option, of course, isn't quite correct if you've run the system update after the initial installation. – hsivonen Apr 20 '12 at 11:32

On Ubuntu refer to command:

apt-get source package-name

it is recommended that you only use apt-get source as a regular user, because then you can edit files in the source package without needing root privileges.


There's a couple issues in the accepted answer and with the linked better answer in Unix Stack Exchange. Here's an improved and tested script with comments:


# ${Source} doesn't always show the source package name, ${source:Package} does.
# Multiple packages can have the same source, sort -u eliminates duplicates.
dpkg-query -f '${source:Package}\n' -W | sort -u | while read p; do
    mkdir -p $p
    pushd $p

    # -qq very quiet, pushd provides cleaner progress.
    # -d download compressed sources only, do not extract.
    apt-get -qq -d source $p


Run as non-root user (_apt works). Note any errors as they may indicate packages with no sources available. You may want to run the script with 2>err.log to parse these manually later.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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