makefilerpmautotoolsautoconf

How to successfully run make install without root privileges


I'm trying to package an RPM of a systemd service written in C and managed by autoconf and autotools. I've learned that I should not build programs as root, which means that I should run make instead of sudo make, which is not a problem. However the next step, running make install, produces these errors:

make[3]: Nothing to be done for 'install-exec-am'.
 /usr/bin/mkdir -p '/dbg/etc/foo'
 /usr/bin/install -c -m 644 /home/vagrant/dev/lib/foo.cfg '/dbg/etc/foo'
/usr/bin/install: cannot remove '/dbg/etc/foo/foo.cfg': Permission denied
make[3]: *** [Makefile:538: install-dist_pkgconfDATA] Error 1
(...)

If I instead run sudo make install, the command completes without errors. The Makefile seems written to expect root privileges, because it has the lines:

prefix = /dbg
(...)
sysconfdir = ${prefix}/etc

How can I get make install to run successfully without root privileges?

For context, I'm ultimately trying to package an RPM, and so far the make rpm command fails with this error:

config.status: creating src/Makefile
config.status: error: cannot find input file: `.libgnu/gnu/Makefile.in'
error: Bad exit status from /var/tmp/rpm-tmp.lafSd8 (%build)

But perhaps this is because I've run sudo make install with root privileges?


Solution

  • How can I get make install to run successfully without root privileges?

    You cannot install to the usual system directories without privilege. When installing directly from source, the normal idiom is to build as yourself, then install as root:

    $ make
    $ sudo make install
    

    But that's moot for building RPMs, because during the RPM building process, you don't want to install to the regular system directories anyway. Instead, you want to install to the RPM build root. Or more precisely, you want the rpmbuild command to install to the build root, as scripted in the applicable RPM spec file.

    This is an absolute breeze for packages that have a build system that leverages Automake. The makefiles produced by such a system recognize an environment or make variable named DESTDIR. If set, its value is used as the path to a staging area, relative to which software is to be installed by make install. In the %install scriptlet of an RPM spec file, that might be used something like this:

    %install
    
    make install DESTDIR="$RPM_BUILD_ROOT"
    

    This facility is useful enough that it or an analogue has been picked up by other build tools, such as CMake, and is sometimes implemented even in hand-written makefiles.

    If your package's build system does not provide something like that then you have to be more creative. For example, you could

    , among other possibilities.

    But that is mostly about how you prepare an RPM spec file. Your previous question seemed to indicate that the software already had a mechanism for building RPMs, and if so, you shouldn't need to build a spec file yourself.

    For context, I'm ultimately trying to package an RPM, and so far the make rpm command fails with this error:

    config.status: creating src/Makefile
    config.status: error: cannot find input file: `.libgnu/gnu/Makefile.in'
    error: Bad exit status from /var/tmp/rpm-tmp.lafSd8 (%build)
    

    But perhaps this is because I've run sudo make install with root privileges?

    If your objective is to build an RPM then you probably don't need to perform a make install at all, with or without sudo. Probably not a plain make, either. Those would ordinarily be done under control of rpmbuild, which I guess your particular makefile is willing to run for you. With RPM file in hand, if you want to install the software on the build machine then you would use RPM tools (rpm or yum / dnf) to install it from the RPM (and this does require privilege).

    As for the error, it looks like a failure particular to the package, or to some detail of how you're trying to build the RPM from it. Do read whatever the package's build documentation has to say about using its RPM-building feature. The needed steps might be as simple as

    $ ./configure
    $ make rpm