26

I want to change the Package directory in Julia. The default is

"~/.julia/v0.4"

I want to move it to /opt/julia/v0.4/. Ideally I want to move the packages that are already installed in ~/.julia/v0.4 to the new location. But if that is not possible I can reinstall them.

What do I have to do?

5
  • 1
    you can run export JULIA_PKGDIR=/opt/julia/v0.4 in shell, then use Pkg.init() to initialize a new package system in the new directory, and just copy and paste these packages to the new location.
    – Gnimuc
    Apr 4, 2016 at 9:56
  • I looked up JULIA_PKGDIR in the the doc. I should just set JULIA_PKGDIR=/opt/julia, since the version will be appended automatically. You should post an answer.
    – a06e
    Apr 4, 2016 at 10:00
  • @GnimucK. I did this but it broke the Jupyter notebook. How can I alert Jupyter of the new location?
    – a06e
    Apr 4, 2016 at 10:08
  • what's the result of jupyter kernelspec list?
    – Gnimuc
    Apr 4, 2016 at 10:10
  • @GnimucK. Nevermind. I fixed it by running Pkg.build("IJulia") in Julia.
    – a06e
    Apr 4, 2016 at 10:11

2 Answers 2

48

Julia-v0.6 and before

one can change julia's package directory by following these steps:

  1. run export JULIA_PKGDIR=/your/directory in shell(or manually add a new environment variable JULIA_PKGDIR on windows)
  2. run Pkg.init() in julia to initialize a new package system
  3. copy REQUIRE from old directory to the new one
  4. run Pkg.resolve() in julia

Julia-v0.7+

The "Package directory" in the new package manager is called DEPOT_PATH, it can be changed by adding an environment variable JULIA_DEPOT_PATH:

JULIA_DEPOT_PATH=./test julia

julia> DEPOT_PATH
1-element Array{String,1}:
 "./test"

(v0.7) pkg> add JSON2
   Cloning default registries into /Users/gnimuc/test/registries

With the new package manager, we are able to create isolated projects in any directory we want instead of having a single giant package directory. Every project contains a Project.toml and a Manifest.toml if it has some dependencies. These two files record and keep tracking the environment of the project.

UPDATE

The following info might be obsoleted. I highly recommend to use PkgTemplates.jl for generating projects in Julia-v1.0+.

Generate a new project

We can generate a new project in any folder, but we must cd to the project folder to using the project. The (v0.7) below shows we're still in the default environment, so we cannot use the newly generated project:

(v0.7) pkg> generate ./MyNewProject
Generating project MyNewProject:
    ./MyNewProject/Project.toml
    ./MyNewProject/src/MyNewProject.jl

julia> using MyNewProject
ERROR: ArgumentError: Module MyNewProject not found in current path.
Run `Pkg.add("MyNewProject")` to install the MyNewProject package.
Stacktrace:
 [1] require(::Module, ::Symbol) at ./loading.jl:868

If we cd to the project folder and activate the environment, then we can using our new project without any problems:

shell> cd MyNewProject/
/Users/gnimuc/MyNewProject

(v0.7) pkg> activate .

(MyNewProject) pkg> 

julia> using MyNewProject

I think that's the big difference between the new package manager and the old one. In short, we need to explicitly activate our unregistered project/package.

Download and init someone else's project

According to the doc, we can add an unregistered package/project via add command:

(HelloWorld) pkg> add https://github.com/fredrikekre/ImportMacros.jl

This command adds the target package as a dependency of our current project. In this example, we added ImportMacros in HelloWorld's Manifest.toml. What if we just use it as a top-level project? To add it to the default environment (v0.7)? no, we don't need to. The answer is we can directly download the code, cd to the folder and run instantiate in the pkg mode:

shell> git clone https://github.com/Gnimuc/GLTF.jl GLTF
Cloning into 'GLTF'...
remote: Counting objects: 286, done.
remote: Compressing objects: 100% (56/56), done.
remote: Total 286 (delta 73), reused 103 (delta 59), pack-reused 167
Receiving objects: 100% (286/286), 62.16 KiB | 46.00 KiB/s, done.
Resolving deltas: 100% (135/135), done.

shell> cd GLTF

pkg> activate .

(GLTF) pkg> instantiate
  Updating registry at `~/.julia/registries/General`
  Updating git-repo `https://github.com/JuliaRegistries/General.git`

The new package manager is great! We neither need "include before using" nor make everything as a package just for using it. We have full-featured "Project" now!

3
  • 2
    If you have IJulia, run Pkg.build("IJulia") after.
    – a06e
    Apr 4, 2016 at 10:34
  • Your update kinda gave me hope for what I want to do. I'm on windows and whenever I do a Pkg.add or add in pkg mode, all these packages go to ~/.julia which I do not want. I thought I can isolate the packages to my project using your method, but apperently not.
    – Vivere
    Oct 6, 2021 at 17:54
  • 1
    @Vivere Could you elaborate a bit more about your use case? All dependencies are cached in the JULIA_DEPOT_PATH and these packages are isolated from each other. When developing your own package, you could activate to an isolated environment without any version collision problems. To me, it looks like your goal is to publish the package and pack all its dependencies. PackageCompiler.jl is the right tool for this.
    – Gnimuc
    Oct 7, 2021 at 4:57
5

Julia only way:

julia> ENV["JULIA_PKGDIR"] = "E:\\Julia-0.6.0\\portable"
"E:\\Julia-0.6.0\\portable"

julia> ENV["JULIA_PKGDIR"]
"E:\\Julia-0.6.0\\portable"

julia> Pkg.init()
INFO: Initializing package repository E:\Julia-0.6.0\portable\v0.6
INFO: Cloning METADATA from https://github.com/JuliaLang/METADATA.jl

However, the cache dir is still pointing to the old folder, so I checked why that is and figured it out:

julia> Base.LOAD_CACHE_PATH
1-element Array{String,1}:
 "C:\\Users\\kung\\.julia\\lib\\v0.6"

julia> Pkg.__init__()
2-element Array{String,1}:
 "E:\\Julia-0.6.0\\portable\\lib\\v0.6"
 "C:\\Users\\kung\\.julia\\lib\\v0.6"

julia> pop!(Base.LOAD_CACHE_PATH)
"C:\\Users\\kung\\.julia\\lib\\v0.6"

julia> Base.LOAD_CACHE_PATH
1-element Array{String,1}:
 "E:\\Julia-0.6.0\\portable\\lib\\v0.6"

As simple function:

function set_julia_dir(dir::String)
    ENV["JULIA_PKGDIR"] = dir
    Pkg.init()
    Pkg.__init__()
    pop!(Base.LOAD_CACHE_PATH)
end

set_julia_dir("E:\\Julia-0.6.0\\portable")
3
  • Will this setting be preserved if I restart Julia?
    – a06e
    Jul 5, 2017 at 9:44
  • The environment variable is only temporary, hence would need to call set_julia_dir every time
    – kungfooman
    Jul 5, 2017 at 10:59
  • 2
    If you don't want to set JULIA_PKGDIR in your shell startup script, the only way is to edit Base, by default it's hardcoded to joinpath(homedir(),".julia") in base/pkg/dir.jl and then rebuild the system image of Julia
    – kungfooman
    Jul 5, 2017 at 16:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.