How to recompile a package from source

  4 years ago

In some cases you might find yourself in need to apply a patch or change a compile time configuration option for a package you installed from the repository. It's easy to do that, as tools are available to download the source of a package and recompile it after you have made the changes you need.


  1. sudo apt-get update
    As always, before you install packages from the terminal do an update first so that your local package cache is in sync with the repository.
  2. sudo apt-get install build-essential devscripts
    Installs the base compiler toolchain.
  3. Open Software Sources from your menu, and make sure "Enable source code repositories" is marked. Then click "Update the cache".
    This allows for downloading source for packages.

Recompiling from source

  1. apt-get source package
    Downloads the source for a package to your current directory. Replace package with the name of the package you want to download the source for. Mind that you must not use sudo!
  2. dpkg-source -x filename.dsc
    Extracts the source. Replace filename.dsc with the name of the downloaded .dsc file. You can look in your file manager, or with a command like ls *.dsc to find that.
  3. cd dirname
    Change directory to the newly created source directory. Replace dirname with the name of the extracted directory. You can look in your file manager, or with a command like ls -l to find that.
  4. sudo apt-get build-dep package
    installs the build time dependencies needed to compile the package from source. Replace package with the same name as used in step 1.
  5. You can now apply any patches you need or change any compile time configuration options. That's a manual step that I can't generalize in this tutorial. Recommend you keep the terminal open on the current directory, and use another terminal if needed for doing modifications.
  6. debuild -us -uc
    Recompiles the package.
  7. cd ..
    Goes one directory up, as here you will find your newly created package.
  8. sudo dpkg -i filename.deb
    Installs the recompiled package. Replace filename.deb with the name of the created .deb file. You can look in your file manager, or with a command like ls *.deb to find that.

Cleanup consists of deleting the downloaded files, the extracted source code directory, and the created recompiled package.

MagicMint 4 years ago

Although I’m using Linux Mint just for the reason of not having to do that on a production machine, this is a _really_ good tutorial :-)

talo 4 years ago

Good tutorial.
Thsank you!