How To: Install C Package From Source

This article outlines installing packages from source into your Linux system.

Sometimes the luxury of apt-get install, or yum install is not an option. However, a tar.gz or tar.bz2 source code is the only way of getting the package you need. Basically you have to unpack the tar and compile and finally install the newly compiled binary, simple right? I will outline the process here.

1. Unpack The Source Package Using tar -xvf

Lets say you have a tar.gz package, you must unpack the tarball first. So for example:

erik@debian:~$ tar xvzf data.tar.gz (or tar xvjf data.tar.bz2 if its a bz2 file)

This will unpack the tarball into a data/ directory. Enter that directory. (cd data)

2. Run ./configure To Setup Makefile

When downloading source packages, the designers usually take into account that each Linux distribution and architecture (check endianness of 32bit or 64bit) has different compilers and may need to be configured a different way. This is where the ./configure comes into play. It will setup the Makefile in such a way that it will detect correct libraries and compilers to use, or inform you if your system does not meet basic requirements to build the package.

To execute the ./configure:

data@debian:~/data$ ./configure 
checking for g++... g++
checking for C++ compiler default output file name... a.out
checking whether the C++ compiler works... yes
checking whether we are cross compiling... no
checking for suffix of executables...
checking for suffix of object files... o
checking whether we are using the GNU C++ compiler... yes
checking whether g++ accepts -g... yes
checking for win32... no
checking for Qt >= 4.1.0... 4.2.1
...

This will output information as to what your system provides for compilation. If everything works correctly we can move onto the compilation step.

3. Compile Source Using Makefile make

Now we can get down to business. After configure has finished, a Makefile will be present to compile your application from scratch. If everything went as planned it is only one more step to installing the program. Enter the command make. This will begin to compile your program and may take quite awhile depending on the package you are trying to install.

data@debian:~/data$ make
g++ -c -pipe -O2 -Wall -W -D_REENTRANT  -DQT_NO_DEBUG -DQT_XML_LIB 
-DQT_GUI_LIB -DQT_NETWORK_LIB -DQT_CORE_LIB -I/usr/share/qt4/mkspecs/linux-g++ 
-I. -I/usr/include/qt4/QtCore
-I/usr/include/qt4/QtCore -I/usr/include/qt4/QtNetwork -I/usr/include/qt4/QtNetwork 
-I/usr/include/qt4/QtGui -I/usr/include/qt4/QtGui -I/usr/include/qt4/QtXml 
-I/usr/include/qt4/QtXml -I/usr/include/qt4 -Isrc/gui/common -Isrc/gui/help/browser 
-Isrc/lang -Isrc -Isrc/config -Isrc/control -Isrc/gen/moc -Isrc/gen/ui 
-o obj/mainwindow.o src/gui/mainwindow.cpp

Barring any errors (it will show you if there were) we can continue on to the installation step.

4. Install Package To System Using make install

Finally, the package is now compiled and ready for all users of the system to use it, there is just one problem… it is not yet installed to a directory where everyone can access it. In some cases, you have to run as root or use sudo make install to actually install the software.

data@debian:~/data$ sudo make install

or

data@debian:~/data$ su
 // enter root password
data@debian:~/data# make install

If all went as planned you have now unpacked, configured, compiled, and installed your new source package to your system. While it is not as elegant as apt-get install or yum install, sometimes this is the only way to install packages to your system.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *