Download source code of installed packages

Asked by Wolf-Martin Rasenack

I need to do a license clearing of an installed Ubuntu system (16.04.6) and for this I need the source code of every component which is installed in my system. I can get a list of the packages with "apt list --installed" and this list is a little bit more than 2000 lines long.
Is there a way to download the very specific source code for everything in my system - especially the correct version?

I have tried generating links to the lauchpad pages and downloading those via wget but this will still result in a big amount of manual work.

Most probably it will not really help to use the official source code iso images (I have only found 16.04.4 but not 16.04.6), because some packages may have been updated.


Question information

English Edit question
Ubuntu Edit question
No assignee Edit question
Solved by:
Wolf-Martin Rasenack
Last query:
Last reply:
Revision history for this message
actionparsnip (andrew-woodhead666) said :

You can feed the output of dpkg to apt-source. Something like

dpkg -l grep -v ^rc | awk {'print $2'}

Will show all installed packages. You can then pipe that into apt to pull source for all packages. It'll be a lot of data and take a long time to download.

Revision history for this message
actionparsnip (andrew-woodhead666) said :
Revision history for this message
Wolf-Martin Rasenack (martinator) said :

thanks for now, I'll try it out in three hours - I suppose it won't work for my goal to get a very specific version of the sources - it will retrieve the latest... but I'll try it.

Revision history for this message
Wolf-Martin Rasenack (martinator) said :

short comment to the command above: there mus be an additional "|" before "grep":
dpkg -l | grep -v ^rc | awk {'print $2'} | apt-get source
That works great for getting the sources.
I'll have to try for my final system which is a little outdated (xenial) if it really retrieves the correct version (unfortunately it is within a docker and I have figure out how to best access internet with it).

Thanks again for the quick help!!

Revision history for this message
actionparsnip (andrew-woodhead666) said :

The grep -v ^rc removes uninstalled packages

Revision history for this message
Wolf-Martin Rasenack (martinator) said :

Just a question with an example (with a newer Ubuntu version):
Courrently rsync rsync 3.1.2-2.1ubuntu1.1 is installed and could be updated to 3.1.2-2.1ubuntu1.2
If I now fetch the sources via apt-get source apt - which version does fetch the sources for?
Just in case it is not such a minor change in version - which source version will it fetch?


Revision history for this message
Manfred Hampl (m-hampl) said :

from man apt-get:
source causes apt-get to fetch source packages. APT will [...] download into the current directory the newest available version of that source package [...]

To force downloading the source of the version that you currently have installed, you might try adding the version number to the command, probably something like
dpkg -l | grep -v ^rc | awk {'print $2=$3'} | xargs apt-get source

Remark: In case that a certain package cannot be downloaded any more, because it stems from a foreign source or older release, you will most probably not be able to download its source. You might check with "ubuntu-security-status" or "ubuntu-support-status" (depending on your release) whether all packages can still be downloaded.

Remark: rsync 3.1.2-2.1ubuntu1.2 currently is available in bionic-proposed. Are you aware of the potential implications with the -proposed repository bucket (software is still in testing process)? There is the recommendation to enable -proposed only temporarily for testing a single package and disabling it again immediately afterwards.

Revision history for this message
Wolf-Martin Rasenack (martinator) said :

Thanks a lot for your answers and ideas!
First to the implemntation of the above proposal:
dpkg -l | grep -v ^rc | awk {'print $2=$3'} | xargs apt-get source
It must be
dpkg -l | grep -v ^rc | awk {'print $2 "=" $3'} | xargs apt-get source
so that awk concatenates the strings properly and it results in a good command for apt-get.
Next problem: dpkg outputs 5 or 6 lines at the beginning which have nothing to do with a package, so apt-get cannot deal with that, so I first wrote that into a text file, deleted these lines and then made a
less myfile.txt | xargs apt-get source
and that started in principle OK - buuuttt...
(I have a German linux version, translated it to english:)
E: the version »0.6.45-1ubuntu1.3« of package »accountsservice« cannot be found
E: Source package for accountsservice cannot be found.
So that won't work and most probably also for most other packages (and yes, I enabled all deb-src lines in the /etc/apt/sources.list).

What I meanwhile did, is I crawled the sites for the needed packages, downloaded the links to the relevant source pakages and downloaded those. It's quite some scripting work and network load but I meanwhile succeeded to get the very right source packages.