Header Ads

How to Compile Software From Source Code in Linux


Ever been too scared to install a program that wasn’t an RPM or DEB?

In Windows, installing software is a matter of clicks. In Linux, Depending on the distribution you have, software can be downloaded in the form of either RPM or Deb packages. Again, you can compile software directly from source code—download the source code which comes in a tarball, unzip it and then compile it.

In fact, this is the way source packages were distributed in the old days, and you might still have to go by this route in some cases. However, to most people out there, compiling from source still feels like voodoo. Here is a quick guide to all that you need to know about compiling from source and what goes behind the scenes, without leaving anything to chance.

Unpacking
Command to use: [tar xvzf mypackage.tar.gz] or [tar xvjf mypackage.tar.bz2]

This is the first thing to be done when you download the software. All the source files, associated libraries and documentation are distributed as compressed archives called tarballs. They are compressed using either gzip or bzip2, and hence the different extensions and the slightly differing switches used in the command.

After unpacking, a directory will be created with the name of the package in the destination folder. Change the directory using cd mypackage and then use ls to explore the directory tree. Make sure to read the readme, install and other documentation. Some packages might need some additional libraries or might suffer from dependency issues, so it makes sense to know what’s needed.

Configuring
Command to use: ./configure

After you have unpacked the tarball and have also solved any dependency issues by installing required libraries it’s time to go to the next step: configuration. You have to run the command while inside the installed package directory. This command does not change anything substantially. It basically does a house-keeping job, checking whether all the required resources in the form of system libraries are present and then assigning values for system dependent variables. Various switches can be used along with the ./configure command to change the behaviour of the program.

For example, appending -quiet would stop printing the checking… messages during the configure process. If you know what you are doing you can use -no-create to inspect the output files before they are created. Using -prefix=mydirectory you can change the path where the Makefile will be created. The sample output of a configure command will look something like this:

creating cache ./config.cache
checking for extra
includes… no

checking for extra libs… no
checking for a BSD compatible
install…/usr/bin/install -c
checking whether build envi-
ronment is sane… yes
checking whether make sets
${MAKE}… yes
checking for working
aclocal… found
checking for working auto-
conf… found
checking for working
automake… found
checking for working auto-
header… found
checking for working make-
info… found
checking for a C-Compiler…
checking for gcc… gcc

creating Makefile
creating kdevelop/Makefile
creating po/Makefile
creating kdlgloader/Makefile

creating config.h

After the ./configure command has run—during which you will see a bunch of messages scrolling up the screen in rapid fire sequence—a Makefile will be created. This Makefile is then used to build the binary which then needs to be installed.

Building
Command to use: make

The make command uses the Makefile to create installable binaries. Binaries are the Unix equivalent of executables, or .exe files. The make command is time consuming and results in a whole bunch ofmessages scrolling across your screen. This part is going to take a lot of time, depending on the package being compiled as well as the system configuration. There will be another bunch of messages scrolling across the screen, sometimes with warnings about some resource being absent. If all is okay, it will display the command prompt. If however, there is some problem it prompts you with appropriate statusmessages.

Most of the errors in the compilation process are due to missing or incompatible libraries. Say you have a software that depends on GTK+, with the latest version not present. You might then have to download it from the Web. For the most part, if your OS is new you won’t have any problems. However,you can always search software repositories provided by your OS vendor. Look for development versions which end with –devel.

Installation
Command to use: make install

The make install command is the equivalent of point and click routine on Windows. The installation time will again depend on how big the software is. Before doing this, you need to log in as root. Since you have followed the best practices and have up till now done everything from a user account type su (sudo for Ubuntu) and enter the root password. After getting administrator privileges use this command to install the software.

You will have no glitches and every thing will work out fine. Don’t forget to log out by using exit when you are done. The program will be usually installed in /usr/local/bin. However, if you have specified a path during the configuration process, you will have to navigate to that directory to access the program. In most modern Linux distributions, you will see a graphical shortcut and will have to click there to launch the program.

It’s that simple, really. Oh, and if you need to uninstall the program just delete the binaries. In some cases, you could use the make uninstall command to uninstall the program. Get cracking..!