Scripts that download their dependencies

An ideal distributable script is fully contained in a single file. It runs on any compatible operating system with an appropriate language runtime. It is plain text, and you can copy and paste it. It does not require mucking about with a package manager, or several, to run. It does not conflict with other scripts’ packages or require managing a project environment to avoid such conflicts.

The classic way to get around all of these issues with scripts is to limit yourself to using the scripting language’s standard library. However, programmers writing scripts don’t want to; they want to use libraries that do not come with the language by default. Some scripting languages, runtimes, and environments resolve this conflict by offering a means to download and cache a script’s dependencies with just declarations in the script itself. This page lists such languages, runtimes, and environments. If you know more, drop me a line.


Anything with a Nix package

The Nix package manager can act as a #! interpreter and start another program with a list of dependencies available.

#! /usr/bin/env nix-shell
#! nix-shell -i python3 -p python3
print("Hello, world!".rjust(20, "-"))


D’s official package manager DUB supports single-file packages.

#! /usr/bin/env dub
/+ dub.sdl:
name "foo"
import std.range : padLeft;
import std.stdio : writeln;
void main() {
    writeln(padLeft("Hello, world!", '-', 20));


Groovy comes with an embedded JAR dependency manager.

#! /usr/bin/env groovy
@Grab(group='org.apache.commons', module='commons-lang3', version='3.12.0')
import org.apache.commons.lang3.StringUtils
println StringUtils.leftPad('Hello, world!', 20, '-')

JavaScript (Deno)

Deno downloads dependencies like a browser. Recent versions require “exec magic” to pass the run argument to /usr/bin/env deno. Here the magic is modified from a comment by Rafał Pocztarski. JSPM makes it possible to import npm packages meant for Node.js in the browser and thus Deno.

#! /bin/sh
":" //#; exec /usr/bin/env deno run "$0" "$@"
import leftPad from "";
console.log(leftPad("Hello, world!", 20, "-"));

Kotlin (kscript)

kscript is an unofficial scripting tool for Kotlin that understands several comment-based directives, including one for dependencies.

#! /usr/bin/env kscript
//DEPS org.apache.commons:commons-lang3:3.12.0
import org.apache.commons.lang3.StringUtils
println(StringUtils.leftPad("Hello, world!", 20, "-"))

Racket (Scripty)

Scripty interactively prompts you to install the missing dependencies for a script in any Racket language.

#! /usr/bin/env racket
#lang scripty
#:dependencies '("base" "typed-racket-lib" "left-pad")
#lang typed/racket/base
(require left-pad/typed)
(displayln (left-pad "Hello, world!" 20 "-"))

Scala (Ammonite)

The scripting environment in Ammonite lets you import Ivy dependencies.

#! /usr/bin/env amm
import $ivy.`org.apache.commons:commons-lang3:3.12.0`,
println(StringUtils.leftPad("Hello, world!", 20, "-"))

Tags: list, programming.