Julia Environment

In this blog post, I explain how to manage your Julia programming environments.

Julia Environment and Julia Project

What is a Julia environment?

A julia environment, also referred to as a “julia env”, is a self-contained workspace where you can develop, run and manage Julia code. It is created using a julia virtual environment, which allows you to install and manage packages specific to that environment without interfering with packages in other julia environments or your system.

A julia environment is like a container that has its own set of dependencies and configurations, including the version of Julia itself. This means that you can have multiple julia environments with different configurations and dependencies for different projects.

By creating a julia environment, you can avoid conflicts between packages and their dependencies, which can be a common issue when developing with julia. You can also ensure that your code will work consistently across different machines or platforms.

What is a Julia project?

A julia project is a specific type of julia environment that has a specific directory structure and a Project.toml file that lists the dependencies required for the project. When you activate a julia project, you can easily manage the dependencies and versions required for that project.

In summary, a julia environment (or “julia env”) is a self-contained workspace created using a julia virtual environment that allows you to manage and run Julia code with specific dependencies and configurations. Julia environments are like containers, and a julia project is a type of julia environment with a specific directory structure and dependency management system.

Creating a Julia Environment

To create a Julia environment, you can use the built-in package manager called Pkg. Here are the steps to create a new julia environment:

  1. Open a Julia REPL (Read-Eval-Print Loop) by typing julia in your terminal.
  2. Type ] to enter the package manager mode or Pkg mode.
  3. Use the activate command followed by the name of the directory where you want to create your new environment. For example, to create an environment named myenv, you can type:
activate "myenv"
  1. Once you activate the new environment, you can add packages to it using the add command while your are in Pkg mode (That is, after typing julia in your terminal you typed ] to enter the package manager mode. For example, to add the JSON package, you can type:
add "JSON" or add JSON
  1. After adding the packages you need, you can exit the package manager mode by typing Ctrl+D or by typing ] and then backspace.

Activate and Deactivate Julia Environment

Your new julia environment is now created and ready to use. You can activate it anytime by calling the activate command with the name of the directory where you created it. For example, to activate the myenv environment again, you can type:

activate "myenv"

To get out of the myenv environment while you are in Pkg mode, you can type:

activate

Note that we simply typed activate with no arguments.

Garbage Collecting Packages

There is another useful command

gc [-v|--verbose] [--all]

You can simply type help gc in Pkg mode to get more explanation about the command.

gc frees disk space by garbage collecting packages not used for a significant time. The --all option will garbage collect all packages which can not be immediately reached from any existing project. Use verbose mode for detailed output.

To list all the installed packages in an environment we type status in the Pkg mode. I suggest using help status to check additional options for usage.

Removing Packages

One use of gc is when you want to remove a julia package or all julia packages. To remove all existing packages in an environment type (here we use the environment we installed JSON package in) rm --all but note that this command removes the different packages information from the project.toml file without deleting the actual package files. You can instantly add JSON package to the myenv environment by typing add JSON in package managing mode. To remove the actual package files after removing the packages from the environment type in Pkg mode:

gc --all

Here I only had installed JSON package in my environment. You get a message like the following depending on the number of packages installed in your environment:

pkg> gc --all
      Active manifest files: 2 found
      Active artifact files: 0 found
      Active scratchspaces: 0 found
     Deleted 4 package installations (392.724 KiB)

Using someone else’s Julia project

Simply clone their project using e.g. git clone, cd to the project directory and call

(v1.0) pkg> activate .

(SomeProject) pkg> instantiate

If the project contains a manifest, this will install the packages in the same state that is given by that manifest. Otherwise, it will resolve the latest versions of the dependencies compatible with the project.

Note that activate by itself does not install missing dependencies. If you only have a Project.toml, a Manifest.toml must be generated by “resolving” the environment, then any missing packages must be installed and precompiled. instantiate does all this for you.

If you already have a resolved Manifest.toml, then you will still need to ensure that the packages are installed and with the correct versions. Again instantiate does this for you.

In short, instantiate is your friend to make sure an environment is ready to use. If there’s nothing to do, instantiate does nothing.

Specifying project on startup

Instead of using activate from within Julia you can specify the project on startup using the --project=<path> flag. For example, to run a script from the command line using the environment in the current directory you can run

julia --project=. myscript.jl

List Environments

Currently there is no built-in command for listing all the existing environments in Julia programming language. There is an open issue for it.

Best Practices

Since we cannot list Julia virtual environments. To prevent environments being scattered on the hard drive we can define all our environments in a specific folder. Something like

 Directory of C:\JuliaProjects\MyJuliaEnvironmentsFolder

04/23/2023  02:55 PM    <DIR>          .
04/23/2023  02:55 PM    <DIR>          ..
04/23/2023  02:54 PM    <DIR>          env1
04/23/2023  02:55 PM    <DIR>          env2
04/23/2023  02:55 PM    <DIR>          env3
               0 File(s)              0 bytes
               5 Dir(s)  31,909,466,112 bytes free

As you can see we created a folder named MyJuliaEnvironmentsFolder and in that folder we opened julia in terminal and called commands like below in Pkg mode

activate env1
add JSON

activate env2
add Plot

activate env3
add JSON

You get the idea, we simply create different environments in a specific folder so we do not get lost when it comes to cleaning up certain packages. We can easily find them all in a specific path we have decided to put them into.

4.4 5 votes
Article Rating
Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
David

Great article. Thanks for the useful tips!