How to download a Debian package's source code?

Downloading source code from Debian repositories is as simple as running apt-get source <PACKAGE>. It will put 3 files in your current directory, .dsc that describes package, .orig.tar.gz that contains unmodified sources, and .diff.gz that contains Debian-specific changes. But if you get error E: Unable to find a source package for <PACKAGE>, then you might not have the source code repository specified, so go to /etc/apt/sources.list (or some file in /etc/apt/sources.list.d/ that contains your Debian repository), find a string like:

deb ftp://ftp.debian.org/debian wheezy main contrib non-free

Change deb to deb-src:

deb-src ftp://ftp.debian.org/debian wheezy main contrib non-free

Update package index files with sudo apt-get update and try again. See also:

  • The difference between deb versus deb-src in sources.list
  • What is a Debian package @ The Debian GNU/Linux FAQ
  • How do I install a source package @ The Debian GNU/Linux FAQ

A second possibility is to download the source code from upstream directly instead of downloading it from Debian repositories. This has the advantage that if you not only want to read the source code but might like to change something you can directly commit and submit it to upstream (assuming it is not a Debian patch).

You can usually find out the upstream source code repository URL in the file /usr/share/doc/$package_or_program_name/copyright.

$ head /usr/share/doc/git/copyright

Format: http://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
Upstream-Contact: [email protected]
Source: https://www.kernel.org/pub/software/scm/git/

Files: *
Copyright: © 2005-2014, Linus Torvalds and others.
License: GPL-2

Files: xdiff/*
Copyright: © 2003-2009, Davide Libenzi, Johannes E. Schindelin

This file is also referenced from packages.debian.org (search for "Copyright File").