Unix fundamentals - compiling software from scratch

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 - http://peter.upfold.org.uk/

Peter Upfold is a technology enthusiast from the UK. Peter’s interest in Linux stems back to 2003, when curiosity got the better of him and he began using SUSE 9.0. Now he runs Linux Mint 9 on the desktop, runs a CentOS-based web server from home for his personal website and dabbles in all sorts of technology things across the Windows, Mac and open source worlds.

Home » Articles »

Discussion: Unix fundamentals - compiling software from scratch

  1. Binny V A (guest)

    # Posted on 20 September 2007 at 08:10 PM

    $ su - [[enter root password when prompted]]

    make install

    That should be $ su

    The '-' must not be there.



  2. Karper (guest)

    # Posted on 20 September 2007 at 10:01 PM

    It's rarely that such post cover the other, equally important aspect of being able to uninstall the application. How's that done? Would it be better to do sudo checkinstall and install the .deb package by saying sudo dpkg -i nameofdebpackage.deb. Atleast that way, the uninstall is simple. You just do sudo dpkg -r nameofdebpackage.deb, right?

    • Noob. Please help out if you know better :)


  3. thumb (guest)

    # Posted on 21 September 2007 at 11:48 AM

    Karper:

    make uninstall or sudo make uninstall



  4. Karper (guest)

    # Posted on 21 September 2007 at 11:53 PM

    thumb:

    One more question: Run sudo make uninstall from the /usr/local directory? 'Cause I might have deleted the original untarred directory from my home folder, right...

    Not knowing how to undo any install like this has prevented my from trying out so much software...

    Thanks for your help!



  5. gustgr (guest)

    # Posted on 23 September 2007 at 07:50 PM

    Just a few words about uninstalling software that was compiled and installed from source.

    If you are under a distribution which has good support for packaged software (such as Debian) you can compile the program XYZ from scratch, and instead of installing it with a "make install", you may build a package with the binaries you have just compiled and then you just install it the way you'd install any other package, and remove it as easily.

    If you don't like package managers, or you do not have a good one available to you, you can use checkinstall (http://www.asic-linux.com.mx/~izto/checkinstall/). The problem with installing software from scratch is that rarely there is an uninstall rule written in the Makefile, making the process of uninstalling software very annoying (most developers think that uninstalling the software is your task, not theirs). Checkinstall keeps track of what was installed with "make install" and where, so it is easy to remove every file installed.

    Another choice is to use the --prefix option when you are ./configuring the source. If you want the Makefile of program XYZ to install the program under the directory /usr/local/xyz, but you do not want it to touch any other location of your system, you can easily use --prefix=/usr/local/xyz. So, if you want to remove the whole installation, you only need to do a plain rm -rf /usr/local/xyz. The annoying thing here lies in the fact that you have to add /usr/local/xyz/bin to your $PATH, and /usr/local/xyz/man to your $MANPTH, etc (and you have to change your environment variables again when you remove the software from that location).



  6. # Posted on 04 October 2007 at 12:22 PM

    [...] So let’s get some LAME. You might be able to get this from your distribution, but that will usually involve enabling custom repositories, so it’s often easier to compile yourself. [...]



  7. # Posted on 30 April 2008 at 06:22 PM

    [...] in September, I showed you how most source applications can be compiled and installed. Installing software. It’s something that [...]



  8. # Posted on 20 May 2008 at 04:45 PM

    [...] On my Ubuntu system, I was able to simply sudo apt-get install fondu, but if a package is not available for your operating system, you can get the source code and compile it yourself. [...]



Home » Articles » Unix fundamentals - compiling software from scratch