How to get source code of package using the apt command on Debian or Ubuntu

last updated in Categories , ,

I need source code of code of package installed using the apt-get command or apt command on Debian or Ubuntu. How do I get or download source code of package on a Debian or Ubuntu system? How can I download Ubuntu/Debian package’s source code?

Source code is nothing but a text file version of a Debian or Ubuntu software. Usually, we install compiled software using the apt-get command/apt command. However, sometimes we need source code directly written in a programming language. Source code is useful to apply a custom patch or add/remove existing features from software. The first step to do this is to download source code. Let us see how to download the source code (DEB file) using the command line.
Tutorial requirements
Operating system/appUbuntu or Debian Linux
Root privileges requiredYes
DifficultyEasy (rss)
Estimated completion time5m
Table of contents

ADVERTISEMENTS


How to get source code of package using the apt-get/apt command

The procedure to download source code is as follows for Ubuntu Linux.

Please note that apt-get does support third-party closed-source applications. You can only download the source code of open-source software.

Step 1 – Enable source code repo

Sources are normally not installed. Hence, you can only install them if you know the package name and when you enable them. Therefore, edit the /etc/apt/sources.list file, run:
$ sudo vi /etc/apt/sources.list
Make sure the deb-src type references an Ubuntu distribution’s source code as follows:
# Sources specification for the Ubuntu 20.04 LTS distro #
deb-src http://archive.ubuntu.com/ubuntu focal main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu focal-updates main restricted universe multiverse
deb-src http://security.ubuntu.com/ubuntu focal-security main restricted universe multiverse

Where,

  1. deb-src : Indicate that you need source code for .DEB files.
  2. http://archive.ubuntu.com/ubuntu : URL to fetch index and .deb files source code.
  3. focal : Ubuntu Linux 20.04 LTS code name
  4. main restricted universe multiverse : State component name for repo such as main, restricted, universe, and multiverse

Step 2 – Update index

Run the following command to resynchronize the package index files from their sources as defined by the deb-src keyword in /etc/apt/sources.list for Ubuntu Linux
$ sudo apt-get update
# OR #
$ sudo apt update

Step 3 – Download Ubuntu package’s source code

Let us download source code for bash shell, run:
$ sudo apt-get source {pkg1}
$ sudo apt-get source bash

How to get and modify the source code of packages installed through apt-get
You may see the following error if you forgot to set up deb-src as explained in step # 1:

E: You must put some ‘source’ URIs in your sources.list

Step 4 – Understanding downloaded source code files

Let us run the ls command to see source code:
$ ls -l

total 5512
drwxrwxr-x 15 vivek vivek    4096 Jul 19 10:32 bash-5.0
-rw-r--r--  1 vivek vivek   74356 Jun 29 14:43 bash_5.0-6ubuntu1.1.debian.tar.xz
-rw-r--r--  1 vivek vivek    2418 Jun 29 14:43 bash_5.0-6ubuntu1.1.dsc
-rw-r--r--  1 vivek vivek 5554808 Jan 14  2019 bash_5.0.orig.tar.xz

By default, the source code is extracted into bash-5.0 directory:
$ cd bash-5.0
$ ls

The upstream bash source tarball with .tar.xz ending is stored in bash_5.0.orig.tar.xz file. A description file with .dsc ending contains the name of the package, both, in its filename as well as content (after the Source: keyword). A tarball, with any changes made to upstream source, plus all the files created for the Debian package stored in bash_5.0-6ubuntu1.1.debian.tar.xz file. If the --download-only option passed to the apt-get command, then the source package will not be unpacked:
$ sudo apt-get --download-only source source bash
When downloaded, we can extract the source files for bash as follows:
$ dpkg-source -x /path/to/pkg.dsc
$ dpkg-source -x bash_5.0-6ubuntu1.1.dsc

It is also possible to build packages:
$ sudo apt-get --build source {pkg}
$ sudo apt-get --build source bash

Another option is to make changes in debian/rules files
$ vi bash-5.0/debian/rules
Next we can build out custom bash package as follows:
$ export EDITOR=vim
$ dch -n

Make sure we satisfy the build dependencies for a source package and to avoid errors install those libs and tools:
$ sudo apt-get -y build-dep bash
Finally, build a Debian package:
$ debuild
Verify new packages:
$ cd ..
$ ls *.deb

Install them:
$ sudo dpkg -i bash_{version}*.deb

How to download Debian package’s source code

The procedure is the same as Ubuntu distro but URL syntax changes as follows:
$ sudo vi /etc/apt/sources.list
Edit/update as follows:
# source repo for Debian 10 buster #
deb-src deb http://deb.debian.org/debian buster main

Save and close the file in vim, and then run the following command:
$ sudo apt-get update
Let us download source code for the Apache web server:
$ apt-get source apache2

Conclusion

We explained how to enable Ubuntu/Debian source repo and download source code for the package by name. The apt-get source command is useful when you want to study packaging or a specific Debian package. Also useful to know which compile-time options enabled for a particular package. And finally, we can rebuild packages to add or remove components. See Debian guide wiki page for more information and read the following man pages by typing the man command:
$ man apt
$ man apt-get
$ man 5 sources.list
$ man debuild
$ man dch

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.


ADVERTISEMENTS

3 comment

  1. Hi Vivek,
    I really like those “small” tips :thumbsup:
    Hint: in the examples in step 3 you have duplicated the word `source` :-))

Leave a Comment