Picture this scenario. You've installed some Debian packages on your system. And you want to install the same packages on another system. But you lost the binary archives i.e. the deb files. Would you download them again from their official website or repository? What if the particular version of a specific package is no longer available elsewhere? No worries! Since they are already installed on your system, you can easily recreate the .deb files. Say hello to dpkg-repack, a commandline Debian package archiving tool to recreate Debian binary packages that are already installed on your system. To put it succinctly, dpkg-repack utility can able to create a .deb file out of a debian package that has already been installed on your system.
The dpkg-repack utility can be helpful in many situations where you want to,
- recreate .deb files from existing installed debian packages,
- easily copy packages from one system to another,
- save the current state of a package before upgrade it,
- backup a specific version of a package for future use,
- save specific packages which are no longer available for download.
Install dpkg-repack in Debian, Ubuntu and derivatives
The dpkg-repack is available in the default repositories of Debian, Ubuntu and its variants. To install dpkg-repack, simply run the following command:
$ sudo apt install dpkg-repack
How recreate Debian binary packages with dpkg-repack
For example, a while ago I installed Microsoft Teams application on my Ubuntu desktop.
$ dpkg -l | grep teams
ii teams 1.3.00.5153 amd64 Microsoft Teams for Linux is your chat-centered workspace in Office 365.
I no longer have the teams.deb file. I deleted it right after installing the Microsoft Teams application. I can go to the official download page and download it again. But why should bother downloading it over and over? I can now simply recreate the teams.deb file from already installed teams package with dpkg-repack utility like below:
$ dpkg-repack teams
dpkg-deb: building package 'teams' in './teams_1.3.00.5153_amd64.deb'.
As you see, the above command recreated the "teams_1.3.00.5153_amd64.deb'" file from the Teams package that is already installed on my Ubuntu desktop. The newly created deb file will be saved in the current working directory.
Similarly, you can convert any installed Debian packages back to .deb packages using dpkg-repack utility.
It is also possible to recreate multiple deb files with space-separated:
$ dpkg-repack <package1> <package2> <package3>
If you can't rebuild the deb file due to any permission issues, try from fakeroot environment like below:
$ fakeroot -u dpkg-repack teams
One interesting feature of dpkg-repack utility is if any changes have been made to the package while it was unpacked (i.e. files in /etc modified), the new package will inherit the changes.
Viewing the contents of deb file
You can view the contents of that deb file using command:
$ dpkg --contents teams_1.3.00.5153_amd64.deb
drwxr-xr-x root/root 0 2020-05-26 15:15 ./ drwxr-xr-x root/root 0 2020-05-26 15:15 ./usr/ drwxr-xr-x root/root 0 2020-05-26 15:15 ./usr/bin/ -rwxr-xr-x root/root 286 2020-02-20 16:46 ./usr/bin/teams drwxr-xr-x root/root 0 2020-05-26 15:15 ./usr/share/ . . . drwxr-xr-x root/root 0 2020-05-26 15:15 ./usr/share/teams/swiftshader/ -rwxr-xr-x root/root 286264 2020-02-20 16:46 ./usr/share/teams/swiftshader/libEGL.so -rwxr-xr-x root/root 2677296 2020-02-20 16:46 ./usr/share/teams/swiftshader/libGLESv2.so -rwxr-xr-x root/root 114964776 2020-02-20 16:46 ./usr/share/teams/teams -rw-r--r-- root/root 1040912 2020-02-20 16:46 ./usr/share/teams/v8_context_snapshot.bin
The dpkg-repack comes in handy when installing the same set of packages on multiple systems, just transfer them using external hard drive or via LAN to other systems and install them using dpkg command:
$ sudo dpkg -i <package-name>
Please note that you still need to download the dependencies. To install the dependencies, run:
$ sudo apt-get -f install
Alternatively, you can find the dependencies of the package as described in the following link:
And then recreate all of them using dpkg-repack as described above.
This will also help when you want to roll-back a package to the previous working version. For instance, let us say you just upgraded Vim package to new available version. If the new version is not working as expected, you can simply roll-back to the old version which you created earlier using dpkg-repack.
For more details, refer man pages:
$ man dpkg-repack