You've come a long way in your Linux education! You've configured various aspects of your system, you've upgraded and installed with rpm, you've learned basic commands, learned about partitions, and now it's time to take your next stepŸinstalling from source code!
Although this feat may sound daunting, it's actually quite simple. Thanks to the advanced compilers (gcc and friends) and well-documented README and INSTALL documents (included with nearly all source code), the compiling and installation of an application via its source code is not only a simple, but enjoyable, experience. Beyond the simplicity and joy, with an application compiled from source code, you have a program that runs more efficiently and cleanly. This is largely due to the fact that the particular application was compiled specifically for/on your machine. This has several advantages, of which efficiency tops the list.
The first step in compiling from source is to get the source. I always try to make things simple, so I've decided to standardize this instruction and find a small application that is sure to work across distributions. The file to download is the source for a very simple console blackjack card game that can be found here. When Netscape asks you where you want to save the file, REMEMBER WHERE YOU PUT THIS FILE. I generally create two subdirectories within my /home/USER directory; one for rpm files (called rpms) and one for source files (called tars).
Before we go on, I want to explain tar. tar is a tool that archives files and directories, so you can effectively take a directory that contains subdirectories and files and pack it into one file. Once this file is tarr'd, the new extension is .tar. For example: You have a directory called stuff, within stuff you have subdirectories called work and play, and within both subdirectories, you have several files. When you tar the main directory, you take all of its content and pack it, along with the main directory, into a single file called stuff.tar. The command to do this is tar cf stuff.tar stuff.
Now, once you have a file tarr'd it is still the same size as it was before, but you want to make it smaller. So, you run the gzip application to bring it down to a more manageable size. When you run gzip stuff.tar you'll then have a file with a new extension (.gz) called stuff.tar.gz. This new file is a "zipp'd" version of the archive file you created by packing together all of the contents of the main directory called stuff.
Of course, as with any Linux application, there are shortcuts. To cram the archiving and zipping steps into one step, you can run the following:
tar cfz stuff.tgz stuff
which will give you a zipp'd and tarr'd file called stuff.tgz in one simple step!
Now, on with our installation.
The first step is to unzip and unpack the file. We'll do this both the long and the short way. The long way is as follows (NOTE: The make and make install commands must be run as rootŸso in order to move ahead, you must have root privileges):
cd to the directory where you saved the blackjack-0.1.tgz file
run the command gunzip blackjack-0.1.tgz
run the command tar xvf blackjack-0.1.tar
cd into the newly created blackjack-0.1 directory
run the compile command make
run the install command make install
Once the make install command finishes, just stay in the directory in which you ran the installation and run ./blackjack to run the program.
Now the short form (NOTE: The make ; make install command must be run as rootŸso to move ahead you must have root privileges):
cd to the directory where you saved the blackjack-0.1.tgz file
run the command tar xvfz black*
cd into the newly created blackjack-0.1.tgz directory
run the command make ; make install
Now, you are free to run the application as you would have above, and you can do it with fewer steps than before!
There is often another step to be taken when installing from source. Generally, when you run the second step of cding into the newly created directory, you should first read the README or the INSTALL files. README and INSTALL are very good roadmaps that will guide you through the entire process from installation to execution. However, there is a quick and dirty way to discern the steps involved in installing the application. After cding into the new directory, take a look around. If you see a configure file (it's actually a binary file) you know that you have to take this additional step. With a configure file in play, you have to run the ./configure command so that a Makefile is generated. A Makefile is the file that allows the application to compile, and when your Makefile is generated by configure, you know your Makefile is specific to your architecture and your environment. After the Makefile is generated, you will then go about the compilation steps outlined above.
If, when looking around in the new directory, you do not see a configure file and you see a Makefile, you know that you are ready for the compilation. NOTE: There are instances when the Makefile will need to be edited for certain variables. It is crucial that you take a look at the README and INSTALL files for this very reason. For instance, take majordomo. In order to get this application installed, there are approximately five variables to be edited within the Makefile. This will be detailed in either the README or INSTALL files. Read these files!
So, you've now installed your first application from its source code. Don't you feel like a new and improved person? With your newly boosted intelligence, you can go out there and track down more applications and install them from source. Soon you'll be installing your kernel from source and creating your own Linux distribution. Good luck!
Jack Wallen, Jr. is pleased to have joined the TechRepublic staff as editor –in chief of Linux content. Prior to his headfirst dive into the computer industry, he was a professional actor with film, TV, and Broadway credits. Now Jack is content with his new position as Linux Evangelist.If you'd like to share your opinion, please post a comment at the bottom of this page or send the editor an e-mail.
Jack Wallen is an award-winning writer for TechRepublic and Linux.com. He’s an avid promoter of open source and the voice of The Android Expert. For more news about Jack Wallen, visit his website getjackd.net.