Package management for Jsonnet files

Posted in Build on June 5, 2023 by Denys Dushyn ‐ 3 min read

Package management for Jsonnet files

This article is part 2 of the articles about dependency management for Jsonnet files and projects.

The series consists of the following parts:

  1. Dependency and package management for Jsonnet files (jb tool).
  2. The Structure and the purpose of the jsonnetfile.json file
  3. Transitive dependencies, legacy imports, local vs. remote dependencies

Dependency management

Jsonnet Bundler is a tool that simplifies the management of dependencies in Jsonnet projects. Here are some reasons why you might consider using Jsonnet Bundler:

  • Simplified dependency management Jsonnet Bundler allows you to manage your project’s dependencies in a single file, called jsonnetfile.json.

  • Automatic installation and updating of dependencies. Jsonnet Bundler allows installing and updating dependencies specified in jsonnetfile.json.

  • Version pinning. Jsonnet Bundler allows you to specify exact version numbers for your dependencies in jsonnetfile.json. This ensures that your project uses the correct versions of each dependency, even if newer versions are released.

  • Repeatable builds. By using Jsonnet Bundler to manage your project’s dependencies, you can ensure that your builds are repeatable and consistent across different environments.

  • Integration with GitOps workflows. Jsonnet Bundler can be integrated with GitOps.

There are several ways how to install jsonnet-bundler. In the article you will find 2 ways: binary(pre-built) and from the source code.

Jsonnet Bundler installation (binary, pre-built)

To install the Jsonnet Bundler tool, you will need to follow a few steps.

1. Install the Jsonnet Bundler binary (pre-built)

The easiest way to install jsonnet-bundler is by downloading the pre-built binary for your operating system.

Open your web browser and go to the Jsonnet Bundler GitHub repository.

Navigate to the Releases section and download the binary file suitable for your platform (e.g., jb_<version>_darwin_amd64 for macOS). Save the binary file in a directory accessible from the command line.

2. Add the Jsonnet Bundler to your PATH

After downloading the binary, you need to add it to your system’s PATH environment variable to make it accessible from any location in your command line interface.

This step may vary depending on your operating system. For example, on macOS and Linux, you can open your terminal and edit your shell’s configuration file (e.g., ~/.bashrc, ~/.bash_profile, or ~/.zshrc) using a text editor like nano or vi.

Add the following line at the end of the file, replacing /path/to with the actual path to the directory where you saved the jb binary:

export PATH=/path/to:$PATH

Save the file and reload your shell’s configuration by either restarting your terminal or running the command source ~/.bashrc (or ~/.bash_profile or ~/.zshrc depending on the file you edited).

3. Verify the installation

To verify that the Jsonnet Bundler is installed correctly, open a new terminal window or tab, and run the following command:

jb --version

If everything is set up correctly, you should see the version number of the Jsonnet Bundler printed in the terminal output.

Jsonnet Bundler installation (source code)

1. Install the Go programming language

The Jsonnet Bundler is written in Go, so if you want to install it from source, you’ll need to have Go installed on your system.

Visit the official Go website and follow the installation instructions for your operating system.

2. Install the Jsonnet Bundler tool

go install -a github.com/jsonnet-bundler/jsonnet-bundler/cmd/jb@latest

NOTE: please use a recent Go version to do this, ideally Go 1.13 or greater.

This will put jb in $(go env GOPATH)/bin. If you encounter the error jb: command not found after installation then you may need to add that directory to your $PATH as shown in their docs.

Congratulations!

You have successfully installed the Jsonnet Bundler tool. You can now use it to manage dependencies in your Jsonnet projects, such as fetching and resolving external libraries, versioning, and more. Refer to the Jsonnet Bundler documentation and tutorials to learn how to utilize its features effectively and enhance your Jsonnet development workflow.