How To Find Which Repository A Package Is From On Debian and Ubuntu

I recently had to find which repository held the nginx package so I could enable the corresponding deb-src line in order to build the source code with apt.

There are two ways to get this information. The first is with apt show <PACKAGE> e.g.:

apt show nginx

Then search for the line that begins APT-Sources:

APT-Sources: http://mirrors.digitalocean.com/ubuntu focal-updates/main amd64 Packages

Alternatively, can get the same information by using apt policy:

# apt policy nginx
nginx:
  Installed: (none)
  Candidate: 1.18.0-0ubuntu1
  Version table:
     1.18.0-0ubuntu1 500
        500 http://mirrors.digitalocean.com/ubuntu focal-updates/main amd64 Packages
     1.17.10-0ubuntu1 500
        500 http://mirrors.digitalocean.com/ubuntu focal/main amd64 Packages

Both of these commands tell us that correct repository line in sources.list contains this http://mirrors.digitalocean.com/ubuntu focal-updates/main information.

This tells us that this is the repository line that contains nginx:

deb http://mirrors.digitalocean.com/ubuntu/ focal-updates main restricted

Therefor to grab the source I just need to create a new line:

deb-src http://mirrors.digitalocean.com/ubuntu/ focal-updates main restricted