Change Package directory in Julia

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!

Leave a Comment