---
title: Single-file scripts that download their dependencies · DBohdan.com
updated: 2023-01-15 15:15:30Z
created: 2023-01-15 15:15:30Z
source: https://dbohdan.com/scripts-with-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](https://docs.python.org/3/tutorial/venv.html) 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](https://dbohdan.com/contact).
## Contents
- [Anything with a Nix package](#anything-with-a-nix-package)
- [D](#d)
- [Groovy](#groovy)
- [JavaScript (Deno)](#javascript-deno)
- [Kotlin (kscript)](#kotlin-kscript)
- [Racket (Scripty)](#racket-scripty)
- [Scala (Ammonite)](#scala-ammonite)
## Anything with a Nix package
The Nix package manager can [act as a `#!` interpreter](https://nixos.org/manual/nix/stable/command-ref/nix-shell.html#use-as-a--interpreter) and start another program with a list of dependencies available to it.
```
#! /usr/bin/env nix-shell
#! nix-shell -i python3 -p python3
print("Hello, world!".rjust(20, "-"))
```
## D
D’s official package manager DUB supports [single-file packages](https://dub.pm/advanced_usage).
```
#! /usr/bin/env dub
/+ dub.sdl:
name "foo"
+/
import std.range : padLeft;
import std.stdio : writeln;
void main() {
writeln(padLeft("Hello, world!", '-', 20));
}
```
## Groovy
Groovy comes with an embedded [JAR dependency manager](http://docs.groovy-lang.org/latest/html/documentation/grape.html).
```
#! /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](https://deno.land/) downloads dependencies like a browser. Deno 1.28 and later can also import from NPM packages. Current versions of Deno require you to pass a `run` argument to `deno`. One way to accomplish this from a script is with a form of [“exec magic”](https://wiki.tcl-lang.org/page/exec+magic). Here the magic is modified from a [comment](https://github.com/denoland/deno/issues/929#issuecomment-429004626) by Rafał Pocztarski.
```
#! /bin/sh
":" //#; exec /usr/bin/env deno run "$0" "$@"
import leftPad from "npm:left-pad";
console.log(leftPad("Hello, world!", 20, "-"));
```
On Linux systems with [recent GNU env(1)](https://coreutils.gnu.narkive.com/e0afmL4P/env-add-s-option-split-string-for-shebang-lines-in-scripts) and on [FreeBSD](https://www.freebsd.org/cgi/man.cgi?env) you can replace the magic with `env -S`.
```
#! /usr/bin/env -S deno run
import leftPad from "npm:left-pad";
console.log(leftPad("Hello, world!", 20, "-"));
```
## Kotlin (kscript)
[kscript](https://github.com/holgerbrandl/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](https://docs.racket-lang.org/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](https://ammonite.io/#IvyDependencies).
```
#! /usr/bin/env amm
import $ivy.`org.apache.commons:commons-lang3:3.12.0`,
org.apache.commons.lang3.StringUtils
println(StringUtils.leftPad("Hello, world!", 20, "-"))
```
* * *
Tags: [list](https://dbohdan.com/tags#list), [programming](https://dbohdan.com/tags#programming).