Want to learn how to compile Linux programs from source? Then read on! For some reason, the prospect of having to compile Linux programs from source scares many Linux users – even those who have been using the operating system for years. But it need not scare them. Installing software from source code files is a simple operation in most cases. And if you learn the basic techniques, then you’ll soon be able to diagnose the problems that occur on those occasions when things don’t proceed as planned.

Steps Required to Compile Linux Programs from Source

To compile Linux programs from source, use a four-step process:

1. Unpacking the Source Code

In this example we’re going to compile the SQLite database. If you want to follow along, download the latest source code tarball (a .tar.gz file).

A tarball is just a compressed file, very similar to a .zip file. It will either have the extension tar.gz or .tgz. To work with its contents you must first extract them using the following command: Sometimes you will have a tar.bz (or tar.bz2) file. Use the following command if you’re working with a tar.bz file: This process creates a directory with the same name as the file.

2. Resolving Dependencies

Enter this new directory using cd directory name and then, as sudo (or su in many other Linux distributions), execute the ./configure command:

The ./configure command checks to see if all the software that this particular program relies upon – such as an appropriate compiler – is installed. The ./ prefix tells Linux to look for the configure file in the current directory and execute it. Note that sometimes configure is not the name of the file that resolves these dependencies. If you get an error when running configure look in the directory for a “README” or “INSTALL” file or similar. That should tell you which file is responsible for this step. In fact it’s a good idea to read any included files before attempting to compile any program. If everything goes well, you won’t see any errors. We were lucky here. Usually, though, you will be missing something. Just examine the output and install any missing dependencies using your package manager. Run ./configure again until you don’t see any more errors.

3. Compilation

Once you have resolved all the dependencies, you must compile the program. Use the make command to do this:

This process can take a few minutes and substantially longer for some programs. Make sure that the output does not display any errors before continuing.

4. Installation

At this point you have compiled the binaries, but now you need to install them. Just execute make install. This step moves all the binaries into their correct location on your system so that your program is ready to use:

If you have been following along, execute sqlite3 at a terminal prompt, and if the database has been installed correctly, you should see the sqlite3 database prompt.

Congratulations! But what if you want to remove the program you just installed? That’s simple. Just visit the directory you installed the program from and execute: Followed by:

Conclusion

It’s easy to install most programs from source in this way, but some require slightly different steps from those shown here. For example, you might need to use cmake instead of make. Always read the enclosed “README”, “INSTALL” or other documentation provided by the developers, and you won’t go too far wrong.