Setting up a Julia Package

In Julia, packages are the natural medium for code that doesn't fit in a simple script or notebook. While this might sound excessive at first, it provides many conveniences. This page serves as a guide for creating your first Julia package. It walks you through the process of the initial setup and the structure of a Julia package.

Table of Contents

PkgTemplates.jl

PkgTemplates.jl is a highly configurable package for project templates. Thanks to these templates, setting up the file structure for a Julia package takes seconds.

We suggest you use the following template for your project work at TU Berlin:

using PkgTemplates

template = Template(;
    user="YourGitHubUsername",
    authors="FirstName LastName <YourEmail@campus.tu-berlin.de>",
    julia=v"1.10",
    plugins=[
        License(; name="MIT"),
        Git(; manifest=false),
        Tests(; project=true),
        GitHubActions(; x64=true, extra_versions=[v"1.10"]),
        Codecov(),
        Documenter{GitHubActions}(),
    ],
)

⚠️ Don't just Copy & Paste ⚠️

Make sure to customize the user and authors fields with your GitHub account, name, and email address!

Let's go through this template step by step:

We also use the following plugins to customize our template, most of which are defaults:

After you created the template, run

julia> template("MyPackage")

At the end of the package generation, Julia will inform us that our project has been created in the ~/.julia/dev folder:

[ Info: New package is at ~/.julia/dev/MyPackage

You can set a custom directory via the keyword argument dir of your template.

Tip

The template above was customized for the JuML project work at TU Berlin.

Take a look at the PkgTemplates user guide to create a template customized to your needs.

Project structure

Let's take a look at the structure of the files generated by PkgTemplates.jl by opening our project in VSCode. The file explorer on the left should show the following files:

├── .github
│   └── workflows
│       ├── CI.yml
│       ├── CompatHelper.yml
│       └── TagBot.yml
├── .gitignore
├── docs
│  ├── make.jl
│  ├── Manifest.toml
│  ├── Project.toml
│  └── src
│     └── index.md
├── LICENSE
├── Manifest.toml
├── Project.toml
├── README.md
├── src
│   └── MyPackage.jl
└── test
   ├── Manifest.toml
   ├── Project.toml
   └── runtests.jl

5 directories, 10 files

Let's go through this step by step:

Tip

We will describe the project structure in more detail in the page on Writing a Julia package.

Further reading

Last modified: October 09, 2024.
Website, code and notebooks are under MIT License © Adrian Hill.
Built with Franklin.jl, Pluto.jl and the Julia programming language.