3.2 Package Management

Most people are unlikely to choose a programming language based on its package manager (or lack thereof). Despite this reality, the Julia package manager is one of those features that really makes me appreciate the language. Julia’s package manager is extremely simple to work with and also enables much more reproducible code. Let us explore and see how this is the case:

There are two different fundamental ways of working with packages in Julia: via the REPL’s Pkg mode and via the Pkg package. I will focus on using the REPL since it is extremely intuitive for new users. You can start by launching Julia in the terminal. Then, type ] in which should take you into Pkg mode:

   _       _ _(_)_     |  Documentation: https://docs.julialang.org
  (_)     | (_) (_)    |
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 1.6.3 (2021-09-23)
 _/ |\__'_|_|_|\__'_|  |  Official https://julialang.org/ release
|__/                   |

(@v1.6) pkg> 

From here, one of the natural things to do is check what commands you can run in the package manager. To do this, type an ? in and press enter/return.

You will see all the possible commands:

(@v1.6) pkg> ?
  Welcome to the Pkg REPL-mode. To return to the julia> prompt, either press
  backspace when the input line is empty or press Ctrl+C.


  pkg> cmd [opts] [args]

  Multiple commands can be given on the same line by interleaving a ; between
  the commands. Some commands have an alias, indicated below.


  activate: set the primary environment the package manager manipulates

  add: add packages to project

  build: run the build script for packages

  develop, dev: clone the full package repo locally for development

Some of the most common commands you will use are add, activate, status (or the shorthand st), and remove (or the shorthand rm). In this book, we will be using Flux.jl, so if you want to play around with the package, you can simply install it by typing add Flux.

After the install finishes, you can check your package environment by typing status:

(@v1.6) pkg> status
      Status `~/.julia/environments/v1.6/Project.toml`
  [587475ba] Flux v0.12.7

The package manager automatically shows the file which is managing the packages and their versions. In this case, it is ~/.julia/environments/v1.6/Project.toml. As a general best practice, it is recommend to always use local environments instead of making changes to your main Julia environment. You can do this by activating a new environment.

julia> pwd()

shell> cd Desktop # type `;` to enter the shell mode from the REPL

(@v1.6) pkg> activate .
  Activating new environment at `~/Desktop/Project.toml`

In the code above, you can see I started out in my main user folder. I then entered the shell mode by typing ; and used the change directory command to switch to my Desktop folder. From there, I did activate . which activates the current folder I am in. I can confirm this by typing status:

(Desktop) pkg> status
      Status `~/Desktop/Project.toml` (empty project)

and you can see the newly created project is empty.

Hopefully these examples give you a sense of how easy to user and powerful the Julia package manager is. You can read more about working with packages from the Pkg.jl docs: https://pkgdocs.julialang.org/v1/getting-started/.

CC BY-NC-SA 4.0 Logan Kilpatrick