Distribute Shell Scripts with .deb or .rpm

Package your scripts for distribution.


If you have written a useful shell script, you may want to distribute it to users in a standardized, reliable way. Relying on raw script downloads or copy-paste can be error-prone and hard to maintain. Instead, converting your scripts into .deb (for Debian/Ubuntu) or .rpm (for Red Hat/Fedora) packages makes installation, updates, and removal much easier for users. In this guide, we walk through the process of packaging a shell script using both formats.


Why Package Your Scripts?

  • Consistency: Packages ensure your script installs in the right place, with correct permissions.
  • Versioning: Users can easily upgrade or downgrade with the package manager.
  • Dependencies: Specify needed software so users get everything required.
  • Removal: Uninstall cleanly without leftover files.

Step 1: Prepare Your Shell Script

Let’s assume your script is called myscript.sh.

  • Make sure your script has a proper shebang, e.g., #!/bin/bash
  • Test the script thoroughly.
  • Optionally, provide a man page or README.
#!/bin/bash
echo "Hello, world! This script is now packaged."

Step 2: Directory Structure

Both .deb and .rpm packages expect files to be arranged how they should appear on the target system. Commonly, scripts go in /usr/local/bin or /usr/bin.

Example folder structure:

mypackage/
├── DEBIAN
│   └── control
└── usr
    └── local
        └── bin
            └── myscript

Copy your script, without extension, into usr/local/bin/myscript and make it executable:

chmod +x usr/local/bin/myscript

Step 3: Packaging as .deb

1. Create the Control File

Inside the DEBIAN directory, make a control file:

Package: myscript
Version: 1.0.0
Section: utils
Priority: optional
Architecture: all
Maintainer: Your Name <youremail@example.com>
Description: My sample shell script

2. Build the Package

Navigate to the parent directory and build:

dpkg-deb --build mypackage

This creates mypackage.deb. You can install it with:

sudo dpkg -i mypackage.deb

Step 4: Packaging as .rpm

You’ll need the rpm-build tools installed. Red Hat-based systems expect a specific tree under ~/rpmbuild/.

1. Setup the Folder Structure

mkdir -p ~/rpmbuild/{SOURCES,SPECS,BUILD,RPMS,SRPMS}

Copy your script (myscript) into ~/rpmbuild/SOURCES/.

2. Create a SPEC File

Create myscript.spec in ~/rpmbuild/SPECS/:

Name:           myscript
Version:        1.0.0
Release:        1%{?dist}
Summary:        My sample shell script

License:        MIT
BuildArch:      noarch

%description
My sample shell script.

%prep

%build

%install
mkdir -p %{buildroot}/usr/local/bin
cp %{SOURCE0} %{buildroot}/usr/local/bin/myscript
chmod 0755 %{buildroot}/usr/local/bin/myscript

%files
/usr/local/bin/myscript

%changelog
* Wed Jun 12 2024 Your Name <youremail@example.com> - 1.0.0-1
- Initial package

3. Build the RPM

cd ~/rpmbuild/SPECS
rpmbuild -ba myscript.spec

The .rpm will be in ~/rpmbuild/RPMS/noarch/. Install with:

sudo rpm -i ~/rpmbuild/RPMS/noarch/myscript-1.0.0-1.noarch.rpm

Step 5: Distribute and Maintain

Share your built .deb or .rpm files, or set up your own repositories for easier user access. For more complex scripts, add dependency declarations, configuration files, or pre/post install scripts as needed.


Conclusion

Packaging your shell scripts as .deb or .rpm brings them into the workflow most Linux users expect for software installation. Not only does this make your scripts more professional and easier to use, it also improves maintainability—which means you can share your script with the world, secure in the knowledge it will Just Work.


Further Reading: