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.


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? – DevNull May 8 '16 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 '16 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. – DevNull May 8 '16 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 '16 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. – DevNull May 8 '16 at 3:16

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.

  • 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" . – Stéphane Gourichon May 27 '18 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. – Karl Richter Jul 13 '18 at 20:13

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.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.