Using Julia Projects to Manage Dependencies
约 394 字大约 1 分钟
Julia
2025-11-19
1. How to Create a Julia Project
Julia uses the built-in Pkg package manager to organize projects and dependencies. It is recommended to use an isolated environment for each project so that you can avoid package conflicts and keep the environment reproducible. This article explains how to create and use a Julia project.
The code in this article was tested with Julia 1.12.1.
1. Initialize a Julia project (generate)
Enter the Julia REPL and run:
using Pkg
Pkg.generate("MyPackage")Or press ] to switch from the julia> prompt to pkg>, then run:
generate MyPackageThe two forms above are equivalent.
This generates:
Generating project MyPackage:
MyPackage\Project.toml
MyPackage\src\MyPackage.jl- The
MyPackagefolder MyPackage\Project.toml: records dependency names and compatibility rangesMyPackage\src\MyPackage.jl: example package source codeManifest.toml: records all dependencies and exact versions to make the environment reproducible
Now switch to the newly created project folder:
cd MyPackage2. Activate the project (activate)
Inside the project folder, press ] to enter pkg> mode and run:
activate .This activates the current folder as a Julia project. You should see the project name appear in the prompt. At that point the project is active.
3. Add dependencies
Add dependencies as usual. For example:
using Pkg
Pkg.add("Plots")
Pkg.add("DataFrames")The dependencies will be written into Project.toml, and Manifest.toml will also be updated. If this is the first dependency added to the project, Manifest.toml will be created automatically.
4. Remove dependencies
Removing a package in Julia is straightforward. You can use Pkg.rm() or use rm package_name in Pkg mode.
For example, to remove Plots:
Pkg.rm("Plots")Julia will automatically update both Project.toml and Manifest.toml.
5. Basic project structure
MyPackage/
│
├── src/
│ └── MyPackage.jl
│
├── Project.toml
└── Manifest.toml2. How Others Install Dependencies After Cloning Your Project
If someone clones your project with Git:
git clone https://github.com/yourname/MyPackage.git
cd MyPackageThen in the Julia REPL:
using Pkg
Pkg.activate(".")
Pkg.instantiate()Pkg.instantiate() installs every dependency and exact version according to Project.toml and Manifest.toml, ensuring the environment matches yours.
3. Summary of Common Commands
generate MyPackage: create a new projectactivate .: activate the current projectadd PackageName: add a dependencyrm PackageName: remove a dependencyinstantiate: install all dependencies listed in the project files
