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

While a typical archive file relies on a separate program (e.g., tar, gunzip) 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.

To install makeself on Linux, 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.1.5. Copy all the shell scripts in the directory to /usr/bin.

$ wget http://megastep.org/makeself/makeself-2.1.5.run
$ chmod 755 makeself-2.1.5.run
$ ./makeself-2.1.5.run
$ cd makeself-2.1.5
$ 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:

  • –gzip : Use gzip for compression (default option).
  • –bzip2 : Use bzip2 for compression.
  • –nocomp : Do not compress.
  • –notemp : Do not extract files into a temporary directory, but in a new sub-directory created in the current directory.
  • –follow : Follow all symbolic links, and archive files that are symbolic-linked.

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

email me