Compiling open source projects in Linux

If you are new to Linux, one thing you see a lot of is that people pull open source projects down to their workstation, compile them, and install them. In the Windows or MacOS world – you expect to find an installer. Even in modern-day Linux, every is used to package managers (like apt-get, yum, rpm, etc).

With that said, there will be times when you have to pull down some source and compile it. How do you do that? Well, it turns out it’s not as scary or as bad as you might think. Here is an overview:

  • Download or clone the code to your machine.
  • Run “configure” to verify dependencies are in place.
  • Run “make” to actually compile the project.
  • Run “sudo make install” to install it on your local computer.
  • Run “sudo make uninstall” to uninstall and remove the library/application from your local computer.

With that said, just about every case will fall into two scenarios:

  1. The open source project is on github, so you need to “clone” the repository and start there.
  2. The open source project is zipped into a “tarball”, which needs to be downloaded and unzipped, then you start from there.

I tried to find an example of each that you could try on your own computer – see below. Also, this is pretty much specifically for C or C++ projects, which is what most open source projects are. Other languages work differently, but then again – most of the times when you might need to do this, it’s very likely it’s a C or C++ project.

Also note that you don’t need to know the C language to run these – the projects are specifically configured to compile in almost all circumstances, with just a few commands.

Using an open source git project:
For this example, you can find this project on github – nmap:

https://github.com/nmap/nmap

Now, this is widely available via package managers (sudo apt-get install nmap), so you don’t actually need to compile this. However, it’s a small project which builds on it’s own, so it’s a simple example.

First, let’s create a directory for the source (I’m working out of ~/tmp/):

$ mkdir ./nmap

Next, we use git to “clone” (or copy down) the source to the nmap folder:

$ git clone https://github.com/nmap/nmap ./nmap

if you don’t have git installed, it’s as simple as :

$ sudo apt-get install git

Now, step into the nmap folder:

$ cd ./nmap

Run configure to verify the dependencies are OK:

$ ./configure

Then compile the project:

$ make

Then install it (you need to be root):

$ sudo make install

To verify that it is installed and working, you can either run the command or view the man page:

$ nmap
$ man nmap

To uninstall it, run:

$ sudo make uninstall

Then to verify it has been uninstalled, attempt to run the app or view the man page:

$ nmap
bash: /usr/local/bin/nmap: No such file or directory
$ man nmap
No manual entry for nmap

and that’s it, it’s as simple as that.

Using an open source tarball project:
For this example, you can find this project on sourceforge:

http://sourceforge.net/projects/mhash/files/mhash/0.9.9.9/

this is a module that was needed for steghide. Since it’s a standalone project, it should make it nice and simple. As you can see, this is the source code, bundled up into a .tar.gz file. This is a “tarball“, or a compressed file which had directory structure and files within it.

You could certainly download the tarball with your browser, but it’s often ideal to work in the command-line for this – and even create a script to automate this sort of thing. So, let’s create a directory structure and download the file.

Create a sub-directory where we can work (I’m working in ~/tmp/):

$ mkdir libmhash
$ cd ./libmhash

Now, retrieve the tarball and put it in the current directory:

$ wget http://sourceforge.net/projects/mhash/files/mhash/0.9.9.9/mhash-0.9.9.9.tar.gz

You will now have the zip/tarball in the current directory – called “mhash-0.9.9.9.tar.gz”. Remember, you can just start typing the first few letters and hit <TAB> to let the shell complete the name. If there are more than one option, hit <TAB> twice and it will show you the available options.

Anyhow, to untar this – and this is one of those things you just need to memorize, the command-line argument is “-xzvf”. I remember it by the position of the letters on the keyboard. Perhaps you can come up with a mnemonic? Xylophone Zebras Very Funny? I don’t know. Anyhow – un-tar the file:

$ tar -xzvf ./mhash-0.9.9.9.tar.gz

that will create a “mhash-0.9.9.9” directory, navigate into that:

$ cd ./mhash-0.9.9.9

from here, you can configure, make (compile), and make install (install) the module:

$ ./configure
$ make
$ sudo make install

to verify the module is installed, look at the output which tells you – and you can see the man pages installed:

$ man mhash

Now, to uninstall, run:

$ sudo make uninstall

Again, look at the output. To confirm and now, you should get an error if you try to view the man page:

$ man mhash
No manual entry for mhash

That’s about it. Every once in a while, you’ll get an error that you need a module – most of the time, you can just “sudo apt-get install <modulename>”. If not, sometimes you need to run the above process to compile and install it, THEN you can go back to your original project.

Bottom line:
In the modern days of package managers (like apt-get), this is concept is needed less-and-less. However, when it is needed, hopefully this cheat-sheet helps you get through the process (that includes you, Future Robert!)

Posted in Computers and Internet, Development Tools, General, Infrastructure, Linux, Organization will set you free, Professional Development, Security, Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Archives
Categories

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 5 other followers

%d bloggers like this: