Change the Install directory with make install


Overview

Generally, packages are installed in the default directory. Depending on the system's Linux version, it might be necessary to use a different directory such as /usr or /user/local. We might also want to install a particular software application for a single user rather than for the entire system.

We'll take a look at how to change where packages get installed by running make uninstall.

Using ./configure Parameters

When using autoconf/automake to build software packages, a configure script with several standard parameters and (sometimes) additional custom parameters is provided. Some packages don't use autoconf but they usually offer a compatible ./configure script.

We can specify which directories make will create when we run ./configue by using parameters.

  • –prefix=<dir> − By default, this is usually /usr or usr/local, and it is the directory used for other settings.

  • –libdir=<dir> − This is the libraries directory, and it’s usually ${prefix}/lib or ${prefix}/lib64 by default

  • –bindir=<dir> − This is the executables directory, and it’s usually ${prefix}/bin by default

By default, the prefix parameter is used for both lib and bin directories. We can use the prefix argument to specify where we want to install our program.

When we want to ensure that a package follows our own structure, we can use GNU's diffutil package as an example. It installs to /usr/lib by default. Let’s set prefix to /usr, and libdir to /usr/lib64 −

$ ./configure --prefix=/usr --libdir=/usr/lib64

Since we didn't specify bindir, it defaults to /usr/bin because we specified prefix /usr. The prefix parameter doesn't apply to directories we don't set explicitly.

After running make install, the diffutil package will be installed into the desired locations. We're going to install to /usr so we'll need root privileges.

$ make install
$ ls -l /usr/bin/diff
-rwxr-xr-x 1 root root 1078184 Jun 6 11:21 diff

You can also change the installation directory so that it installs into your own personal folder instead of the system folder. Then, you won't need root privileges to run the installer.

We’ll start by cleaning up the installation directory using make clean and then configure it to be installed into /home/tpoint/diffutils/usr −

$ make clean
$ ./configure --prefix=/home/tpoint/diffutils/usr

When we change prefix, other parameters such us libdir and bindir use the /home/user/diffutil/usr as the base dirctory.

Using DESTDIR

When we run./configue, we're changing the building and installing processes, so the installed files would be using those paths. For example, the application might need to install resources such as icons, sound files, and so on, and it must be able to locate them. Let’s look for the prefix inside the directory where we installed diffutils using --prefix=/path/to/dir.

$ strings /home/tpoint/diffutils/usr/bin/diff | grep /home/tpoint/diffutils/usr
/home/tpoint/diffutils/usr/share/locale

We can see that strings were found in the path /home/user/bin/diffutils/usr inside the binary we just downloaded. It’s fine if we don’t install the new version at a different location.

We sometimes just want to install a program into another location without changing its internal folder structure.

An example would be when we want to create a tarball of an installed program and then copy it to another computer. If that happens, then we normally install the packages into an empty and temporary directory first. So, the package doesn't need to mention this temporary address.

For proper execution, we will use the DESTDIR=<dir> environment variable when running make install. This path will be added into all installation directories. We're going to uninstall diffutils from our system by removing its current installation and installing it again with DESTDIR.

$ rm -r /home/tpoint/diffutils
$ make clean
$ ./configure --prefix=/usr
$ make
$ make DESTDIR=/home/tpoint/diffutils install

Make will use /home/tpoint/diffutils as its installation location. We've configured DiffUtils to use /usr as its default directory. Here’s the result −

$ ls -l /home/tpoint/diffutils/usr/bin
total 1740
-rwxr-xr-x 1 tpoint users 191456 Jun 6 12:48 cmp
-rwxr-xr-x 1 tpoint users 1078184 Jun 6 12:48 diff
-rwxr-xr-x 1 tpoint users 300552 Jun 6 12:48 diff3
-rwxr-xr-x 1 tpoint users 204376 Jun 6 12:48 sdiff
$ strings /home/tpoint/diffutils/usr/bin/diff | /home/tpoint/diffutils

As we see, we installed the package in /home/tpoint/diffutils. This time, there is no reference to the directory specified by DESTDIR.

Using Makefile Parameters

If a package does not contain a ./configure file, it may provide a makefile that uses GNU conventions. In this case, the parameters used for ./configure can be followed.

If we want to set a variable with make, we’ll use the parameter variable=value. Let’s change the prefix to /usr and libdir to /usr/lib64

$ make clean
$ make prefix=/usr libdir=/usr/lib64
$ make prefix=/usr libdir=/usr/lib64 install

We need to recompile the package and then install it again using the same configuration.

Variables in the Makefile will be set up using./configure conventions, and the prefix variable will be used as the base location for other options.

We can now use DESTDIR in the same manner as we used PREFIX earlier. Doing so allows us to install the packages in any location without changing their structure. Let’s use prefix=/usr/local this time, and let’s add DESTDIR to install diffutils in /home/tpoint/diffutils −

$ rm -r /home/tpoint/diffutils
$ make clean
$ make prefix=/usr/local
$ make prefix=/usr/local DESTDIR=/home/tpoint/diffutils install

First we remove the destination folder in case there are old copies of the app from the previous install. Let’s take a look at the results −

$ ls -l /home/tpoint/diffutils/usr/local/bin
total 1740
-rwxr-xr-x 1 tpoint users 191456 Jun 6 22:59 cmp
-rwxr-xr-x 1 tpoint users 1078184 Jun 6 22:59 diff
-rwxr-xr-x 1 tpoint users 300552 Jun 6 22:59 diff3
-rwxr-xr-x 1 tpoint users 204376 Jun 6 22:59 sdiff
$ strings /home/tpoint/diffutils/usr/local/bin/diff | /home/tpoint/diffutils

We installed diffutilis in /home/baeldung/diffutils using the path /usr/local. There are no trace files for the DESTDIR directory within the binary.

Only DESTDIR is in upper case, while PREFIX, LIBDIR, etc., are in lower case.

We can also use the Makefiles' environment variable when the packages provide the ./configure script. If you're using ./configure, then the make variable will take precedence over any configure parameter.

Conclusion

We learned how to set the location where packages get installed when they're installed using make install.

Initially, we looked at executing the./configure command, then switched directories. If there is no./configure present, we can edit makefile variables instead.

Updated on: 03-Jan-2023

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements