As you start building machine learning models with Julia, managing the software components your project relies on becomes a significant aspect of your workflow. Julia comes equipped with a sophisticated built-in package manager, Pkg.jl, designed to handle dependencies, manage project-specific environments, and ensure that your ML projects are reproducible and shareable. Understanding Pkg.jl is fundamental to an efficient and organized development process.
Pkg.jl manages the packages your project needs, such as DataFrames.jl for data manipulation or MLJ.jl for machine learning tasks. It ensures that you have the correct versions of these packages and that their own dependencies are also met, preventing conflicts and the infamous "works on my machine" problem. This is particularly important in machine learning, where the exact versions of libraries can influence model behavior and results.
The most common way to interact with Pkg.jl is through its special REPL (Read-Eval-Print Loop) mode. You can enter this mode from the standard Julia REPL by simply typing the right square bracket, ].
julia> ]
pkg>
Your prompt will change from julia> to pkg>, indicating that you are now in the package manager environment. Here, you can issue commands to manage packages for your projects. To exit the Pkg REPL and return to the Julia REPL, press Backspace or Ctrl+C (on an empty line).
When you start managing packages for a project, Pkg.jl creates and uses two important files in your project's root directory:
Project.toml: This file lists the direct dependencies of your project. Think of it as your project's high-level declaration of needs. For each package you explicitly add, Project.toml records its name and a set of compatible versions. For example, if you add DataFrames.jl, an entry for it will appear here. This file is human-readable and intended to be edited (though often indirectly via Pkg commands) and committed to version control.
Manifest.toml: This file is a complete, detailed list of all packages required for your project to run, including both direct dependencies (those in Project.toml) and indirect dependencies (packages that your direct dependencies rely on). Critically, Manifest.toml records the exact version of every single package in the dependency graph. This file ensures that anyone using your project can recreate the precise environment, down to the specific version of each component. Manifest.toml is automatically generated and updated by Pkg.jl; you typically shouldn't edit it by hand. It should also be committed to version control.
Together, these two files provide a system for defining and recreating project environments.
This diagram shows how Pkg.jl commands, accessed via the Pkg REPL mode, interact with
Project.tomlandManifest.tomlfiles within your project directory, and how these files relate to external package registries and your own code.
By default, if you don't explicitly activate a project environment, Pkg.jl operates on a global environment specific to your Julia version. While this is fine for quick experiments, for any serious project, especially in machine learning, you should use project-specific environments. This isolates dependencies for each project, preventing version conflicts between different projects.
To create and activate an environment for a new project:
].pkg> activate .
Activating new project at `/path/to/your/project`
The . indicates the current directory. If Project.toml doesn't exist, Pkg.jl will create one. Now, any package operations will affect this project's environment only. The pkg> prompt might also change to reflect the active project, for example, (your-project) pkg>.
Here are some of the most frequently used commands in the Pkg REPL mode:
add PackageName: Adds the latest version of PackageName (and its dependencies) to the current project environment. It updates both Project.toml and Manifest.toml.
(your-project) pkg> add DataFrames
To add a specific version of a package, use add PackageName@version, for instance, add [email protected]. You can also add packages directly from Git repositories or local paths.
status (or st): Displays the packages currently installed in the active environment, along with their versions and a brief status.
(your-project) pkg> st
Status `Project.toml`
[a93c6f00] DataFrames v1.3.6
...
update (or up): Updates all packages in Project.toml to their latest compatible versions, respecting the compatibility constraints defined. If you want to update a specific package, use up PackageName.
(your-project) pkg> up
rm PackageName: Removes PackageName from the project's direct dependencies. Pkg.jl will also remove any indirect dependencies that are no longer needed.
(your-project) pkg> rm DataFrames
instantiate: This is a very useful command, especially when working collaboratively or setting up a project on a new machine. It reads the Manifest.toml file (or Project.toml if Manifest.toml is absent) and downloads and installs all listed packages to their specified versions. This ensures that you get the exact environment defined by the project.
(your-project) pkg> instantiate
If you clone a Julia project from a repository that includes Project.toml and Manifest.toml, the first step after navigating to the project directory and activating its environment is typically to run instantiate.
Reproducibility is a foundation of good scientific practice, and it's equally important in machine learning. Pkg.jl, through Project.toml and Manifest.toml, provides the tools to achieve this:
activate .).Project.toml and Manifest.toml to version control (e.g., Git) along with your source code. These files fully describe your project's dependencies.cd into the project directory, start Julia, enter Pkg mode, and run activate . followed by instantiate. This will recreate the exact software environment, ensuring that code runs as expected and results are consistent.While the Pkg REPL mode is convenient for interactive use, Pkg.jl can also be used programmatically within your Julia scripts. This is useful for automating environment setups or for tools that manage Julia projects.
To use Pkg.jl functions, you first need to import it:
import Pkg # or using Pkg for all exported names
# Example: Adding a package programmatically
Pkg.add("JSON")
# Example: Instantiating the environment for the current project
Pkg.instantiate()
This programmatic access allows for powerful scripting of project and package management tasks.
By mastering Pkg.jl, you gain fine-grained control over your project dependencies, leading to more stable, reproducible, and manageable machine learning projects in Julia. As you progress through this course and start using various ML-specific packages, you'll find Pkg.jl to be an indispensable tool in your Julia toolkit.
Was this section helpful?
Pkg.jl, detailing its functionalities, REPL mode, configuration files (Project.toml, Manifest.toml), and essential commands.Pkg.jl and project management practices for building robust applications.© 2026 ApX Machine LearningEngineered with