Creating Julia Packages
October 31, 2025
It is entirely possible to write all your Julia code in a single script file. However, as your projects grow in size and complexity, organizing your code into packages can provide significant benefits. Today I’ll discuss how to create and manage Julia packages effectively.
What Makes a Julia Package?
You should be comfortable creating a Julia environment whenever you
start a new project. An environment has a Project.toml file
that lists the dependencies for your project, and by default only has a
[deps] section. A package will have four additional
keywords in the Project.toml file:
name: The name of your package.uuid: A unique identifier for your package.version: The current version of your package.authors: A list of authors who have contributed to the package.
While you can add these to the top of a Project.toml
file manually, it’s much easier to use the built-in package manager in
Julia. To create a new package, you can use the following command in the
Julia REPL:
julia> ]
pkg> generate("MyPackage")This will create a directory structure in your current working
directory with the necessary files and folders for a Julia package. This
folder will include a src directory where your main code
will reside, and a file inside src named
MyPackage.jl that serves as the entry point for your
package.
Developing Your Package
Let’s walk through an example. We have created a package called
MyPackage. Open this directory in VS code and open the
src/MyPackage.jl file. You’ll notice that it contains a
module definition:
module MyPackage
greet() = println("Hello from MyPackage!") # This is automatically added, yours may differ
end # moduleDelete the greet function and add the following code
instead:
f(x) = x^2This is now a function inside your package. Open a Julia REPL, ensure
you are in the MyPackage environment, and run:
julia> using MyPackage
julia> f(3)This should give an error. The function f is defined in
the package but it is not exported, meaning it is not
directly accessible from outside the module. In other words, you can use
f inside the package, but not directly outside the package.
Try running the following in the same REPL:
julia> MyPackage.f(3)Now you get the expected output of 9. To make
f accessible directly, you need to export it. Modify your
MyPackage.jl file to include:
export fI typically put this line right after the function definition. Now,
you can use f directly:
julia> f(3)If this still gives an error, you either didn’t save
MyProject.jl or you do not have Revise.jl
installed in your global environment.
A quick diversion to install Revise.jl. In the Julia
REPL:
julia> ]
pkg> activate
pkg> add Revise
pkg> activate .This should enter the package manager, add Revise to
your global environment, and then return to your package environment.
When you restart your REPL, Revise.jl should be
automatically loaded. Now, when you make changes to your package code
and save the file, Revise.jl will automatically track those
changes, and you won’t need to restart your REPL or re-import the
package.
It is perfectly fine to put all your code in the
MyPackage.jl. However, as your package grows, you might
want to organize your code into multiple files. You can create
additional files in the src directory and include them in
your main package file using the include function. For
example, if you create a file named utils.jl, you can
include it in MyPackage.jl like this:
include("utils.jl")Don’t forget to export any functions from these additional files that you want to be accessible outside the package. You should only export from the main package file, not from the included files.
Making your Package Available to Others
I am not going to cover registering your package with the Julia package registry in this post. However, that isn’t necessary. Upload your package to a public Git repository and others can add it directly using the URL. For example:
julia> ]
pkg> add https://github.com/username/MyPackage.jlThen a user can use your package just like any other package.
Further Reading
For more detailed information on creating and managing Julia packages, refer to the official Julia documentation on Creating Packages. This resource provides comprehensive guidance on best practices, versioning, testing, and publishing your packages.
You can also read about
- Testing
- Documentation
- Continuous Integration (will be in both testing and documentation links)
- Versioning