DEV Community

Cover image for Linux make install command
Kinjal
Kinjal

Posted on

Linux make install command

While installing packages in linux, we come across the following command many times.

$ make install
Enter fullscreen mode Exit fullscreen mode

Let's understand this with a simple example.

install is a command that was written for software installation, but it can do more than that. make install will do whatever instruction is defined in makefile.

This example uses a sample hello world C program.

#include <stdio.h> 
int main() {
 printf("Hello, World!"); 
}
Enter fullscreen mode Exit fullscreen mode

Here is the directory structure of testapp which is created for this example.

$ ls testapp
installer.sh  makefile  testapp  testapp.c  testapp.conf 
Enter fullscreen mode Exit fullscreen mode

Let's take closer look at installer.sh

#!/bin/bash
ROOTDIR=${1:-/opt/testapp}
OWNER=${2:-testapp}
GROUP=${3:-testapp}

# Create bin and opt directories
install -v -m 755 -o $OWNER -g $GROUP -d $ROOTDIR/bin $ROOTDIR/etc
if [ "$?" -ne "0" ]; then
  echo "Install: Failed to create directories."
  exit 1
fi

# install binary
install -b -v -m 750 -o $OWNER -g $GROUP -s testapp $ROOTDIR/bin
if [ "$?" -ne "0" ]; then
  echo "Install: Failed to install the binary"
  exit 2
fi

# install configuration file
install -b -v -m 600 -o $OWNER -g $GROUP testapp.conf $ROOTDIR/etc
if [ "$?" -ne "0" ]; then
  echo "Install: Failed to install the config file"
  exit 3
fi

echo "installation completed.."
Enter fullscreen mode Exit fullscreen mode

All the chown install ect commands are defined in installer.sh and insatller itself is wrapped up in makefile so that makefile can be kept clean.

Here is makefile

all:    testapp.c
    $(CC) -o testapp     testapp.c

clean:
    rm -f testapp

install: testapp
    ./installer.sh /opt/testapp kiwi kiwi

purge:
    rm -rf /opt/testapp
Enter fullscreen mode Exit fullscreen mode

In the install command, kiwi refers to the username. You can have your custom name here.

And the content of sample conf file testapp.conf

$ cat testapp.conf
testapp=testapp
Enter fullscreen mode Exit fullscreen mode

Finally, running it.

$ sudo make install
./installer.sh /opt/testapp kiwi kiwi
'testapp' -> '/opt/testapp/bin/testapp' (backup: '/opt/testapp/bin/testapp~')
'testapp.conf' -> '/opt/testapp/etc/testapp.conf' (backup: '/opt/testapp/etc/testapp.conf~')
installation completed..
Enter fullscreen mode Exit fullscreen mode

testapp can be found in /opt directory if the installation is successful.

That's a very simple example. Here is one useful link for further reading.

Download the code here.

Reference:

Latest comments (1)

Collapse
 
tilkinsc profile image
Cody Tilkins

You should add a .so into the mix here.