While yum, apt-get, rpm are very handy to install a package that is already compiled, you still might encounter some situations where you have to install a software from source code.
This article explains on a very high level how to compile and install a software from source code.
Download the Source Code Package and Unpack it
The source code for software on Linux comes in the form of compressed tar files, which typically have either .tar.gz or .tar.bz2 extensions. The tools that are used for packing the source code into these tar balls are ‘tar’ (used for combining multiple files into one), ‘gzip’ or bzip2 (used for compression). To fetch the source code tarball for a particular software you need to know the URL to the tarball.
Once you have the download link, use ‘wget’ to fetch the tarball from command line.
$ wget <link to the tarball>
The above command will download the tarball into the current directory. wget command is very flexible and has lot of options. To learn more about wget, refer to the 15 wget examples.
Next you needs to unpack the tarball in order to get access to the source code and other files. Depending on the extension, use one of the following commands:
$ tar -xvfz <name of tarball with .tar.gz extension> (or) $ tar -xvfj <name of tarball with tar.bz2 extension>
tar command is very flexible and has lot of options. To learn more about tar, refer to the 10 tar examples.
Read Install Documentation
Once the software source code is downloaded and extracted, the very first thing that one should do is to go through the documentation. This may sound boring to most of us but this is a very important step as doing this step thoroughly would save you from most of the future problems. The documentation provides information about the software, changes since last version, links to more documentation, information regrading the author of the software, steps for compilation and installation of software etc. So we can see that lots of valuable information is present in the documentation.
This whole information is broadly divided into two files : ‘Readme’ and ‘Install’. While ‘Install’ covers all the information required for compilation and installation, all the other information is covered in the ‘Readme’ file. Please note that the name of file and it case may vary.
Configuration
Once the above step is over then we can assume that we have sufficient theoretical knowledge about this software and now we can move forward and configure the environment for compiling and installing the software on our system. Most of the packages come along with a configuration script that can be used for configuring the environment. The file name for configuration file is mostly ‘configure’. This script usually accepts parameters that can be used to control some features of this software. Also this script makes sure that all the tools required for compilation are present in the system.
To learn about the options provided by a specific configuration file, run the following command:
$ configure --help
To start configuring the build environment, execute the following command :
$ ./configure
The above command will check and/or create the build environment and if everything goes fine then it produces a file called ‘makefile’. The file ‘makefile’ is used in the compilation of the software.
Compilation
Once the makefile is generated, then in the same directory just run the following command:
$ make
The above command will compile all the source code related to the software. If compilation encounters some problem then error is thrown on the console.
Installation
Once the compilation is done successfully then all the required binaries are created. Now is the time to install these binaries in the standard paths so that they can be invoked from anywhere in the system. To do this run the following command :
$ make install
Note that some times installing the software may require root privileges, so one may gain the rights and then proceed with the above command.
The above 5 steps show how to fetch, unpack, configure, compile and install the software from source. Additionally one could do some cleanup by removing the directory created while unpacking the software tarball.
The following articles are few examples on how to install a software from source code.
- How to Install Apache 2.4.2 from Source on CentOS 6.2 with SSL
- How to Install and Configure Nginx from Source on Linux
- Instruction Guide to Install PHP5 from Source on Linux
- Tripwire Tutorial: Linux Host Based Intrusion Detection System
- HowTo: 7 Steps for Linux vsftpd Install, Configuration, Users Setup
While compiling and installing open source software from source, there could be some issues/errors that may come up. Lets look at a few of those here:
- Missing shared library: Sometimes when you run the program you just installed, you get an error related to some .so that your program is not able to find. Firstly, .so are synonymous to the DLLs we have in windows. These are shared libraries that are required by the program. Secondly, these type of errors erupt when your program is installed in some non-standard path or the shared library is actually not present in your system. For the first case, you need to tell the shell environment the path at which these new shared libraries are installed. This can be done by using the ‘ldconfig’ command or by modifying the LD_LIBRARY_PATH variable.
- Broken source code: No matter how much pain you take by going through all the documentation and covering all the steps building the software but if the source code gives some compilation error then it very much means that the software has broken source code. Nothing much can be done in this case except referring this problem back to the author of this software. Meanwhile, if you think you can you may debug the errors and see if these are trivial errors that can be fixed (like syntactical errors).
- No configure script: Though rare, but sometimes you’ll find that there is no configuration script present in the source code directory. If this happens that does not mean that you are stuck. In this case all you need is to go through the documentation in detail and there you will definitely find some information regarding configuration of environment for compiling and installation of software.
Comments on this entry are closed.
Actually, it’s more useful to use ‘checkinstall’ instead of ‘make install’.
This command will create a package for your system out of the sources and install it via native methods. It’ll make package update and removal easier.
Thank you for this handy quick guide TGS !
Thank you.
Due to memory loss (strokes) issues I need to have many “cheat sheets.”
This is a valuable article for me to add to my collection.
Hi,
Thanks a lot for very nice article…
Thanks a lot.
Thanks for the details article..
Sometime for some packages configure script may not be present , we might need to generate them using autoreconf.
http://en.wikipedia.org/wiki/GNU_build_system
Maybe this can be a new topic.
Tips:
You can do that to minus time for compiling :
# make -jX
Where ‘X’ is the number of thread.
Thanks man. But I’ve failed 99.99% of all the times I’ve tried to install software from source. It’s a nightmare to me and my worst experience with Linux.
What are the uninstall options with make install?
Thanks for the article.
Hi,
Thanks, I am new to linux and learning the command. This is so informative about install from source. Thanks again 🙂
How does one update a software installed via source?
I used your tutorial to install Joe Editor on a Amazon Linux AMI.
I have to use Sudo on these AMIs. How do I make the compiled program run under sudo?
in step one, you said using the following command:
$ tar -xvfz
I tried it, and got this error:
tar: z: Cannot open: No such file or directory
I asked and searched around, seems the filename must follow f argument immediately like this:
$ tar -xvzf
hope you check it. and thanks for this very good tutorial!