How To Create Julia Packages

I wanted to put all the code for the different algorithms for QR decompositions into a Julia package, so that I could reuse the functions by simply entering using QRGramSchmidt in julia. This requires that your code is grouped into a package. In Matlab, you would more or less create some .m files and put them into a folder from your Matlab path. The process is simple and straightforward but tracking dependencies can be difficult, especially when you want to distribute the package to your colleagues. My personal experience with Python is probably best described by this xkcd comic.

Julia provides currently two ways to create packages. Either by using the builting functionality from Pkg or by using the package PkgTemplates.

Using Pkg.jl

The process on how to create packages is described in the official documentation. Also, it doesn’t hurt to read the sections about code loading and about the load path.

Creating a simple package is straightforward. In your Julia command line press ] to enter the pkg mode. Your shell prompt should now start with pkg>. Let’s assume we want to create a package named FOO. In that case we enter

generate FOO

and you should get an output similar to

 Generating project FOO:

There should be a folder named FOO in your current working directory now that contains the listed files. The file project.toml contains some meta information like name and version. The FOO.jl file contains a dummy module with a simple function. Your code should go into this file.

I have used this method to create the packages for my orthogonalisation algorithms.

Adding dependencies

Let’s say you want to add the LinearAlgebra package as a dependency to your package FOO. First you move your working directory to FOO and enter the pkg mode again. Then enter activate . to activate your current project. Your prompt should change to (FOO) pkg>. Now enter add LinearAlgebra. This will update the Project.toml file with the new dependency and generate a Manifest.toml file with the dependency structure of your dependencies. The package LinearAlgebra for example depends on Libdl, which you can see in the Manifest.toml file.

Using PkgTemplate.jl

Creating a package FOO is straightforward. Just enter

using PkgTemplates

There will be an interactive prompt where you can enter the necessary data for your package. Once you are done, you just have to generate a package from the template that you just created. Therefore, enter t("FOO") on your command line to generate the folder structure with all necessary files.

Note that PkgTemplate.jl is a lot fancier than Pkg.jl and provides functionality to set up unit testing for example.

Mathematician and
Software Engineer

Researcher, Engineer, Tinkerer, Scholar, and Philosopher.