Most software installation in Linux is done via the inbuilt installers for software that has already been pre-packaged for that particular distribution. Different Linux flavors have their own preferred installers. Ubuntu has “apt-get”, CentOS has “yum”, and of course “rpm” is common to all of them (yum is actually just a wrapper around rpm).
These installers make it easy to find, install, and uninstall various packages in a predictable and systematic way. Yum for example, allows you to “undo” individual installations, get rid of packages that are no longer needed, takes care of dependencies etc. Due to all these advantages, it’s always recommended to get your software from a recognized repo if you can. It can save you a lot of headaches later on.
Sometimes however, the software doesn’t have a convenient file all ready to install on your particular Linux distribution. The only thing you have is the actual source code. In which case, the author might have created a “tar.gz” file bundled with the source code and it’s up to us to compile and install it on to our system. In this tutorial, I’ll show you how to do that, as well as how to get rid of program afterwards, if possible.
Step 1: Getting the tar.gz file and Unzipping it into a Folder
The first step is simply downloading the tar.gz file and extracting its contents into a folder – usually of the same name as the tar file itself. For this example, I’m going to make use of a sample “hello world” tar.gz file from gnu.org. It’s free software and is used for demo purposes. In this case, the software simply prints “hello world” onto the screen. It looks simple, but the source code is surprisingly complex!
So first we use a “get” command, and a “tar” command like this:
wget http://ftp.gnu.org/gnu/hello/hello-2.10.tar.gz tar -zxvf hello-2.10.tar.gz
The first command will download the file and the second will unzip it into a directory like this:
The contents of the new directory include the “configure” script that we’re going to use in the next step:
Every Linux admin has used these two commands over and over for several scripts. An easier way to do it might be to combine both of them using curl like this:
curl http://ftp.gnu.org/gnu/hello/hello-2.10.tar.gz | tar xz
You can see below how this works:
Step 2: Compiling and Installing the Source Code
Now that we have the source code, navigate into the directory and type in the following commands:
./configure make make install
The last command requires admin privileges, so make sure that you either use it with “sudo”, or that you run it as root to begin with. These commands can take a bit of time even for simple programs like our “Hello World” example. After the installation is complete, we can run the command from anywhere on our system like this:
And that’s how we install a package from source code in Linux! Of course you need to be careful as there can be extra steps to perform using the “make” command. Look for a file named “INSTALL” or “README” in the source code directory of the application for additional configuration parameters and compiling options.
Step 3: Uninstalling the Package
Unlike an rpm based package installer like yum or apt-get, we can’t just remove software installed from source code using a single command from anywhere. And there’s no guarantee that the creator of the package has even provided an uninstall mechanism! Having said that, most well made tar.gz source files do provide an uninstall script. To do this, we first need to navigate into the directory containing the source code and type in the following:
If the uninstall provision exists, the software will be removed from your system. As you can see below, the “hello” command no longer works after uninstallation:
But be careful! Installing from source code can get messy if no uninstall functionality is provided. In the next tutorial, I’ll show you an alternative way of installing from source so that we can always uninstall it with minimum fuss.