Embed Unrar into a Custom Linux Distribution

This article provides a direct guide on embedding the unrar utility into a custom Linux distribution, covering source acquisition, licensing considerations, cross-compilation, and root filesystem integration. Whether you are building an embedded system with tools like Buildroot and Yocto or assembling a rootfs from scratch, integrating unrar ensures your system can natively extract RAR archives.

1. Choose Between Proprietary Unrar and Unrar-Free

Before embedding the binary, determine which version fits your distribution’s licensing model:

  • Official Unrar (RARLAB): Freeware, proprietary source code. It supports RAR5 and newer archive formats, but its license prohibits using the code to re-create the RAR compression algorithm.
  • Unrar-Free: GNU GPL-licensed utility that uses libarchive. It is fully open-source but lacks support for RAR5 archives.

For full compatibility with modern RAR files, the official RARLAB source is standard.

2. Manual Compilation and Rootfs Integration

If you build your distribution using a custom toolchain or a minimal root filesystem (rootfs), compile unrar manually.

Step 1: Download the Source

Fetch the source code directly from the official source archive:

wget https://www.rarlab.com/rar/unrarsrc-6.2.12.tar.gz
tar -xzf unrarsrc-6.2.12.tar.gz
cd unrar

Step 2: Compile (Native or Cross-Compilation)

For a native build, run:

make -f makefile

For cross-compiling targeting a different architecture (e.g., ARM, MIPS) or standard library (e.g., musl):

make -f makefile CXX=<cross-compiler-prefix>-g++ STRIP=<cross-compiler-prefix>-strip

To avoid runtime dependency issues with libstdc++ or mismatched libc versions on minimal systems, link statically:

make -f makefile LDFLAGS="-static"

Step 3: Copy to Rootfs

Install the generated binary into your target root filesystem image:

install -m 755 unrar /path/to/target/rootfs/usr/bin/

3. Integrating with Build Systems

If you use an automated embedded Linux build system, leverage their package management mechanisms.

Buildroot Integration

Create a custom package under package/unrar/:

  1. package/unrar/Config.in:
    config BR2_PACKAGE_UNRAR
        bool "unrar"
        depends on BR2_INSTALL_LIBSTDCPP
        help
          RAR extraction utility.
  2. package/unrar/unrar.mk:
    UNRAR_VERSION = 6.2.12
    UNRAR_SOURCE = unrarsrc-$(UNRAR_VERSION).tar.gz
    UNRAR_SITE = https://www.rarlab.com/rar
    UNRAR_LICENSE = RARLAB
    UNRAR_LICENSE_FILES = license.txt
    UNRAR_REDISTRIBUTE = NO
    
    define UNRAR_BUILD_CMDS
        $(TARGET_MAKE_ENV) $(MAKE) -C $(@D) -f makefile \
            CXX="$(TARGET_CXX)" STRIP="$(TARGET_STRIP)"
    endef
    
    define UNRAR_INSTALL_TARGET_CMDS
        $(INSTALL) -D -m 0755 $(@D)/unrar $(TARGET_DIR)/usr/bin/unrar
    endef
    
    $(eval $(generic-package))

Yocto Project / OpenEmbedded

Add the recipe provided in the meta-openembedded layer (meta-oe/recipes-extended/unrar/unrar_*.bb) to your local.conf or distribution image recipe:

IMAGE_INSTALL:append = " unrar"

Because of the RARLAB proprietary license, you may also need to whitelist the non-commercial license flag in your local.conf:

LICENSE_FLAGS_ACCEPTED += "commercial_unrar"

4. Verification

After generating and booting your distribution image, verify that the binary executes properly and links all shared dependencies:

ldd /usr/bin/unrar
unrar --version

If using a minimal shell environment without dynamic linkers, confirm that unrar was built as a static binary by ensuring file /usr/bin/unrar reports statically linked.