Unix fundamentals - compiling software from scratch

  • September 20, 2007
  • Avatar for peter
    Peter
    Upfold

This is a fundamentals post - where we cover a fundamental concept to do with a particular bit of free/open source software so you can learn it, brush up and then be armed with that knowledge for future posts!

Installing software. It's something that you do quite a lot if you're like most computer users. On Unix-like systems, there are several different ways you be getting that program however - it's not necessarily a simple case of double-clicking one setup file.

One of these ways is to download the program's source code and compile it yourself. This process can be a little tricky to the uninitiated, but has several benefits - including meaning you'll have the latest copy of the program and you'll be able to get a copy if you're using an operating system or distribution where no pre-built packages are available.

Unfortunately, the ways different bits of software are built means that this process can differ slightly depending on exactly what you're working on. If you're having problems, it might be you're dealing with something that's a little different, so you may have to look for more help.

Let's dive in!

Dependencies

First of all, there is one usual major pitfall that can be difficult at first when you start compiling software on your own. That is dependencies. In order to build a program, you need to have all the software that program depends on installed. On top of that, you need all of those programs' development files. Confused yet?

Basically, look around on the program's site for a list of dependencies and then use your OS or distribution package manager to install those dependencies (and the -dev or -devel packages if necessary).

Download and extract

If you've got dependencies all ready to go, you need to go and actually download the source code and extract the downloaded file. Most source code is distributed in .tar.gz or .tar.bz2 files, which you can extract from the terminal with (respectively);

$ tar xzvf filename.tar.gz
$ tar xjvf filename.tar.bz2

Pick the relevant line from there (top for .tar.gz, bottom for .tar.bz2).

Now the files are extracted, you need to change into the new directory and get configuring.

$ cd nameoffolder

./configure

This step prepares your system to get compiling the code. The configure script checks for dependencies (if you have any unsatisfied ones, you will be told here and configure will fail), makes sure everything is ready to go and generates some files.

Making sure you're in the directory that extracted, simply do:

$ ./configure

Note that most software includes special options you can pass to configure if you wish to customise your build. Check the individual program documentation to see if you need this (or might want it) and follow the specific instructions for the configure step.

make

Once configure has finished without any errors (if there were, you may need to backtrack to the dependencies stage), you're ready for the main step. The make command will go through all the source files, compile them into object code and then link it all together to make the executable program.

$ make

This could take some time depending on the size and complexity of the application, and it will certainly use some CPU power!

make install

Once make has completed, there will be one more step to take. make install copies the finished files out of the temporary source directory and into their final destination on your system (this will probably be somewhere under /usr/local unless you specified otherwise).

This step will probably need to be executed as root. On most systems, that will be:

$ su
[[enter root password when prompted]]
# make install

However, on Ubuntu, and other systems that use sudo, it will be:

$ sudo make install

This process should take less time than make did, and once it's done your program is installed and ready to run.

We're all done!

In the vast majority of cases, this is the simple process you need to undertake to compile from scratch. As I said earlier, however, software differs so always check the official instructions first!

Avatar for peter Peter Upfold

Home » Articles »