How to create a self-extracting archive or installer in Linux

Last updated on December 15, 2020 by Dan Nanni

While a typical archive file relies on a separate program (e.g., tar, gunzip, 7z) to extract content from the archive file, a self-extracting (SFX) archive is an executable itself, and can self-extract its content simply upon running. A self-extracting installer does the same thing, but it also copies the extracted content to appropriate directories.

In this tutorial, I will explain how to create a self-extracting archive or installer on Linux.

For this purpose, you can use a command-line utility called makeself. The makeself tool is a shell script which creates a compressed TAR archive out of input directories/files, and adds a small shell script stub at the beginning of the archive to initiate self-extraction, and guide installation of extracted files.

Install makeself on Linux

To install makeself, download the latest version in an archive format, and extract the downloaded archive as follows. Once the archive has extracted itself, it will create a new directory called makeself-2.4.2. Copy all the shell scripts in the directory to /usr/bin.

$ wget https://github.com/megastep/makeself/releases/download/release-2.4.2/makeself-2.4.2.run
$ chmod 755 makeself-2.4.2.run
$ ./makeself-2.4.2.run
$ cd makeself-2.4.2
$ sudo cp *.sh /usr/bin

The basic usage of makeself.sh is as follows.

makeself.sh [options] [directory_to_package] [sfx_archive_filename] [label] [startup_script] [optional_script_args]

The label argument is the message to print while an SFX archive is uncompressed.

The startup_script argument specifies the script/command to launch after an SFX archive is successfully extracted. This is useful when you create a self-extracting installer. Typically a start-up script will copy/install the extracted content to appropriate target directories. The start-up script must be located inside the directory to package, so that the script is included in the SFX archive.

Here are some of available options for makeself.sh:

Create a Self-Extracting Archive

To create a self-extracting archive which contains all files inside ./backup directory, do the following. Here the start-up routine does nothing more than printing Extraction done.

$ makeself.sh --notemp ./backup ./backup.run "SFX archive for backup" echo "Extraction done"
Header is 403 lines long

About to compress 1540 KB of data...
Adding files to archive named "./backup.run"...
./
./jpeg/
./jpeg/1.jpg
.
.
CRC: 2238411397
MD5: 0b0fd3a2ba08ffcec821b9cbaa11b70d

Self-extractible archive "./backup.run" successfully created.

To extract files from the archive, simply execute the archive:

$ ./backup.run
Creating directory backup
Verifying archive integrity... All good.
Uncompressing SFX archive for backup.............
Done

Create a Self-Extracting Installer

If you want to create a self-extracting installer, you need to prepare a separate start-up script which will do the installation upon file extraction. Here I assume that the program directory to package is located at ./program. So prepare a start-up script inside ./program directory.

$ vi ./program/install.sh
#!/bin/sh
   
if [ -d $HOME/bin ]
then
    cp myprogram $HOME/bin/
fi

Then make the start-up script executable.

$ chmod 755 ./program/install.sh

Go ahead and create a self-extracting installer, and package the start-up script along with it as follows.

$ makeself.sh ./program ./program.run "SFX installer for program" ./install.sh

Support Xmodulo

This website is made possible by minimal ads and your gracious donation via PayPal or credit card

Please note that this article is published by Xmodulo.com under a Creative Commons Attribution-ShareAlike 3.0 Unported License. If you would like to use the whole or any part of this article, you need to cite this web page at Xmodulo.com as the original source.

Xmodulo © 2021 ‒ AboutWrite for UsFeed ‒ Powered by DigitalOcean