Commands.page Logo

How to Create a Self-Extracting Archive in Ubuntu

This guide explains how to create a self-extracting archive in Ubuntu using command-line tools. You will learn to package files into a single executable script that automatically unpacks itself without requiring additional software. We cover the most reliable method using the makeself utility to streamline your file distribution process.

Install the Makeself Utility

The standard tool for creating self-extracting archives on Linux is makeself. It is not always available in the default repositories, so you should download the latest script directly from the official source. Open your terminal and run the following commands to clone the repository and make the script executable:

git clone https://github.com/megastep/makeself.git
cd makeself
chmod +x makeself.sh

Prepare Your Files

Organize the files you wish to archive into a single directory. For this example, assume you have a folder named my_program containing all the necessary files. Ensure that any installation or startup script you want to run automatically after extraction is inside this folder and has executable permissions.

Create the Archive

Navigate to the directory where you cloned makeself. Use the following syntax to generate the self-extracting archive:

./makeself.sh [archive_dir] [file_name] [label] [startup_script]

Replace the brackets with your specific details. For example, to create an archive named install.sh from the my_program folder that runs setup.sh upon extraction, use:

./makeself.sh my_program install.sh "My Program Installer" ./setup.sh

This command creates a single file called install.sh. This file contains both the compression header and your compressed data.

Run the Self-Extracting Archive

You can now distribute the install.sh file to any Ubuntu machine. To run it, the user only needs to make it executable and launch it. They do not need to install makeself or any archiving tool to extract the contents. Execute the following commands to test the archive:

chmod +x install.sh
./install.sh

The script will verify the integrity of the archive, extract the files to a temporary or specified directory, and automatically execute the startup script you defined during creation.