I-435 near Lackman Rd.
Scott Nazelrod

Compiling from Source on Linux

Why src?

In Linux more and these days you're able to get extra programs through means similar to the ways of Windows users: RPMs, DEBs, and similar files. You also have some slick command line apps like yum (Fedora), apt-get (Debian), and emerge (Gentoo) that download and install the programs for you in one command - superior to even the Windows way.

However, a valuable skill to learn as you get used to Linux is the method of compiling apps from source. Compiling from source is sometimes neccessary:

The Process

Compiling from source is usually a very standardised process which is a lot simpler than it sounds. If you're in a hurry, all you need to know is that you need to do these five steps:

  1. su -
  2. Unzip and untar the source file, and change into the directory contained inside
  3. ./configure
  4. make
  5. make install

And that's it. Simple, yes? That's all you need to know. Below we discuss what you're doing, and why.

How it Works

When you get a source tarball, first you have to unzip and untar it. This usually results in a directory containing the source code and some other files.

One of these other files is called the README file (note the use of caps) and can be viewed from the command line via the less README command. This file usually generally outlines the compile process like we did above. Press q to dismiss this file and return to a prompt.

Let's take a look at the first command: su. This stands for superuser and simply switches to the root user. Input the password when asked for it, and you should be good. Note that the final command, make install, is the only one that actually requires root privlege, but it's more convenient to do the whole process as root.

configure

The next command, ./configure, may seem unfamiliar. configure is another one of the interesting files contained in the source tarball. It's a bash script, a text file with executable permissions. The ./ part of the command indicates for bash to look for the file in the current directory, symbolised by a dot. If the ./ were omitted, bash would look in the default binary folders (called the user's $PATH) rather than the current directory.

The configure script asks for the information necessary for a successful compile. If configure encounters any problems, it will report them to you. Otherwise, the output will end with the line "Good - your configure finished. Start make now" and you will be back at the prompt.

make and make install

Normally, when one is compiling a program that they have written, you invoke the compiler directly, typically using the gcc and g++ commands. However, especially with large projects, typing in all the commands needed to compile the program is unreasonable, and it would be almost inevitable that you'd mistype a command.

make solves these problems. The source package is supplied with a file, called the makefile, which contains the complete roster of compiler commands which need to be executed. All one must do is invoke make and it will locate the makefile and begin feeding commands to the compiler, using the ./configure output as needed.

Makefiles often come in several sections. make install simply instructs make to use the install section of the makefile. This section contains commands that will install the program on your system.

Conclusion

After make install is completed, do which [program-name] to verify that the program was in fact installed correctly. which should respond with a folder path such as /usr/bin. If so, demote yourself back to a regular user (type exit) and you will be able to enjoy your new program!

Valid XHTML 1.1