Ubuntu – Using sudo apt-get install, why no source code

aptpackage-managementsoftware installationsource code

I was trying to install a package for robotic operational system (ROS). And I just go ahead and type sudo apt-get install <package>

After installation, I see a list of files in /opt/share folders, which are the packages I want.

But if I go into src, the source code (.cpp files) aren't there.

I can see the source code if I go to developer's github pages, but how come the package is installed without using the source code? How can I download the source code and install the package using apt-get install command?

Best Answer

By default apt-get install does not download the source codes of packages. There is a specific apt-get option to do that which is source.

From man apt-get:

source source causes apt-get to fetch source packages. APT will examine the available packages to decide which source package to fetch. It will then find and download into the current directory the newest available version of that source package while respecting the default release, set with the option APT::Default-Release, the -t option or per package with the pkg/release syntax, if possible.

Source packages are tracked separately from binary packages via deb-src lines in the sources.list(5) file. This means that you will need to add such a line for each repository you want to get sources from; otherwise you will probably get either the wrong (too old/too new) source versions or none at all.

So the syntax to download the source package of something to the current directory would be (no need to be root):

apt-get source <package_name>

Also note that as the man pages says you need to have the deb-src repositories enabled for all the regular deb repositories you want to keep track of source packages. The simplest way to do that would be to just add -src to the original repo's initial deb string:

deb http://archive.ubuntu.com/ubuntu/ trusty main restricted
deb-src http://archive.ubuntu.com/ubuntu/ trusty main restricted

On a different note, you might have checked the /usr/src directory which is the designated place for all kernel sources.

Related Question