Skip to main content

Section A.2 Managing Packages in Julia

As with many software packages, additional functionality is available in other libraries or packages. If you take a look at https://juliapackages.com you can search through what is available. For example, relevant to this course is JuMP, which is useful for solving linear optimization problems (and others). Clicking on any of the package names will give additional information about the package including documentation (hopefully).
If you don’t have a package installed and try to use it, like:
using Primes
you will get an error that it isn’t installedβ€”unless you actually installed it previously. If you are in the REPL, then it may ask you to install the package. Hit y. (Note: this is a 1.12 version feature and does not currently work as a jupyter notebook that you have in vscode).
If you are in a jupyter notebook, entering the following two lines in a cell and evaluating (running) the cell, will also work.
using Pkg
Pkg.add("Primes")
and after fetching the package, rerunning
using Primes
should no longer give an error. Then, for example, try
isprime(17)
which returns true because 17 is a prime number.
 1 
The function isprime is actually nondeterministic function that determines if a number is true and you can find out more information at the Primes package documentation. This demonstrates 4 commands including isprime and factor which returns all prime factors (and the order).
A few other packages that will be helpful in this course are:
IJulia
This provides the browser interface to Julia. You should already have this installed.
JuMP
This package is an interface for finding extrema of functions with constraints.
HiGHS
This provides functionality for solving linear problems with JuMP.
CairoMakie and Makie
These are plotting packages for Julia.

Subsection A.2.1 Managing Packages in Julia

Although we can use the Pkg package to handle packages, this section will use the terminal REPL to handle any non-standard packages. Additional documentation on this is given in https://docs.julialang.org/en/latest/stdlib/Pkg/ First, open up a terminal version of julia (either in the Terminal, PowerShell or vscode).
 2 
For this, it is assumed that you have version 1.12 installed and are using it for the default version. The rest of this should work identically with other versions.
See SectionΒ A.1 for help in this. You will get:
               _
   _       _ _(_)_     |  Documentation: https://docs.julialang.org
  (_)     | (_) (_)    |
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 1.12.0 (2025-10-07)
 _/ |\__'_|_|_|\__'_|  |  Official https://julialang.org/ release
|__/                   |
or similar and then
julia>
which means we’re ready to handle julia commands. If we type ], then the prompt turns into:
(v1.12) pkg>
where the 1.12 will be the version of julia that you are running. There are a number of commands that we will cover here:
and the commands in parentheses are the shortcut.

Subsection A.2.2 Adding a package

In the package command line, type add package name to add the package. For example, to add the JuMP package:
add JuMP
and it is not installed, you will get something like:
Updating registry at `~/.julia/registries/General.toml`
   Resolving package versions...
    Updating `~/.julia/environments/v1.11/Project.toml`
  [4076af6c] + JuMP v1.29.1
    Updating `~/.julia/environments/v1.11/Manifest.toml`
  [6e4b80f9] + BenchmarkTools v1.6.2
  [523fee87] + CodecBzip2 v0.8.5
  [944b1d66] + CodecZlib v0.7.8
  [bbf7d656] + CommonSubexpressions v0.3.1
  [34da2185] + Compat v4.18.1
  [864edb3b] + DataStructures v0.19.1
  [163ba53b] + DiffResults v1.1.0
  [b552c78f] + DiffRules v1.15.1
  [ffbed154] + DocStringExtensions v0.9.5
  [f6369f11] + ForwardDiff v1.2.2
  [92d709cd] + IrrationalConstants v0.2.6
  [0f8b85d8] + JSON3 v1.14.3
  [4076af6c] + JuMP v1.29.1
  [2ab3a3ac] + LogExpFunctions v0.3.29
  [1914dd2f] + MacroTools v0.5.16
  [b8f27783] + MathOptInterface v1.45.0
  [d8a4904e] + MutableArithmetics v1.6.6
  [77ba4419] + NaNMath v1.1.3
  [bac558e1] + OrderedCollections v1.8.1
  [276daf66] + SpecialFunctions v2.6.1
  [1e83bf80] + StaticArraysCore v1.4.3
  [10745b16] + Statistics v1.11.1
  [856f2bd8] + StructTypes v1.11.0
  [3bb67fe8] + TranscodingStreams v0.11.3
  [6e34b625] + Bzip2_jll v1.0.9+0
  [efe28fd5] + OpenSpecFun_jll v0.5.6+0
  [37e2e46d] + LinearAlgebra v1.11.0
  [9abbd945] + Profile v1.11.0
  [9e88b42a] + Serialization v1.11.0
  [2f01184e] + SparseArrays v1.11.0
  [8dfed614] + Test v1.11.0
  [e66e0078] + CompilerSupportLibraries_jll v1.1.1+0
  [4536629a] + OpenBLAS_jll v0.3.27+1
  [05823500] + OpenLibm_jll v0.8.5+0
  [bea87d4a] + SuiteSparse_jll v7.7.0+0
  [8e850b90] + libblastrampoline_jll v5.11.0+0
Precompiling project...
  28 dependencies successfully precompiled in 46 seconds. 46 already precompiled.
A few things to note:
  • This is a long list of additional packages that JuMP needs to run and the version that JuMP uses for this version. This is long because JuMP is a pretty complicated package. Note that when installing the Primes package that we did above, there was only one other package needed to be installed with Primes.
  • You results will vary depending on version numbers available and what subpackages (like LinearAlgebra or ForwardDiff) are needed for the current version of the package you want to load.
  • The line after the 2nd Updating line is the package (and version) that you are installing.
  • All of the lines after the 3rd Updating line is all of the packages that this depends on.
  • The + sign means that the package is being added.
If you want to add multiple packages at the same time, say packages A, B and C, type add A B C. You can also add particular versions of a package (often for testing or to avoid a bug). For example, if you want version 0.3.0 of ForwardDiff type:
 add ForwardDiff@0.3.0 
You will then get info on the dependencies on that version.

Subsection A.2.3 Package Status

The status command (or st) will just list all of the main packages installed. For example,
(@v1.12) pkg> st
Status `~/.julia/environments/v1.12/Project.toml`
  [87dc4568] HiGHS v1.19.3
  [7073ff75] IJulia v1.31.0
  [4076af6c] JuMP v1.29.1
  [b8f27783] MathOptInterface v1.45.0
  [27ebfcd6] Primes v0.5.7
  [37e2e46d] LinearAlgebra v1.12.0
and note that these are just the packages added by the add command, not all of the dependencies. If you want all of the dependencies as well, type st --manifest and I get a huge list of packages.
If you run st then you may also get information that some packages have updates. If this is true, you can run the update command as shown next.

Subsection A.2.4 Removing a Package

You can remove a package by typing remove or rm then the package name. If I want to remove the ForwardDiff package, then
remove ForwardDiff
we get the following:
Updating `~/.julia/environments/v1.0/Project.toml` 
[f6369f11] - ForwardDiff v0.3.0 
Updating `~/.julia/environments/v1.0/Manifest.toml` 
[49dc2e85] - Calculus v0.4.1 
[c5cfe0b6] - DiffBase v0.2.0 
[f6369f11] - ForwardDiff v0.3.0 
[77ba4419] - NaNMath v0.3.2 
Note:
  • The rm command removes the package from the list of available packages, but doesn’t remove them from your harddrive.
  • If you want to see everything installed, navigate to the /.julia/packages directory, which is where they are stored. I have gone into this directory to delete package when troubleshooting.

Subsection A.2.5 Updating packages

If you type update or up you will update all of the installed packages (and dependencies). For example:
Updating `~/.julia/environments/v1.0/Project.toml` 
[7073ff75] ↑ IJulia v1.11.1 β‡’ v1.12.0 
Updating `~/.julia/environments/v1.0/Manifest.toml` 
[7073ff75] ↑ IJulia v1.11.1 β‡’ v1.12.0 
[b85f4697] ↑ SoftGlobalScope v1.0.5 β‡’ v1.0.7 
[5e66a065] ↑ TableShowUtils v0.1.1 β‡’ v0.2.0 
and all updates will be with an ↑. If you only want to update a single package, type the name after update.

Subsection A.2.6 Building Packages

Generally a package is built after it is installed. Building a package might include running code (or unpacking files) after it is installed. Sometimes if things get wonky, rebuilding is a good thing to do.
build
or if you only want say IJulia built,
 build IJulia 

Subsection A.2.7 Precompiling packages

When a package is used, often it requests to be compiled. For example, when using Primes, then following is shown:
[ Info: Precompiling Primes [27ebfcd6-29c5-5fa9-bf4b-fb8fc14df3ae]
and basically some code is compiled beforehand, generally to speed up code. You can precompile all code with
precompile
and it may take a while, but you won’t have to wait, when you load the package with the using command. This is generally not needed anymore and is done automatically.

Subsection A.2.8 Testing a Packages

To test a package, say the ForwardDiff package, then
test ForwardDiff
It list all of the dependencies first, and then runs a number of tests (and we will show how to write tests soon) and timing information. After a while, it finishes successfully.