DEV Community

Cover image for Linux make install command
Kinjal
Kinjal

Posted on

8 1

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:

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (1)

Collapse
 
tilkinsc profile image
Cody Tilkins •

You should add a .so into the mix here.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay