Installing and managing packages
Julia has a built-in package manager that you can use by loading the Pkg
module or through pkg mode of the Julia REPL. In this section, we will learn how to use it to install packages and manage project environments.
Installing Julia packages
Julia has an increasing number of registered packages that you can easily install using the built-in package manager. In this section, we will install the Plots
library as an example. Let's install Plots
using the add
command from Pkg mode. This way of installing packages comes in handy when working on the Julia REPL:
- Open Julia.
- Enter Pkg mode by pressing the
]
key just after the julia> prompt. - Type
add Plots
after the pkg> prompt and press Enter. - Wait for the installation to finish; it can take some time.
- Press the Backspace key to return to the julia> prompt.
Great! This has been easy, and you now have the Plots
package installed. However, Pkg mode is only available to you when you are in the Julia REPL. But, if you want to install a Julia package from a non-interactive environment (for example, inside a Julia script), you will need to use the add
function from the Pkg
module. The Pkg
module belongs to the Julia Standard Library. Thankfully, you don't need to install the packages of the Standard Library before using them. Let's try adding Plots
again but using the Pkg
module this time. As we have already installed the latest version of Plots
, this will be fast:
- Open Julia.
- Import the
Pkg
module by typingimport Pkg
after the julia> prompt and pressing Enter. - Type
Pkg.add("Plots")
and press Enter.
In the last example, we used import
to load the Pkg
module. In the next section, we will learn some different ways in which you can load packages.
Loading packages
We need to load a package to use it within a Julia session. There are two main ways to load packages in Julia. The first one is using the import
keyword followed by the module name – for example, import Pkg
. As Julia packages export modules of the same name, you can also use the name of a package. When we use import
in this way, Julia only brings the package's module into scope. Then, we need to use qualified names to access any function or variable from that module. A qualified name is simply the module name followed by a dot and the object name – for example, Pkg.add
.
The second way to load a package is to use the using
keyword followed by the module name. Julia will bring the module into scope, as well as all the names that the module exports. Therefore, we do not need to use qualified names to access their functions and variables. For instance, executing using Plots
will bring the exported plot
function into scope. You can still access unexported functions and variables using their qualified names.
Managing environments
A project environment defines a set of package dependencies, optionally with their versions. The Julia package manager has built-in support for them, and we can use them to create reproducible data analysis pipelines and visualizations. For example, this book's code examples use environments to allow the reproducibility of code through time and across different systems.
Julia defines project environments using two files. The first is the Project.toml
file that stores the set of dependencies. The second is the Manifest.toml
file that stores the exact version of all the packages and their dependencies. While the former is mandatory for any environment, the latter is optional. Luckily, we do not need to create those files manually, as we can manage the environment's packages through the package manager.
There are multiple options for dealing with project environments. One is to start julia
in a given environment by using the --project
argument. Usually, we want to create an environment in the folder where we are starting Julia. In those cases, we can use a dot to indicate the current working directory. Let's create an environment in the current folder containing a specific version of the Plots
package:
- Run
julia --project=.
in the terminal to open the Julia REPL using the project environment defined in the current working directory. - Press the
]
key to enter Pkg mode. You will see the name of the current folder on the prompt. That's Pkg mode telling you that you are in that environment. - Type
status
and press Enter to see the content of your current environment. - Type
add Plots@1.0.0
to install Plots version 1.0.0 in that environment. - Run the
status
command in Pkg mode again to check what is in the environment after the previous operation. - Press the Backspace key to return to the Julia prompt.
In the previous example, we have used the --project
argument to start julia
in a particular environment. If you run julia
without indicating a project folder, you will use the default environment corresponding to your Julia version.
You can change between environments using the activate
command. It takes the path to the project folder that contains the environment, and if you do not give any path, Julia will start the default environment of your Julia version. For example, executing activate .
in Pkg mode will start the environment in the current working directory, and running activate
will return the default Julia environment.
When you first activate a non-empty environment on your system, you must install all the required packages. To get all the needed packages, you should run the instantiate
command of Pkg mode. For example, instantiation will be necessary if you want to use an environment created on another computer.
While Pkg mode of the Julia REPL is handy when you are working interactively, sometimes you need to manage environments inside a Julia script. In those cases, the Pkg
module will be your best friend. So, let's create a Julia script that uses a particular version of Plots. First, create a file named installing_plots.jl
with the following content, using any text editor:
import Pkg
Pkg.activate(temp=true)
Pkg.add(Pkg.PackageSpec(name="Plots", version="1.0.0"))
Pkg.status()
In that code, we are using the activate
function of the Pkg
module with temp=true
to create and activate the script environment in a temporary folder. We need to use the PackageSpec
type defined on the Pkg
module to add a specific package version.
Now, you can run the script executing julia installing_plots.jl
on your terminal. You will see that the script creates and activates a new environment in a temporal folder. Then, it installs and precompiles Plots
and its dependencies. Finally, it shows that Plots
version 1.0.0 was installed in the environment. The script will run a lot faster the second time because the packages are installed and precompiled on your system.
There are other Pkg
commands and functions that you will find helpful when managing environments – status
, to list the packages on the current project environment, update
, and remove
. You can see the complete list of Pkg
commands in Pkg mode by typing ?
and pressing the Enter key just after the pkg> prompt. Optionally, you can see extended help for each command by entering ?
and the command name in Pkg mode. If you are using the Pkg
module, you can access the documentation of the functions by typing Pkg.
and the function name in help mode of the Julia REPL.
Now that we know how to install and manage Julia packages, let's start installing some packages to set up the different development environments we will use for Julia.