How to Compile and Install from Source Code

This post covers how to compile and install a program from source code. First up, you will need to get the package build-essential. This package is a list that contains a few essential items for compiling from source, namely package development tools dpkg-dev, the g++ compiler, a few libraries and the make program.

To install build-essential, first update your repository lists then issue the install command:

sudo apt-get update
sudo apt-get install build-essential

Note that some people suggest you need linux-headers-<version>-generic. Unless you plan on compiling modules (drivers) that link with the Linux kernel, you do not need these.

Once you have the package above installed, you’ll need to download the source code for the package you wish to compile and install. Typically, this file will take the form of filename.extension where the extension is usually either tar, tar.gz or tar.bz2

To extract the files contained within, you’ll issue one of the following commands depending on the compression type used (as indicated by the extension).

.tar

tar xvf filename.tar

.tar.gz

tar xvzf filename.tar.gz

.tar.bz2

tar xvjf filename.tar.bz2

Just in case you’re wondering, to create the files, with their respective compression scheme, you simply replace the x with a c as shown below:

tar cvf filname.tar /files/to/include
tar cvzf filename.tar.gz /files/to/include
tar cvjf filename.tar.bz2 /files/to/include

Before I go further, I want to show you a way to log the messages that flash across your screen at the speed of light. Simple substitute the command you are issuing in place of <command>. The results, including standard error, will be written (in this case) to a file in the current directory called compile.log.

( <command> 2>&1 | tee compile.log && exit $PIPESTATUS )

With the files extracted, you’ll want to change into the directory you just created and look for a file called something similar to “README” or “INSTALL”. Any directions contained in any of those files will supersede (or augment) the remaining instructions below. Most packages will probably have a file called configure, but not all will. If it doesn’t, simple skip this next step, otherwise, issue the following command to resolve any dependencies needed:

./configure

When that is finished, run the command

make

If you have a multiple core machine (which you probably do), and want to take advantage of that, you can do so by passing the number of cores to use. The below example uses 4 cores.

make -j4

If that completes with no errors (which it generally should if the package is well maintained and you didn’t have any missing libraries, tools, packages, etc. from the ./configure step), run the command

sudo make install

To actually install the package binaries to your system.

A couple of further notes:

1. Any programs installed this way will not be updated by running the commands

sudo apt-get update
sudo apt-get upgrade

In order to update the program you will need to do so manually, which will typically involve uninstalling and reinstalling the new package, which brings me to my second note:

2. Checkinstall is a very helpful utility. What it does is build a .deb, .rpm or Slackware package from a program’s source code. Installation and conversely uninstallation takes place through your distro’s package manager software.

Fortunately, using checkinstall is very easy. First you install the program:

sudo apt-get update
sudo apt-get install checkinstall

Then run through the standard commands use to compile and install source code:

./configure
make

But instead of issuing the command

sudo make install

You issue the command

sudo checkinstall

which automatically installs the program and creates (assuming you are using Debian or one of its derivatives) a file called <package_name>.deb. At a later time if you wish to remove the package instead of hunting around to manually find all the files placed by the installer, you can simply use the command below instead.

sudo dpkg -r package_name
Tagged , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published.

Protected by WP Anti Spam