Use These Four Simple Steps to Compile Linux Programs from Source

Linux users generally have an easy time finding and installing software. Just about every distribution has a software repository with a nice, graphical front-end. To install a program you usually only have to search for it and then press the “Install” button. If the software isn’t available in the repository, you can usually find pre-compiled binaries online somewhere. Then, it’s a just a matter of running apt-get, yum, or similar (depending on which Linux distribution you are using) to install them. Sometimes, though, you have no option but to compile Linux programs from source yourself, especially if you want the bleeding-edge development version of a program for which no pre-compiled binaries exist.

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.

Also read: How to Use the G++ Compiler on Linux

Steps Required to Compile Linux Programs from Source

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

  1. Unpack the source code
  2. Resolve dependencies
  3. Compile it
  4. Install it

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).

linuxfromsource-sqlite-download

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:

tar -xzvf filename.tar.gz

Sometimes you will have a tar.bz (or tar.bz2) file. Use the following command if you’re working with a tar.bz file:

tar -xjvf filename.tar.bz

This process creates a directory with the same name as the file.

linuxfromsource-extracted-dir

Also read: 5 Tricks to Speed Up Compile Times in Gentoo Linux

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:

cd directory_name
sudo ./configure

linuxfromsource-configure-step

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:

sudo make

linuxfromsource-make

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:

sudo make install

linuxfromsource-make-install

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.

linuxfromsource-installed

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:

sudo make uninstall

Followed by:

sudo make clean

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.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

Mark Lewin

Mark Lewin has been developing, teaching, and writing about software for over 16 years. His main interest is web development generally and web mapping in particular. He also has a passion for open source and Linux.