Compile a package from source on Linux

ubuntu_apps_compLinux has many flavours. While it’s a good thing because you can always get the distro that meets your needs an inherent problem is the lack of a global installer format. You have the deb packages on Debian variants, rpm on Red Hat variants, pkg on Arch and so on… But if you are not on one of the major distros or their variants you may often find yourself in a situation that a software is not available in your distro supported format. Fear not!

The best thing about open source is that you can always tinker around with the source code. If you can’t find the package available in the format supported by your distro you can always build it from the source code! There is a benefit too. A package natively built on your system is optimized for running on it. This is the same principle on which Gentoo works. A Gentoo installation may take several hours because it compiles all the packages from the source during the installation. This article will explain the step by step procedure to build and install a package on your distro from its source code. You may need to become the root user to install the package in the default location but not for compilation. For a true taste of the exercise, try compiling the subversion snapshot of the awesome media player MPlayer.

  • Download and extract the source code of the package. Most of the time they are in tar.gz or tar.bz2 archives. If you are not ready for the cmdline yet, your distro’s archive manager can surely handle these formats. Double click to open using the default archive manager and extract anywhere you want.
  • Navigate into the extracted directory and look for the files README or INSTALL. These are the regular instructive files on how to install the package and are a good starting point. If you find nothing of that sort check out the Wiki or Installation instructions in the webpage of the tool.
  • You are likely to find a file named configure.  If not, look for files like or etc. Otherwise you may need to run autoconf followed by automake. You can learn more on how they work here or from this procedural explanation.
  • By this time you should have the configure file generated. Run
    $ ./configure

    In case you do not have root permissions, you can install the package to under any directory as the root directory. Run

    $ ./configure --prefix=/path_to_directory

    configure supports many options. Run

    configure --help

    for all the options. configure will show you any missing dependencies on your system. As you are building the package from source, you need to have the source code for other packages or libraries on which it is dependent on. You can find them in your system’s default package manager. If you are not sure which package to install, try Google. Many have tried it before and many were puzzled, trust me!

  • The next step is
    $ make

    This is where the actual compilation takes place. In case you hit any issues, the best option is to contact the package maintainer for support as it means the package is broken on your distro. make --help shows you options supported by make.

  • If make is successful, you need to install the compiled package on your system as thefinal step. Run
    $ sudo make install

If everything goes fine, you have the package installed on your system!

Further reference: The GNU configure and build system

One thought on “Compile a package from source on Linux”

Leave a Reply

Your email address will not be published. Required fields are marked *