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

Subscribe to Xmodulo

Do you want to receive Linux FAQs, detailed tutorials and tips published at Xmodulo? Enter your email address below, and we will deliver our Linux posts straight to your email box, for free. Delivery powered by Google Feedburner.

The following two tabs change content below.
Dan Nanni is the founder and also a regular contributor of Xmodulo.com. He is a Linux/FOSS enthusiast who loves to get his hands dirty with his Linux box. He likes to procrastinate when he is supposed to be busy and productive. When he is otherwise free, he likes to watch movies and shop for the coolest gadgets.
Your name can also be listed here. Write for us as a freelancer.

7 thoughts on “How to create a self-extracting archive or installer in Linux

  1. I think the most useful place for SFX is for easily installing programs for my own use (in my own home directory) rather than needing to have root (or sudo) access). It would be nice if it also included (by default) an 'uninstall' option. One of my biggest complaints on linux is when I install a program without any available .deb file (e.g. newest version of Adobe Acrobat Reader) and not being sure how to cleanly uninstall it, especially if root (or sudo) was needed to install it in the first place.

    To be honest, while using .deb, .rmp, Pacman, .tgz (Slackware) etc. to install the applications I want, is convenient and relatively save, actually producing (and testing) all of these versions (and others like PETget or Equo) for my new Linux program is a pain!

    It might be nice (in terms of security) if makeself would by default check and ensure that the resulting SFX can never be run as root? Since root can always do everything on Linux/UNIX this will probably not be possible or really useful against malicious programmers, but might at least prevent some accidental security issues (i.e. overwriting some .so file with a version that is incompatible with the distro default).

  2. (Win-)RAR is far superior compression and feature wise to every archival tool available under Linux, recently tested.

    And the installation of external programs (NOT from reprositories) is THE key feature of personal computing. Linux as Unix misses this feature up to now, and stays therefore in the IT medieval of the seventies with mainframes and workstations.

  3. Call me old fashioned, but why do I need an sfx on Linux? There are much cleaner ways for installing software on every Linux distribution.?

  4. The best way is "don't do it." Those are huge security risks and only ever got popular in the first place because Windows doesn't ship with proper compression or archival tools.

    • I agree that the main problem of self-extracting archives, especially blob installers run with superuser privilege, is their potential security risks. You will have to trust whoever provides SFX archives.

  5. Thank you!
    Your post was very helpful (to find makeself at first, and to use it after).
    I was creating an installer for Virtual Volumes View, and makeself was a great help at the way to distribute the resulting self extracting and installation archive.
    My installer was a text installer and now acts (almost) like Nvidia ones.
    Thanks again for the guidance.

Leave a comment

Your email address will not be published. Required fields are marked *