Saturday, March 28, 2009

Building From Source using Apt-get in Ubuntu

As a reasonably dedicated Debian/Ubuntu user, I often find myself at a disadvantage compared to Gentoo users who compile everything from source. Compiling from source can often yield huge performance gains over just installing binaries provided by your distribution. Luckily, the apt-get system provides users the ability to compile from source just as Gentoo users can.

The first step in compiling from source is issuing
sudo apt-get build-dep package_name
where package_name is the name of whatever package it is you are planning on compiling.

The sudo at the beginning of this step is important, as it gives you the necessary permissions to perform the apt-get part of the command. Issuing the apt-get build-dep command prompts apt-get to install binaries for whatever is required to build the package you specify, from things as simple as basic as the GCC compiler to as complicated as the Gtk developer headers, and any other library the program you are compiling depends on.

After you have installed all of the requirements to build a package, the next step is issuing the

sudo apt-get -b source package_name
where package_name is again the package you are compiling. This is the step that actually does the compiling, so it can take a while. What you are left with after this step is completed is a .deb file in your home directory.

Finally, to install the package you just compiled, there are two options. If you navigate to your home directory in Nautilus and double-click the .deb file you've created, gdebi-gtk will take care of you as far as installing the package is installed. If you prefer to install via the command line, dpkg is the necessary tool.

To install from the command line, first type "cd". Issuing the cd command by itself returns you to your home directory, no matter where you are in the file system. The apt-get -b step places the package in your home directory, so this is where we need to be. Finally, issuing the command "sudo dpkg -i package_name" will install the package that you've just compiled. Once again, the sudo portion of the command is required, as a normal user as is the default in Ubuntu does not have the required permissions to install new software.

If you ever seek to remove the package, you can do so by issuing the "dpkg -r package_name" command. The -r in this case stands for remove.


Wardy said...

What you are doing is new to me but wouldn't this build the same binaries? I thought you would have had to change some of the compilation options to get the 'huge performance gains' you mention in the intro but you unfortunately haven't mentioned this. I was enjoying the article up until I realised there may not be any benefit.

CelloFellow said...

You can't just do apt-get -b source to get "performance gains". You have to do apt-get source and then edit the debian rules.

Also, you may want to do dch -v to change the version number of the package so apt won't insist on upgrading the package.

zoolook said...

Bah. Performance gain? Show me the numbers. I ran Gentoo for almost 7 years and I've found Debian/Ubuntu to be better/faster in many things (apt just rocks). Plus, with Debian/Ubuntu you have a running system in a fraction of the time it takes to configure sources in Gentoo.

Anonymous said...

If you don't see any performance gain, then you are doing it wrong. The speed and responsiveness of a system compiled with hardware specific flags is easy to notice. In some cases(3D) the gain is impressive. Pay attention and you'll see (or prove it to yourself with
In addition, you don't need to configure sources every time you install a source-based distro - just copy the configuration from a working one (obvious, isn't it).

William Jennings Bryan said...

I find Gentoo is suited best to enthusiasts. Like spirtbrat said, compiling with system specific flags should always yield a speed increase, though the package maintainers are typically pretty good at choosing generic flags.

Post a Comment