How to setup Project Environments in Julia | by René | Towards Data Science

When you start to code multiple projects in Julia, it is recommended to use project specific environments for reproducibility and minimizing package dependencies. Julia has a great built-in package manager to make things easy. In this story I share my workflow step-by-step.
Pkg is Julia’s built-in package manager and handles operations such as adding, updating and removing packages. Pkg has it’s own read — evaluate — print — loop (REPL). In my workflow, when I want to create a new project environment, I usually start Julia from the directory where I keep my coding projects. To enter the Pkg REPL, simply type the right square bracket ] from the Julia REPL.
As you can see, the REPL changes color from green (Julia) to blue (Pkg). The (@v1.5) prefix indicates that I am in the default environment, using Julia version 1.5.x
To generate a new project environment, simply type generate followed by the name of your project. An environment directory will be created in your present working directory.
In this environment the files Project.toml and Manifest.toml live that specify what packages and what versions of those packages are available in your environment. However, the default environment (@v1.5) is still active.
To activate your newly created project environment, type activate followed by the name of your project (directory). The prefix changes from (@v1.5)to(demo)indicating that the project demo is active. By typing status you can see that the project is still empty (i.e. no packages added yet).
Adding packages is pretty straightforward and is done using the command add. By default the latest package version is added. Let’s add our first package. We will add the package CSV to our project environment.
As you can see, the files Project.toml and Manifest.toml are updated to reflect the change made.
Now we will add a specific version of the DataFrames package. In this case version 0.20.0 by typing the command add DataFrames@0.20.0
Because we chose to add an older version of the DataFrames package, the CSV package is downgraded from version 0.8.2 to version 0.5.23 as we also can see by using status.
To update all packages in our project environment, use the update command.
In the same way you can remove packages by using the remove command.
When you want to use a Pluto or Jupyter notebook for your coding project, you simply add the corresponding packages to your project environment using add Pluto or add IJulia.
You can leave the Pkg REPL by using the <backspace> key or the <ctrl> + c key combination and start using the added or updated packages.
Once you have setup your project environment, you can start coding. I usually start my workflow from the Bash terminal. I go to the project directory and start Julia with julia --project=. to activate the project environment as specified in the files Project.toml and Manifest.toml in the project directory.
After that, I check the status of the environment from the Pkg REPL or the Julia REPL as is shown below.
The same can be done from the REPL modes within Julia. To start the Shell REPL, type ; in the Julia REPL. The Shell REPL is in red.
For a Pluto notebook one can use:
julia --project=. -e "using Pluto; Pluto.run()"
For a Jupyter notebook one can use:
julia --project=. -e "using IJulia; notebook(dir=pwd())"
The parameter -e stands for evaluate. It evaluates the instructions separated by the semicolon. These instructions can also be performed from the Julia REPL. In that case just use julia --project=. from the Bash or Windows terminal.
This basically describes my workflow in Julia for managing project environments and package version. For more information, see the Pkg documentation. Let me me know your thoughts and best practices.