I wrote a couple of scripts to help users pull their current kernel source package, apply some patches of mine, and build it as a .deb package with make-kpkg. One of the few steps that requires manual intervention is enabling the "Source" downloads via:

  • "Ubuntu Software Center"
    • Edit...
      • Software Sources...
        • "Source Code"

enter image description here

Is there a way to do this automatically via the command line? It's to my understanding I could just un-comment the first few "deb-src" lines in /etc/apt/sources.list, like so:

# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
# newer versions of the distribution.
deb http://ca.archive.ubuntu.com/ubuntu/ trusty main restricted
deb-src http://ca.archive.ubuntu.com/ubuntu/ trusty main restricted

## Major bug fix updates produced after the final release of the
## distribution.
deb http://ca.archive.ubuntu.com/ubuntu/ trusty-updates main restricted
deb-src http://ca.archive.ubuntu.com/ubuntu/ trusty-updates main restricted

This is somewhat error-prone though, given the sed scripts I've written don't necessarily work if the sources are out of the default order, or various other reasons. More importantly, I'd like this to work on Ubuntu 14.04, Ubuntu 16.04, etc (ie: as generic as possible).

Are there any command-line apt-related tools to accomplish this via a shell script?

Thank you.


4 Answers 4


In the end, I just enabled all the src repos.

sed -i '/^#\sdeb-src /s/^#//' "/etc/apt/sources.list"

This doesn't require upgrading the OS to an arbitrary version.

  • 1
    This variant will do the job while keeping the deb-src lines nicely aligned on the left: sed -i '/^#\sdeb-src /s/^# *//' "/etc/apt/sources.list" . May 27, 2018 at 20:58
  • Is there a reason why '' and "" are used. I know that it can make a difference in shell scripts. Is it possible to use one of them only? That would make it easier to use the snipplet when passing it as command argument, e.g. to docker run. Jul 13, 2018 at 20:13

I had a similar problem as you had and finaly I started doing the following script, to write a temporary sources.list and later removing it again:

# making a copy of the users original sources.list file
cp -v /etc/apt/sources.list /etc/apt/sources.list.orig &&

# writing a custom sources.list file
cat > /etc/apt/sources.list << "EOF"
deb-src http://archive.ubuntu.com/ubuntu xenial main restricted
deb http://archive.ubuntu.com/ubuntu/ xenial main restricted
deb-src http://archive.ubuntu.com/ubuntu/ xenial multiverse main universe restricted
deb http://archive.ubuntu.com/ubuntu/ xenial-updates main restricted
deb-src http://archive.ubuntu.com/ubuntu/ xenial-updates multiverse main universe restricted
deb http://archive.ubuntu.com/ubuntu/ xenial universe
deb http://archive.ubuntu.com/ubuntu/ xenial-updates universe
deb http://archive.ubuntu.com/ubuntu/ xenial multiverse
deb http://archive.ubuntu.com/ubuntu/ xenial-updates multiverse
deb http://archive.ubuntu.com/ubuntu/ xenial-backports main restricted universe multiverse
deb-src http://dearchive.ubuntu.com/ubuntu/ xenial-backports main restricted universe multiverse
deb http://archive.canonical.com/ubuntu xenial partner
deb-src http://archive.canonical.com/ubuntu xenial partner
deb http://security.ubuntu.com/ubuntu xenial-security main restricted
deb-src http://security.ubuntu.com/ubuntu xenial-security multiverse main universe restricted
deb http://security.ubuntu.com/ubuntu xenial-security universe
deb http://security.ubuntu.com/ubuntu xenial-security multiverse

# running an update
apt-get update &&
apt-get -y dist-upgrade &&


# putting back the original sources.list
mv -v /etc/apt/sources.list /etc/apt/sources.list.tmp &&
cp -v /etc/apt/sources.list.orig /etc/apt/sources.list &&

# removing backup files
rm -rfv /etc/apt/sources.list.tmp &&
rm -rfv /etc/apt/sources.list.orig
  • What if I'm not on Xenial, but on Trusty/(Wily, Vivid, etc), or Precise?
    – Cloud
    May 8, 2016 at 3:01
  • Well take your source file remove the lines with a # in front and replace the ones in my answer with them.
    – Videonauth
    May 8, 2016 at 3:02
  • I could run a sed/bash regex easily enough to remove all leading # characters, but should I necessarily do that? Some of those sources in the list file enable sources that are either unsupported or bring licensing concerns into the fray. An enable-all approach seems a big hammer to throw at the problem.
    – Cloud
    May 8, 2016 at 3:05
  • Youre aware of the last step i have in my script approach? i remove my temporary sources.list and copy back the one the user had.
    – Videonauth
    May 8, 2016 at 3:08
  • Yes. You're upgrading to the latest release, when all I want is to enable the repo for source for the latest release currently installed on the system.
    – Cloud
    May 8, 2016 at 3:16

I've put together a perl one-liner that will uncomment only the deb-src lines that are preceded by an uncommented deb line:

perl -pi.orig -0e 's/^(deb .*\n)# (deb-src)/$1$2/mg' /etc/apt/sources.list

It will also keep the original one as /etc/apt/sources.list.orig. (so you can restore it later if you want)

This should work across releases, assuming they continue their habit of putting commented-out deb-src lines right after their corresponding deb lines.


Connecting various pieces together, I came up with an easily reversible solution for enabling and disabling source downloads.

Enable source

From cloud and Stéphane Gourichon's solution, with sed modified to ignore non-matching lines and the output redirected with sudo to a temporary file under sources.list.d/:

sed -e '/^#\sdeb-src /s/^# *//;t;d' "/etc/apt/sources.list" \
| sudo tee /etc/apt/sources.list.d/source-repos-tmp.list > /dev/null

Disable source

sudo rm /etc/apt/sources.list.d/source-repos-tmp.list

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .