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.

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


  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


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

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 8 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersncdu pydf
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
8 comments… add one
  • Guntbert Reiter Jul 19, 2020 @ 17:35

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

  • now 3d Jul 19, 2020 @ 19:55

    What about debug symbols? Chromium etc.

    They’re all missing…

  • didou Dec 11, 2020 @ 19:18

    find kali linux 2020.4 debian version !

  • Seamus Dec 19, 2020 @ 10:29

    Hello Vivek,
    I am asking re the Raspberry Pi (a Debian derivative). I understand up to this point:

    sudo apt-get --build source bash

  • Seamus Dec 19, 2020 @ 10:29

    After this step, you go into changes and custom packages – which I am not currently interested in. I only want to build and install a package from source for now. The following two lines are unclear to me:

    1 sudo apt-get -y build-dep bash
    2 debuild

    Re 1- Is this necessary after sudo apt-get –build source bash?
    Re 2- There is no debuild on my system, nor does it show up in apt-cache search… what does this do & where do I get it?

    • 🐧 Vivek Gite Dec 19, 2020 @ 20:17

      that is for downloading source code and rebuild .deb files.

      Do you need to only compile and install bash from source code? If so commands are as follows (make sure you have compilers installed, command is sudo apt-get install build-essential:

      # 1. Download bash source code
      wget https://ftp.gnu.org/gnu/bash/bash-5.1.tar.gz
      # 2. Untar or extract tar ball, run:
      tar xvf bash-5.1.tar.gz
      # 3. The simplest way to compile Bash is to cd into the source code directory using the cd command and then run:
      cd bash-5.1
      # 4. Type ‘make’ to compile Bash and build the ‘bashbug’ bug reporting script too:
      # 5. This will also install the manual pages and Info file:
      sudo make install

      Does that helps? If not please use our forum @ nixcraft.com

  • Seamus Dec 19, 2020 @ 10:30

    Much difficulty entering my question.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum