Monday, December 28, 2009

How to Install unix programs from Source Code

Preparing the System for Compiling

Before you can proceed with compiling programs on your system, you will need a compiler, libraries and some other basic utilities. Some of the common programs required for most of the programs are:

·GNU coreutils : The GNU Core Utilities are the basic file, shell and text manipulation utilities of the GNU operating system. These are the core utilities which are expected to exist on every operating system. Previously these utilities were offered as three individual sets of GNU utilities, fileutils, shellutils, and textutils. Those three have been combined into a single set of utilities called the coreutils.

·GNU binutils - The GNU Binutils are a collection of binary tools. The main ones are ld ( GNU linker ) and as ( GNU assembler )
·GCC- GCC stands GNU Compiler Collection. GCC is an integrated distribution of compilers for several major programming languages. These languages currently include C, C++, Objective-C, Objective-C++, Java, Fortran, and Ada.

·Make - Make is a tool which controls the generation of executables and other non-source files of a program from the program's source files. Make gets its knowledge of how to build your program from a file called the makefile, which lists each of the non-source files and how to compute it from other files. When you write a program, you should write a makefile for it, so that it is possible to use Make to build and install the program.

·GNU tar/gunzip/bunzip2
These are archiving utility generally used to unpack source tarballs. These are generally in format of .tar, .tar.gz or bz2.

Step1: Get Source

In this example, we are going to install the latest version of NMAP released a few weeks back. We get the source from ..dist/nmap-4.20.tar.bz2 which is the current version. The latest rpm version available at this time was nmap-4.11 version.
To do this, I created another directory “nmap” and used wget to get the latest tarball as shown in the screenshot below.

Step 2: Unzip the Source Tarball
Now we unpack (unzip) the tarball by using the tar command.
This will extract the source code for nmap-4.20 into a folder.

Step 3: Run Configure Script


On different systems, the compiler and other libraries might be in different place than a regular Linux system. For example, you may be different type of bash than other users. Configure program creates a MakeFile which will be later used by make program.
Configure is basically a shell script generally written by GNU Autoconf, which looks at your system settings and tries various things to figure out what works. It takes instructions from MakeFile.in and builds a MakeFile which it thinks would work on the current system.
You can view various program options by running "./configure --help"

On my system I don’t want to install NMAPFE (the front-end for nmap) so I am going to run the configure command again with appropriate options.

Once the configure command finishes, it creates a Makefile which will be used by make program to create binaries of nmap program. Let us now see what configure added to our Makefile which was not there earlier.
[root@localhost nmap-4.20]# diff Makefile.in Makefile
4,11c4,11
< NMAP_PLATFORM=@host@
< prefix = @prefix@
---
> NMAP_PLATFORM=i686-pc-linux-gnu
> prefix = /usr/local
48,49c48,49
< TARGETNMAPFE=@TARGETNMAPFE@
< INSTALLNMAPFE=@INSTALLNMAPFE@
---
> TARGETNMAPFE=
> INSTALLNMAPFE=
The last change shows that it removed NMAPFE, since I use --without-nmapfe in my configure option.

Step 4: Use Make Command


Make utility requires a file named Makefile in the same directory in which you are the command. In our case, the MakeFile has been created by using configure script which we will now use to run make command.
Make command uses the directions present in the Makefile and proceed with the installation. The Makefile indicates the sequence, which it must follow to build various components of nmap. This sequence depends on the way the software is designed by its coder.
Now lets run the make command in nmap folder.

Make command generally takes a while, once complete it will compile nmap’s source code and creates the executables. At this point you can use the nmap program from this folder by just typing ./nmap.
This means that everything done, only the same to copied to the installation path, which will be created by the script in the make file, permission are also given by the same script. J
Step 5: Run Make Install
When make is run without any parameters, it starts reading instructions from MakeFile from the start and start compiling code. However, when you run `make install` the make program reads the install label from Makefile and executes only that section of the makefile.
install-nmap: $(TARGET)
$(SHTOOL) mkdir -f -p -m 755 $(DESTDIR)$(bindir) $(DESTDIR)$(mandir)/man1 $(DESTDIR)$(nmapdatadir)
$(INSTALL) -c -m 755 -s nmap $(DESTDIR)$(bindir)/nmap
$(INSTALL) -c -m 644 docs/$(TARGET).1 $(DESTDIR)$(mandir)/man1/$(TARGET).1
$(INSTALL) -c -m 644 docs/nmap.xsl $(DESTDIR)$(nmapdatadir)/
$(INSTALL) -c -m 644 docs/nmap.dtd $(DESTDIR)$(nmapdatadir)/
$(INSTALL) -c -m 644 nmap-services $(DESTDIR)$(nmapdatadir)/
$(INSTALL) -c -m 644 nmap-rpc $(DESTDIR)$(nmapdatadir)/
$(INSTALL) -c -m 644 nmap-os-fingerprints $(DESTDIR)$(nmapdatadir)/
$(INSTALL) -c -m 644 nmap-os-db $(DESTDIR)$(nmapdatadir)/
$(INSTALL) -c -m 644 nmap-service-probes $(DESTDIR)$(nmapdatadir)/
$(INSTALL) -c -m 644 nmap-protocols $(DESTDIR)$(nmapdatadir)/
$(INSTALL) -c -m 644 nmap-mac-prefixes $(DESTDIR)$(nmapdatadir)/
Install section instructs make to copy files created in previous step to final directories. For example, executables are copied into /usr/local/bin. When we ran only make the executables were created in the same folder where we unzipped the tarball. So, now when we run make install, these executables are copied to their final destinations.

Tip: Keeping a log of what was installed by the program
Many good programs provide you with `make uninstall` section to easily uninstall the program and its executables from the system. If not provided, you can use this tip to find what was installed when you ran `make install`.
Before running `make install`, run the following command on your system. This will create a big list of all files that exist in your system except the following directories: /proc , /tmp and /dev. These directories are transient and not used when installing programs so we can ignore them.

After running the `make install` again run the same command and create a post-install list. Then you can run diff between these 2 files and it will show you list of all the files that were installed during nmap installation.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.