Download source code of a package in Ubuntu

Published: December 29, 2022

Last updated: December 29, 2022

Download source code of a package in Ubuntu

In this article, we cover how to download source code of a package in Ubuntu using apt. There are largely two methods we can use to download source code of Ubuntu's packages -

  1. Download them through - https://packages.ubuntu.com/. The official website which lets us search and download packages and,
  2. through apt command-line tool.

We prefer to use apt to download source code of packages.

There could be multiple reasons, why you may want to download source code of a package - it could be to apply a patch, add a new feature or remove an existing one.

Note: Following operations require administrative rights. Contact your System Administrator if you don't have one.

Enable source code repository

First enable source code repository in /etc/apt/sources.list file. Use a text editor to edit the file, we have used nano -

sudo nano /etc/apt/sources.list

Remove # from the entry which may look like the one below or for that matter any entry that starts with deb-src which helps you get the source code of a package.

deb-src http://us.archive.ubuntu.com/ubuntu jammy multiverse

deb-src is to get source packages, Debian control files (.dsc) and diff.gz files.

Once we have made changes to /etc/apt/sources.list, issue the following in terminal -

sudo apt update

Download source code of a package in Ubuntu using apt

To download the source code of a package, use the following syntax -

apt source <package-name>

For instance, for featherpad package -

apt source featherpad

It would download relevant files in the current directory. Check for the files -

ls -l

Here, observe that it has also unpacked the source. That is the reason we see a related directory. If you don't wish to get it unpacked then use --download-only option -

apt --download-only source featherpad

In conclusion, we have covered here how to download source code of a package in Ubuntu using apt.