Home > Articles > Operating Systems, Server > Linux/UNIX/Open Source

  • Print
  • + Share This
This chapter is from the book

Compiling Software from Source

Compiling applications from source is not that difficult. There are two ways to do this: You can use the source code available in the Ubuntu repositories, or you can use source code provided by upstream developers (most useful for those projects that are not available in the Ubuntu repositories). For either method, you need to install the package build-essential to ensure that you have the tools you need for compilation. You may also need to install automake and checkinstall, which are build tools.

Compiling from a Tarball

Most source code that is not in the Ubuntu repositories is available from the original writer or from a company’s website as compressed source tarballs—that is, tar files that have been compressed using gzip or bzip. The compressed files typically uncompress into a directory containing several files. It is always a good idea to compile source code as a regular user to limit any damage that broken or malicious code might inflict, so create a directory named source in your home directory.

From wherever you downloaded the source tarball, uncompress it into the ~/source directory using the -C option to tar:

matthew@seymour:~$ tar zxvf packagename.tgz -C ~/source
matthew@seymour:~$ tar zxvf packagename.tar.gz -C ~/source
matthew@seymour:~$ tar jxvf packagename.bz -C ~/source
matthew@seymour:~$ tar jxvf packagename.tar.bz2 -C ~/source

If you are not certain what file compression method was used, use the file command to figure it out:

matthew@seymour:~$ file packagename

Now, change directories to ~/source/packagename and look for a file named README, INSTALL, or a similar name. Print out the file if necessary because it contains specific instructions on how to compile and install the software. Typically, the procedure to compile source code is as follows:

matthew@seymour:~/source/packagename$ ./configure

This runs a script to check whether all dependencies are met and the build environment is correct. If you are missing dependencies, the configure script normally tells you exactly which ones it needs. If you have the Universe and Multiverse repositories enabled in Synaptic, chances are you will find the missing software (usually libraries) in there.

When your configure script succeeds, run the following to compile the software:

matthew@seymour:~/source/packagename$ make

And finally, run the following:

matthew@seymour:~/source/packagename$ sudo make install

If the compile fails, check the error messages for the reason and run the following before you start again:

matthew@seymour:~/source/packagename$ make clean

You can also run the following to remove the software if you do not like it:

matthew@seymour:~/source/packagename$ sudo make uninstall

Compiling from Source from the Ubuntu Repositories

You might sometimes want to recompile a package, even though a binary package is available in the Ubuntu repositories. For example, a program might have been compiled into a binary with a specific feature disabled that you would like to use. Here is how you can do this. We will call the software package we want to compile foo.

First, get the source from the Ubuntu repositories:

matthew@seymour:~$ apt-get source foo

Install the build dependencies for the package:

matthew@seymour:~$ sudo apt-get build-dep foo

Change to the directory for the source code (may include the version number):

matthew@seymour:~$ cd foo-4.5.2

Make whatever changes you want to make to the package or to the compilation flags. You can do this using ./configure and make, or sometimes by making manual changes to a configuration file. Each package has the potential to do this differently, so you need to see that program’s documentation. Try looking for a README file in the source code to get started.

Next, create a new debian/changelog entry. After you enter this command, you need to enter a message that tells why a new version was made, perhaps something like Matthew’s flight of fancy with extra sauce.

matthew@seymour:~$ dch -i

Build the source package. This creates all the files necessary for uploading a package:

matthew@seymour:~$ debuild -S

Finally, you are left with a foo-4.5.2-1ubuntu1custom.deb package (using whatever version number or suffix you created earlier) that you can install, and later uninstall as well, using your package manager. In some instances, multiple DEB files might be created, in which case you would replace the individual package name in the example here with *.deb.

matthew@seymour:~$ sudo dpkg -Oi foo-4.5.2-1ubuntu1custom.deb
  • + Share This
  • 🔖 Save To Your Account