Compiling Software

Written By: X_console

You will find that most of the software you installed with your Linux distribution will quickly become outdated. You can give your thanks for this to the hundreds of Linux hackers out there who patch programs, upgrade them, and send them out.

One excellent example is the Linux kernel. Whichever distribution you bought, you can bet that it came with an older kernel than the one available right now.

So how do you upgrade software in Linux? Or how about installing and uninstalling applications? Most distributions come with software package managers. The most common is the RedHat Package Manager, or rpm for short. rpm is available in many Linux distributions. However, other distributions have their own package manager. Slackware has pkgtool and Debian has apt-get.

This tutorial will not be based on installing and uninstalling software through the use of package managers. Rather, it will cover the basics of installing software from source, as well as how to remove software as painlessly as possible.

Why Bother Building From Source?

Okay, so you have your trusty package manager and there is little reason to build software from source. Right?

Not so. When new software comes out, chances are, they will be in source code format. A format for your package manager will take a while to come out. Okay, so maybe it will not take that long, but still, if you are one of those who just has to have the newest software as soon as it comes out, source is the only way to go. Also, certain software packages need to be configured in source code format for certain features to be enabled. The binary file you get for your package manager may not have been compiled with this feature you want.

Another reason to compile source: in the event that your package manager breaks down, you may need to build it up from source. Or if you need to use a different Linux distribution without your package manager, knowing how to build from source will be helpful.

Perhaps the most enticing reason to build from source is if you are a programmer. Most Linux programs are released under the GNU General Public License. In a nutshell, programs released under this license allows you to modify and/or to redistribute the program and the source code.. So feel free to make modifications to the source code before compiling them. (Of course, you should only do this if you know what you are doing.)

Requirements

Building software from source means that you need to have development tools like compilers, interpreters, and debuggers. Most Linux distributions come with a set of development tools. If at all possible, install all the development tools that come with your distribution, because you never know when you might need them. If you cannot install all the development tools, then install at least the following:

Certain programs will require certain development tools. Be sure to read the documentation that comes with the source code. The documents will normally tell you what other extra tools need to be present for the program to be properly built. You may also be required to install certain libraries that might be required by certain programs.

Stable or Development?

Linux programs normally come in two flavors: stable and development. Stable releases are those releases that the programmers think are safe enough for production use. Basically this means that it has been thoroughly tested and a lot of the bugs have been cleaned out.

Development programs are those that are alpha and beta releases. These are still being tested and may contain serious bugs that could damage your system. Development releases are mainly for people who want to examine the source code and find as many bugs as possible. Unless you want to tinker with the source code, choose the stable releases.

Which Library Do You Need?

Before you download a source package, you may be asked if you want to download the glibc or the libc package. These are just libraries. If you are using the latest releases of RedHat, Debian, Suse, Caldera, Slackware, or other distributions based on them, you can download either one, but it is recommended you download the glibc version.

Where Do I Keep The Package?

This is a very common question with a very easy answer. You can download and save the package anywhere you want. Some people like it saved in /tmp; others, like me, make a directory specially for downloaded packages, for instance: /packages. I like to be able to keep an organized system so I know where everything is. Again, this is completely up to you. It does not in any way affect the building of the program.

Where Should Programs Be Installed?

Third party software is normally installed in /usr/local/. Binaries go in /usr/local/bin and, if the software is for the root user, /usr/local/sbin. /usr/local is basically just a mirror of the / directory, except that it is where you put your third party software in. Programs that don't seem to be fit for /usr/local (use your judgment), can go into /opt. However these are just conventions. You can install programs anywhere you like.

The Procedure

Installing from source is remarkably easy. At the most it is a five-step procedure. Here are the commands you will type into a console or terminal screen:

  1. tar xvzf foo.tar.gz
  2. cd foo
  3. ./configure
  4. make
  5. make install

That is it. Of course, it is only that simple provided you do not run into problems. In the next section we break everything down and examine what is going on.

It should be noted that most programs are built in the above procedure. Certain programs, however, do not follow the above standard. You will need to refer to the documentation included with the source package.

Unpacking the Source Package

When you download a source package, it will normally be in an archived and compressed format. They will have the extension .tar.gz or .tgz. These files are called tarballs in Linux speak. A .tar.gz file and a .tgz file are the same. .tgz is just shorthand for writing tar.gz.

Beware, though, certain .tgz packages are made specifically for Slackware's pkgtool. Make sure you check if a tarball is for generic Linux systems or for Slackware. If the tarball is a .tar.gz it is most likely for a generic Linux system.

If you happen to download a tarball from a Windows OS, there is a chance that the tarball's extension will get mangled. For instance, you want to download foo.tar.gz. After downloading you find that it has been saved as foo_tar_tar. This is probably due to the multiple dots in the original filename. Simply rename it back to foo.tar.gz or foo.tgz. Both are fine.

Save the tarball to a temporary directory. This can be /tmp or wherever you like. Assuming you saved it in /tmp, you can now begin to unpack it with the following command (as root):

tar xvzf foo.tar.gz 

This assumes that the file you want to unpack is foo.tar.gz. The above tar command along with its options will unpack and uncompress the file. Otherwise you would have to do it this way:

gunzip foo.tar.gz
tar xvf foo.tar

That is fine too. Personally I prefer not having to type two commands if I can do it all in one command. Once you have unpacked the source, a directory will (most of the time) be created. This directory will be the name of the tarball.

Since our tarball was called foo.tar.gz, the created directory in the /tmp directory is now /tmp/foo. Just to be sure, run the ls command to get a listing of all the files in the directory, and the name of the directory created when you unpacked the source tarball. Next step is to cd into that directory and then you can start building the software.

Configuration

It is time to run the configure script. configure will attempt to guess the correct values for system-dependent variables. These values are used to create a Makefile, which in turn will be used to build your software. To run the configure script, type the command:

./configure 

You will see tons of messages running down your system. Do not worry about it. If configure finds a problem, it will tell you and then it will stop. At which point you will have to figure out what the problem is, and hopefully, fix it. If all goes well, then configure should exit gracefully. If you do an ls you will find that there is now a Makefile.

configure comes with many options. You can view a list of all options using the following command:

./configure --help | less 

The one option you will most probably be interested in is --prefix=path. This option lets you specify where you want the software and all its files to be installed in. If you are concerned about an organized system, this is the way to go. If you just want to see if you can compile the software and where all the files will be installed, this is what you want. Before we do this, let us create a directory for the installed files to be put in. Lets call it /tmp/test/foo.

Now we run the configure script:

./configure --prefix=/tmp/test/foo

When you install your software, all of its contents will be kept in /tmp/test/foo. At the moment, /tmp/test/foo will be empty. You have not yet built and installed your software. You have only just generated a configured Makefile. If you do not specify a --prefix=path option, your software will be installed in the default directory, normally /usr/local.

Building The Software

Now comes the exciting part. This is where you actually start building the executable program from the source code. This is done using the make command. Make sure you have the Makefile generated otherwise make will not work:

make 

More text will start to fly off your screen. Again, do not worry about it. If something goes wrong, make will give you an error message. If you have no idea what this error message means, be sure to copy it down and post it here at JustLinux.com.

If all goes well (which it should if you read the documentation provided in the tarball and your system meets the minimum requirements needed to run the program.), you now have your software ready to be run. The final stage is to install it.

Installing

If you have managed to get here, congratulations. The chances of anything going wrong here is very slim. To install the software, make sure that you are root. Then run the command:

make install 

More stuff will fly off your screen. You should be used to this by now. When everything stops, your software has been installed. Since we used the --prefix=/tmp/test/foo option in configure, all our software has been installed in /tmp/test/foo. Check it to be sure. Be sure to keep your Makefile with you. You will need it later on. If you used the --prefix=path option, you will need to modify your /etc/profile file and add the software's path to your PATH variable. Just add the following line right at the bottom of /etc/profile:

PATH=$PATH:/tmp/test/foo/bin
export PATH

Next we have to update your /etc/profile, and this is done with the source command:

source /etc/profile

With that done, your program should now be ready to run. See if you can run the program. If you get library errors, try running ldconfig and then running the program again.

When all is well, it is recommended that you run makewhatis to update your whatis database and updatedb to update your locate database. Finally, you will want to get rid of all the object files created during the make process. This will save you disk space. Use the command make clean to accomplish this.

Uninstalling

How do you uninstall software that you have built from source? Well, there are three ways to do this. We will start with the easiest way, to the hardest way. First if you kept your Makefile like I told you to, you may be able to run the command:

make uninstall

If text flies off your screen, that is a good sign. If make says it does not understand you, that is a bad sign. In the event that make cannot understand you, you may have used the --prefix=path option, in which case you can uninstall the program simply by deleting the directory it is resting in, in our case, /tmp/test/foo. If you decided not to use the --prefix=path option and you did not keep the Makefile, you are in serious trouble. First, you will need to run ./configure again to generate the Makefile. Then try to run a make uninstall and see if that works. If it does not, then you will need to read the Makefile to see where everything gets installed. Save yourself the trouble and keep the Makefile, or use the --prefix=path option.

A Real Life Example

Now for a real life example. I downloaded a tarball called bar.tar.gz and saved it in /tmp.

Okay, so the tarball name is not real. But the procedure is. First I decide I want to install the files in /usr/local/programs/bar. So obviously, I will have to create the appropriate directories. Now I should mention that you do not need to be root to configure and build the software. However, you must be root when you are installing the software.

cd /tmp
tar xvzf bar.tar.gz
cd bar
./configure --prefix=/usr/local/programs/bar
make
make install

Now that my software is installed, I then edit my /etc/profile and append the following:

PATH=$PATH:/usr/local/programs/bar/bin
export PATH

Finally, I source my /etc/profile file and then I am ready to use my program. When I want to uninstall it, I can just run make uninstall, and if that does not work, I can just delete the subdirectory bar under /usr/local/programs.

Conclusion

So that covers the main basics. Do not be afraid to build programs from source. It is not hard. After all, if you plan on upgrading your kernel, you need to build it from source anyway. Sometimes you may not have to run the configure script at all. Read the INSTALL and README files included in the tarball. It tells you what to do.


[ top ]