Skip to content

A quick guide to compiling stuff on Linux

Beginner's Linux Tutorials

Find this tutorial useful?





I thought this might be worth doing: a short tutorial for Linux newbies on how to compile programs by source. Talk about jumping in at the deep end. I’ll do some easier ones as well, don’t worry. Anyway…
Generally, on non-open platforms, you never see the source code of anything (think of the source code as the ‘recipe’ for the program). Even on Linux you generally get new software packaged as shiny binary .rpm (or .deb) files.

However, on occasions you might want/need to compile a piece of software from source; sometimes your distro won’t have packaged that particular piece of software, or you need to install a new version that hasn’t been packaged yet. In this tutorial, I’m going to look at how you can compile stuff yourself (so click Read On if you’re on the front page).

So, here’s how to do it. Now, yes, this does involve command-line stuff, but there’s no getting away from that I’m afraid. Speaking of command line stuff, remember that anything you enter in a command line is case sensitive. Also, in any Linux/Unix command line instructions you will see, there will be a dollar ($) sign or a hash (#) sign at the start of the line. You don’t type this in, it’s just tells you to start typing after the sign (and there’s a geeky explanation too).
Grit your teeth and get ready. Before you start, you need to have a compiler installed on your system before any of this will work, so go to your distro’s software management program and install gcc (or use one of the commands below).

[On Fedora/Red Hat/CentOS] $ su -c "yum -y install gcc"

[On Ubuntu/Debian] $ sudo apt-get install gcc

[On SUSE] $ su -c "yast2 sw_single &" (then search for and install gcc)

If you did any of the above commands, you’ll be asked for a password (either the root password or your login password) before downloading and installing begins.

OK, once that’s done, so grab a copy of the source code for the program you want to compile (from the website of that program).

You will end up with a file usually ending with a .tar.gz or a .tar.bz2 extension. Now it’s terminal time, so open up Konsole (KDE) or Gnome Terminal (Gnome, obviously).

First things first, switch to the folder where your file was downloaded to (in most cases this is the desktop) so run:

$ cd Desktop

Now you need to extract the contents of your downloaded file, so use one of the following commands:

[for .tar.gz] $ tar -xzvf file-name.tar.gz
[for .tar.bz2] $ tar -xjvf file-name.tar.bz2

Here’s a trick for next time you need to type a long filename. If you type the first few letters of a file and press Tab, you will hopefully have the filename completed automatically for you. This saves me loads of time when I (frequently) use the command line. If there’s more than one possibility with the few letters you typed, type more letters until that file is uniquely identified and press Tab. Or alternatively, double-tap Tab to see a list of all the possibilities, so you can quickly find the file you want to work with.
In the time you’ve read that tip, your file should have extracted (the list of files will stop scrolling and you’ll be returned to the command line). You will end up with a folder on your desktop (or wherever) called file-name containing a lot of scary looking files. Never fear, the files can’t hurt you.

So to work with these newly extracted files, we need to change directory (cd) into our new folder (try the tab-trick).

$ cd file-name

Now we need to talk for a second. Programs generally don’t run all by themselves. They depend on other programs being installed in order to run. These dependencies have to be installed first before you compile any software which requires them. Worse still, compiling needs the ‘development libraries’ of each piece of software (now that sounds scary). Thankfully, you can usually run yum -y install [library]-devel or sudo apt-get install [library]-dev to install the packages needed. For now, I won’t cover this in detail, but if you get stuck on anything, leave a comment and I’ll do my best to help.

OK, so back to the compiling. The next step is to run a script called configure, which sets everything up to be compiled. It’s fairly simple to run:

$ ./configure

If that has any errors, you’ve most likely run into dependency problems. Leave a comment if you want, I might be able to help. Otherwise, it will just finish and give you a prompt again (or might tell you to start make). So that’s what we’ll do – run make:

$ make

This usually takes a while, depending on what you’re installing. But when it finishes, there’s one last step. For this step you need to be root, the super-administrator. On Ubuntu, simply put sudo in front of the command make install; on most other distros, you need to run su first (enter your password) and then you’ll be ready.

Either way, when you’re root, run:

# make install

Notice there that I prefixed with the #, not the $. This brings me to the geeky explanation. The $ means you are a standard user (i.e. you are not the root super-administrator) and the # means you are the super-administrator (so be very careful what you do).

If you ran su before (not sudo), you need now to type exit to get yourself back to normal user status.

And your software should be installed (in theory). Needless to say, it doesn’t always go smoothly (usually there are at least some dependency problems to sort out first). If it doesn’t, try installing the Software Development set of packages in your distro’s software management. This usually solves 95% of problems.

Anyway, good luck if you did try a compile, and I’ll be back with more Linux tutorials. Oh, and if you liked this, leave a comment – go on.

Like this post?

If you would like to support the time and effort I have put into my tutorials and writing, please consider making a donation.

2 Comments

  1. Iain wrote:

    Thanks a lot! …but i’ve always got software dependecies and don’t have internet on my laptop(bsd)…. is there any method to install all those libs???

    Friday, April 3, 2009 at 04:42 | Permalink |
  2. tonyz wrote:

    Peter, just what this old man was looking for….
    your 20, I am 58. What a great humbling experience.

    Saturday, September 11, 2010 at 20:28 | Permalink |

2 Trackbacks/Pingbacks

  1. […] Welcome back to my Beginner’s Linux tutorial series and today I’m going back to the subject of software installation. A while ago I looked at compiling software from source, but this time I’m going to look at installing RPM packages. […]

  2. Installing .rpm files on Linux « Blog « Peter Upfold on Monday, August 4, 2008 at 16:09

    […] and today I’m going back to the subject of software installation. A while ago I looked at compiling software from source, but this time I’m going to look at installing RPM […]

Post a Comment

On some sites, you must be logged in to post a comment. This is not the case on this site.
Your email address is not made public or shared. Required fields are marked with *.
*
*
*

Posting a comment signifies you accept the privacy policy.
Please note — your comment will not appear straight away, as all comments are held for approval.