Offline Typst Package Installation: A Simple Bash Script for Air-Gapped Environments

Hi everyone,

Sometimes we need to run Typst in an environment without internet access, which means we have to store packages locally. However, the Typst CLI doesn’t provide a built-in way to install packages from a local .tar.gz file. To work around this, I wrote a small Bash script that automates offline package installation on Linux. (For other platforms, you’d need to adjust the cache folder path.)

Let’s call it install_pkg.sh. After giving it execute permissions (chmod +x install_pkg.sh), you can install a Typst package with:

./install_pkg.sh zero-0.3.2.tar.gz

Below is the script. Feel free to adapt it to suit your needs.

#!/bin/bash
# Script to install a Typst package from a local tar.gz file

PACKAGE=$1
TARGET_DIR=~/.cache/typst/packages/preview

# Check if package file is provided
if [ -z "$PACKAGE" ]; then
  echo "Usage: $0 <package-file.tar.gz>"
  exit 1
fi

# Check if file exists
if [ ! -f "$PACKAGE" ]; then
  echo "Error: File not found!"
  exit 1
fi

# Extract package name and version from the tar.gz file name
PACKAGE_BASENAME=$(basename "$PACKAGE" .tar.gz)
PACKAGENAME=$(echo "$PACKAGE_BASENAME" | rev | cut -d'-' -f2- | rev)
PKGVERSION=$(echo "$PACKAGE_BASENAME" | rev | cut -d'-' -f1 | rev)

# Construct the full target directory path
FINAL_DIR="$TARGET_DIR/$PACKAGENAME/$PKGVERSION"

# Create target directory if it does not exist
if [ ! -d "$FINAL_DIR" ]; then
  echo "Target directory does not exist. Creating $FINAL_DIR..."
  mkdir -p "$FINAL_DIR"
fi

# Extract package
echo "Extracting package to $FINAL_DIR..."
tar -xvzf "$PACKAGE" -C "$FINAL_DIR"

echo "Package installed to $FINAL_DIR"

I’ll also be filing a Feature Request on GitHub, because having a built-in command for installing offline packages would be a great addition to Typst CLI.

Let me know if you find this script useful, or if you have any suggestions!

6 Likes