# What does \`pkg\> activate\` do?

**URL:** <https://discourse.julialang.org/t/what-does-pkg-activate-do/122122>\
**Category:** New to Julia\
**Created:** [November 1, 2024, 11:18am UTC](https://discourse.julialang.org/t/what-does-pkg-activate-do/122122 "2024-11-01T11:18:02Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![world-peace](https://avatars.discourse-cdn.com/v4/letter/w/9f8e36/32.png) [@world-peace](https://discourse.julialang.org/u/world-peace)\
**Post date:** [November 1, 2024, 11:18am UTC](https://discourse.julialang.org/t/what-does-pkg-activate-do/122122/1 "2024-11-01T11:18:02Z")

</div>

I think this is a relatively simple question, and I think I already know the answer but thought it might be worth asking anyway to get a more in depth understanding (which I don’t currently have) or have someone correct me if I’m completely wrong.

What does

```julia
pkg> activate .

```

actually do?

- I know that it activates the current environment, if an environment exists in the _current working directory_.
- My understanding is that this command searches for a `Project.toml` file in the current directory. I think this is the important point?
- It doesn’t seem to matter if a `Project.toml` file does not exist in the current working directory, because the pkg REPL just reports that it has activated an environment with the current working directory name. (But no files are changed on disk.)
- If `Project.toml` does not exist in the _current_ working directory, then a _new_ environment is created in the _current working directory_. This doesn’t actually do anything unless a `pkg> add PACKAGE` is run, which will create the `Project.toml` file, because it does not exist.

Consider the following, possibly surprising behavior.

1. Create a new Package with `pkg> generate MYPACKAGE`
2. This creates a subdirectory called `MYPACKAGE/src`
3. `cd` to `src`
4. Run the Julia REPL, `]` to change to the `pkg` REPL, then run `activate .`
5. Rather than activating the environment in the parent directory, this _creates a new environment in the_ `src` _directory._ Is that a bit odd?

---

<div class="post-metadata">

**Author:** ![world-peace](https://avatars.discourse-cdn.com/v4/letter/w/9f8e36/32.png) [@world-peace](https://discourse.julialang.org/u/world-peace)\
**Post date:** [November 1, 2024, 11:19am UTC](https://discourse.julialang.org/t/what-does-pkg-activate-do/122122/2 "2024-11-01T11:19:59Z")

</div>

Also to note: Activating a package does not seem to change the environment variable `LOAD_PATH`, which can be printed by accessing shell mode in the REPL and running

```julia
echo $LOAD_PATH

```

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [November 1, 2024, 11:38am UTC](https://discourse.julialang.org/t/what-does-pkg-activate-do/122122/3 "2024-11-01T11:38:38Z")

</div>

It sets the current working directory (which is what `.` means) as the active environment, full stop. It doesn’t matter if there’s an environment already there or not, or if there is one in the directory above it, etc. It’s a simple, direct command, it isn’t doing anything fancy.

Then, in a later command, if you add a package to an environment which is empty, it will create a Project.toml and Manifest.toml; if you add a package to an environment with an existing Project.toml, it will add the package to it, and so forth. But that’s unrelated to `activate`.

Note you don’t need to activate the current working directory; you can activate some other directory. E.g. `pkg> activate MySubDirectory` or `pkg> activate ..` to activate the directory above the current one, or `pkg> activate /envs/myabspath/MyProject` etc.

---

<div class="post-metadata">

**Author:** ![xujc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xujc/32/214263_2.png) [@xujc](https://discourse.julialang.org/u/xujc)\
**Post date:** [December 28, 2024, 12:29am UTC](https://discourse.julialang.org/t/what-does-pkg-activate-do/122122/4 "2024-12-28T00:29:29Z")

</div>

Perhaps a silly question: is it allowed to put multiple environments (Project-1.toml, Project-2.toml, …) in the same folder? 🙃

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [December 28, 2024, 12:40am UTC](https://discourse.julialang.org/t/what-does-pkg-activate-do/122122/5 "2024-12-28T00:40:24Z")

</div>

No, I don’t think so

---

<div class="post-metadata">

**Author:** ![xujc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xujc/32/214263_2.png) [@xujc](https://discourse.julialang.org/u/xujc)\
**Post date:** [December 31, 2024, 12:57am UTC](https://discourse.julialang.org/t/what-does-pkg-activate-do/122122/6 "2024-12-31T00:57:13Z")

</div>

thanks 🙂

---

<div class="post-metadata">

**Author:** ![xujc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xujc/32/214263_2.png) [@xujc](https://discourse.julialang.org/u/xujc)\
**Post date:** [January 5, 2025, 3:08am UTC](https://discourse.julialang.org/t/what-does-pkg-activate-do/122122/7 "2025-01-05T03:08:12Z")

</div>

Just in case someone is interested:  
You can share the same environment within different JupyterLab notebooks.

For example, if you want try out various stuffs without the pain of creating a folder for each one of them (and not messing up the default environment), you can create various notebooks in the same folder. Just run `using Pkg; Pkg.activate("./")` at the beginning.

---

<div class="post-metadata">

**Author:** ![MatthijsCox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/matthijscox/32/42831_2.png) [@MatthijsCox](https://discourse.julialang.org/u/MatthijsCox)\
**Post date:** [January 14, 2025, 4:00pm UTC](https://discourse.julialang.org/t/what-does-pkg-activate-do/122122/8 "2025-01-14T16:00:05Z")

</div>

Note that I always enjoy reading the Pkg.jl source code (I have never done this in any other language I used).

In this case you can find the `activate` function here: [Pkg.jl/src/API.jl at master · JuliaLang/Pkg.jl · GitHub](https://github.com/JuliaLang/Pkg.jl/blob/master/src/API.jl#L1305). Which I found by using `using Pkg; @edit Pkg.activate()`.

In the code it seems it mainly sets the `Base.ACTIVE_PROJECT[]` global parameter (and caching somekind of environment snapshot?). So as mentioned by @ericphanson it doesn’t do a lot actually 🙂
