Building from source

06 Apr 2013 in Tech

Ever since I first started using Linux, I've been using package managers to manage my system. Coming from a Windows background I wasn't really sure on how compiling from source works, how to uninstall things you compile etc. I was expecting artefacts to be left behind in some registry I didn't know about, for my system to get confused when things it used to know about were just deleted.

As it turns out, I was wrong to be scared of it. Compiling from source is easy - it's really easy. Plus, once you know how execution paths work on *nix it makes a lot more sense to compile your own stuff.

$PATH

When you run a command, it searches your $PATH which is a list of folders which could contain the binary to execute. A binary is just something we can run, a script or a binary file.

The folders are searched in order, so if you have something with the same name in two folders it'll execute the first one that it finds.

I like to add ~/bin to the beginning of my path, and if I ever need something temporarily (e.g. a newer version of python) I can just compile it, copy it into ~/bin and start using it.

Compiling

People make compiling really easy. Depending on the project, it's between three and five commands to have it installed globally on the system.

The first optional step is to run ./autoconf.sh . Projects such as ZeroMQ have this file, which is used to generate a configure file for the project. This is only used when each system needs a unique configure file.

The next step is to run ./configure. This checks that your system has a sane build environment and perhaps most importantly, generates a Makefile for you.

Makefiles are kind of like instruction sets of what to do. Typing make will run the default action, which for the majority of projects is to compile the source code and produce a binary file.

The next step (if your project supports it) is to run make test. This runs the test subtask of the Makefile, which verifies that the binary built correctly on your system. Hopefully everything will pass fine.

I've found that the binary output is generally placed in the src folder. If you want to place them in ~/bin, copy them from src and you're done.

If you want to install it globally, you need to do one more thing. Run make install (possibly with sudo, depending on the project) to copy the built binary somewhere into your path. Next time you refresh your $PATH and run the command, it should use your shiny new compiled version.

If you ever want to remove the project from your system, just delete the binary file. You can find where it's located by running which binaryName. Run rm /path/to/binary and it's gone. No worrying about registry entries, or any other programs being associated with it. Just delete it, and it's gone.