In this guide, we are going to learn how to install programs from source on Ubuntu 18.04. More often than not, the easiest and hustle free method of installing programs on Ubuntu 18.04 or the whole Linux family is to use the respective distribution package manager. This ensures that you are installing a software that is optimized for your distribution. It also eliminates the issue of having to deal with annoying dependencies. However, there is a point in time when you need to install the latest version of a software to patch some bug or test a new feature or even a software that is not available on your distribution repositories. In this case, then you need to download the source code of the program, compile and install it.
Therefore, in this guide, you going to learn how to;
- Unpack source code using common compression and archive utilities
- Use
make
to compile programs - Apply parameters to a configure script
- Know where sources are stored by default
Install Programs from Source on Ubuntu 18.04
You first need to download the software tarball from the trusted sources before you can proceed.
Unpacking the Source Code
Opensource software is always distributed as a compressed tarball. The tarball contains the program source code which basically contains all the necessary scripts for compiling and installing the software.
There are various tools that can be used to compress the source code tarballs. The most common ones include gzip
, bzip2
, xz
. So how do you unpack the tarball that has been compressed using the various formats of the above tools.
Unpacking .tar.gz
or .tgz
tarballs
To unpack the tar.gz or .tgz, you would use any of the following commands;
Using gunzip
or gzip
.
gunzip -dc tarball.tar.gz | tar xf - gunzip -dc tarball.tgz | tar xf -
Using tar
command;
tar xzf tarball.tar.gz tar xzf tarball.tgz
Unpacking .tar.bz2
, .tar.bz
or .tbz
tarballs
Using bzip2
, bunzip2
.
bzip2 -dc tarball.tar.bz2 | tar xvf - bzip2 -dc tarball.tbz2 | tar xvf - bzip2 -dc tarball.tbz | tar xvf -
Using tar
command;
tar xjf tarball.tar.bz2 tar xjf tarball.tbz2 tar xjf tarball.tbz
Unpacking .tar.xz
or .txz
tarballs
Using unxz
or xz
command;
unxz -dc tarball.tar.xz |tar -xf - unxz -dc tarball.txz |tar -xf
Using tar
command;
tar xJf tarball.tar.xz tar xJf tarball.txz
Building a Program from source
Before you can build a program from the source, you need to install the GNU C compilers and libraries that are required for a successful compilation. Therefore, you need to install the build-essential package that provides all these compilation tools.
apt install build-essential
You also need to check list of dependencies that the program in question requires. This information can be obtained from the documentation of the program in question.
Once you have extracted source code from the tarball, you need to configure
, compile
and install
it. The preferred way of installing a package from source is usually included in the README or INSTALL files in the tarball. You can check these files on how to best build the specific package.
However, in most cases, a configuration script, configure
, contained in the source code directory is usually executed as shown below;
./configure
The common syntax of the configure script is;
./configure [OPTION]... [VAR=VALUE]...
The configuration script is generated using the autoconf
utility. This script adapts the software to the system. It also checks for availability of all the dependencies required to successfully build the software. The dependencies can either be optional or mandatory.
If an optional dependency is missing on the target system, the compilation of that dependency is disabled. However, if a mandatory dependency is missing from the target system, the script will print the related error and exit.
The configure script gives you the ability to optimize how compilation will happen. For example, you can change the installation path of the software by passing the --prefix=/path/
. /usr/local/
is usually the default installation path.
In order to find more options that can be used with the configure
script, run the command;
./configure --help
Compiling the software
The make
command is used to compile and install the software. When the configure script is run, it generates a file called Makefile
with a series of steps and all the information required to build and install the software. Hence to compile a program, just invoke the make
command.
make
Once the compilation is done, you need to invoke make command with the install
option in order to install the program files in their proper directories.
sudo make install
Note that you need to run this command with elevated privileges lest you get the permission denied errors due to lack of write permission to system directories.
Your program binaries will finally be installed to /usr/local/bin
if at all you didn’t change the default install path during configuration. If this directory is not in your path, you can either add the path to your PATH or create a symbolic link of your program from /usr/local/bin
to /usr/bin
.
If you however need to remove stale program object files or uninstall the program, you need to run the following commands below respectively. Note this is only possible if you have not deleted the source code directory.
make clean make uninstall
Well, that is all about how to install programs from source on Ubuntu 18.04. You should now comfortably build and install any software on Ubuntu 18.04. All the best.