# How to add package from a GitHub repo's subfolder

**URL:** https://discourse.julialang.org/t/how-to-add-package-from-a-github-repos-subfolder/118223
**Category:** General Usage
**Tags:** question, package
**Created:** [August 15, 2024, 1:19pm UTC](https://discourse.julialang.org/t/how-to-add-package-from-a-github-repos-subfolder/118223 "2024-08-15T13:19:24Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Shuhua](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shuhua/32/27618_2.png) [@Shuhua](https://discourse.julialang.org/u/Shuhua)
#### Post date: [August 15, 2024, 1:19pm UTC](https://discourse.julialang.org/t/how-to-add-package-from-a-github-repos-subfolder/118223/1 "2024-08-15T13:19:24Z")

</div>

A same question has been answered at [Installing from a subdirectory of Github](https://discourse.julialang.org/t/installing-from-a-subdirectory-of-github/25548).  
However, I still cannot make it work. In my repo [GitHub - ShuhuaGao/GADNF: Reconstruct Boolean network from data using GA on its DNF](https://github.com/ShuhuaGao/GADNF), there is a subfolder “src/GADNF.jl” which forms a package. I try to add it with the following:

```julia
Pkg.add(url="https://github.com/ShuhuaGao/GADNF", subdir="src/GADNF.jl")

```

The error is

```julia
Updating git-repo `https://github.com/ShuhuaGao/GADNF`
ERROR: expected the file `src/GADNF.jl` to exist for package `GADNF` at `C:\Users\shuhu\AppData\Local\Temp\jl_sscy2b`
Stacktrace:
  [1] pkgerror(msg::String)
    @ Pkg.Types C:\Users\shuhu\.julia\juliaup\julia-1.10.4+0.x64.w64.mingw32\share\julia\stdlib\v1.10\Pkg\src\Types.jl:70
  [2] read_package(path::String)
    @ Pkg.Types C:\Users\shuhu\.julia\juliaup\julia-1.10.4+0.x64.w64.mingw32\share\julia\stdlib\v1.10\Pkg\src\Types.jl:580
  [3] resolve_projectfile!(env::Pkg.Types.EnvCache, pkg::Pkg.Types.PackageSpec, project_path::String)
    @ Pkg.Types C:\Users\shuhu\.julia\juliaup\julia-1.10.4+0.x64.w64.mingw32\share\julia\stdlib\v1.10\Pkg\src\Types.jl:862
  [4] (::Pkg.Types.var"#57#58"{Pkg.Types.Context, Pkg.Types.PackageSpec, String})(repo::LibGit2.GitRepo)
    @ Pkg.Types C:\Users\shuhu\.julia\juliaup\julia-1.10.4+0.x64.w64.mingw32\share\julia\stdlib\v1.10\Pkg\src\Types.jl:832
  [5] with(f::Pkg.Types.var"#57#58"{Pkg.Types.Context, Pkg.Types.PackageSpec, String}, obj::LibGit2.GitRepo)
    @ LibGit2 C:\Users\shuhu\.julia\juliaup\julia-1.10.4+0.x64.w64.mingw32\share\julia\stdlib\v1.10\LibGit2\src\types.jl:1160
  [6] handle_repo_add!(ctx::Pkg.Types.Context, pkg::Pkg.Types.PackageSpec)
    @ Pkg.Types C:\Users\shuhu\.julia\juliaup\julia-1.10.4+0.x64.w64.mingw32\share\julia\stdlib\v1.10\Pkg\src\Types.jl:782
  [7] handle_repos_add!(ctx::Pkg.Types.Context, pkgs::Vector{Pkg.Types.PackageSpec})
    @ Pkg.Types C:\Users\shuhu\.julia\juliaup\julia-1.10.4+0.x64.w64.mingw32\share\julia\stdlib\v1.10\Pkg\src\Types.jl:852
  [8] add(ctx::Pkg.Types.Context, pkgs::Vector{…}; preserve::Pkg.Types.PreserveLevel, platform::Base.BinaryPlatforms.Platform, kwargs::@Kwargs{…})
    @ Pkg.API C:\Users\shuhu\.julia\juliaup\julia-1.10.4+0.x64.w64.mingw32\share\julia\stdlib\v1.10\Pkg\src\API.jl:258
  [9] add(pkgs::Vector{Pkg.Types.PackageSpec}; io::Base.TTY, kwargs::@Kwargs{})
    @ Pkg.API C:\Users\shuhu\.julia\juliaup\julia-1.10.4+0.x64.w64.mingw32\share\julia\stdlib\v1.10\Pkg\src\API.jl:159
 [10] add(pkgs::Vector{Pkg.Types.PackageSpec})
    @ Pkg.API C:\Users\shuhu\.julia\juliaup\julia-1.10.4+0.x64.w64.mingw32\share\julia\stdlib\v1.10\Pkg\src\API.jl:148
 [11] add
    @ C:\Users\shuhu\.julia\juliaup\julia-1.10.4+0.x64.w64.mingw32\share\julia\stdlib\v1.10\Pkg\src\API.jl:146 [inlined]
 [12] add(; name::Nothing, uuid::Nothing, version::Nothing, url::String, rev::Nothing, path::Nothing, mode::Pkg.Types.PackageMode, subdir::String, kwargs::@Kwargs{})
    @ Pkg.API C:\Users\shuhu\.julia\juliaup\julia-1.10.4+0.x64.w64.mingw32\share\julia\stdlib\v1.10\Pkg\src\API.jl:176
 [13] top-level scope
    @ REPL[24]:1
Some type information was truncated. Use `show(err)` to see complete types.

```

Any suggestion? Besides, how shall I do the same operation in REPL pkg mode, e,g, `add ...`.

---

<div class="post-metadata">

### Author: ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)
#### Post date: [August 15, 2024, 2:06pm UTC](https://discourse.julialang.org/t/how-to-add-package-from-a-github-repos-subfolder/118223/2 "2024-08-15T14:06:06Z")

</div>

The Pkg REPL invocation is

```julia
pkg> add https://github.com/ShuhuaGao/GADNF:src/GADNF.jl

```

This (and your `Pkg.add` call) works on the first try but reproduces your failure if it is repeated. If I remove the corresponding entry in `~/.julia/clones` it again works, but still only once.

So it looks like a Pkg bug. I recommend opening an issue at [GitHub - JuliaLang/Pkg.jl: Pkg - Package manager for the Julia programming language](https://github.com/JuliaLang/Pkg.jl).

---

<div class="post-metadata">

### Author: ![Shuhua](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shuhua/32/27618_2.png) [@Shuhua](https://discourse.julialang.org/u/Shuhua)
#### Post date: [August 15, 2024, 3:00pm UTC](https://discourse.julialang.org/t/how-to-add-package-from-a-github-repos-subfolder/118223/3 "2024-08-15T15:00:00Z")

</div>

Thanks. I cleaned the `~/.julia/clones`, and ran `pkg> add https://github.com/ShuhuaGao/GADNF:src/GADNF.jl`. But it was stuck on “cloning” forever.

```julia
(jl_Ld4ZeV) pkg> add https://github.com/ShuhuaGao/GADNF:src/GADNF.jl
     Cloning git-repo `https://github.com/ShuhuaGao/GADNF`

```

```julia
julia> versioninfo()
Julia Version 1.10.4
Commit 48d4fd4843 (2024-06-04 10:41 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Windows (x86_64-w64-mingw32)
  CPU: 16 × 13th Gen Intel(R) Core(TM) i5-13500H
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-15.0.7 (ORCJIT, goldmont)
Threads: 16 default, 0 interactive, 8 GC (on 16 virtual cores)
Environment:
  JULIA_NUM_THREADS = auto

```

---

<div class="post-metadata">

### Author: ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)
#### Post date: [August 15, 2024, 3:09pm UTC](https://discourse.julialang.org/t/how-to-add-package-from-a-github-repos-subfolder/118223/4 "2024-08-15T15:09:57Z")

</div>

Cloning took less than a second for me, so that’s either a networking problem or a different behavior between Windows and Linux. Either way it’s a separate issue from the original one.

Things to check for the cloning problem:

- Can you add any other package by URL?
- Can you `git clone` your repo outside of Julia?
- Does it make any difference if you set the environment variable `JULIA_PKG_USE_CLI_GIT` to `true`?

---

<div class="post-metadata">

### Author: ![Shuhua](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shuhua/32/27618_2.png) [@Shuhua](https://discourse.julialang.org/u/Shuhua)
#### Post date: [August 17, 2024, 10:47am UTC](https://discourse.julialang.org/t/how-to-add-package-from-a-github-repos-subfolder/118223/5 "2024-08-17T10:47:40Z")

</div>

- Can add Example.jl normally as [GitHub - JuliaLang/Example.jl: Example Julia package repo.](https://github.com/JuliaLang/Example.jl)
- Can git clone the GADNF repo
- Now the error becomes

```julia
(@v1.10) pkg> add https://github.com/ShuhuaGao/GADNF:src/GADNF.jl
ERROR: invalid git HEAD (reference 'refs/heads/master' not found)

```

after I set `JULIA_PKG_USE_CLI_GIT`. The issue stays even if I remove the environment variable.

---

<div class="post-metadata">

### Author: ![kellertuer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kellertuer/32/220707_2.png) [@kellertuer](https://discourse.julialang.org/u/kellertuer)
#### Post date: [August 17, 2024, 11:01am UTC](https://discourse.julialang.org/t/how-to-add-package-from-a-github-repos-subfolder/118223/6 "2024-08-17T11:01:21Z")

</div>

It seems your command assumes the default branch is `master`; however a few years back this default changed (for new repositories) and is not `main`, for example for that repository.

So according to here (did not test it) [Add package with specific branch failing since julia 1.5.2 · Issue #2230 · JuliaLang/Pkg.jl · GitHub](https://github.com/JuliaLang/Pkg.jl/issues/2230)  
its probably

```julia
(@v1.10) pkg> add https://github.com/ShuhuaGao/GADNF#main:src/GADNF.jl

```

---

<div class="post-metadata">

### Author: ![Shuhua](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shuhua/32/27618_2.png) [@Shuhua](https://discourse.julialang.org/u/Shuhua)
#### Post date: [August 17, 2024, 11:17am UTC](https://discourse.julialang.org/t/how-to-add-package-from-a-github-repos-subfolder/118223/7 "2024-08-17T11:17:38Z")

</div>

Thanks for the hints. Unfortunately, neither worked below:

```julia
(jl_vxKllk) pkg> add https://github.com/ShuhuaGao/GADNF#main:src/GADNF.jl
ERROR: invalid git HEAD (reference 'refs/heads/master' not found)

(jl_vxKllk) pkg> add https://github.com/ShuhuaGao/GADNF#master:src/GADNF.jl
ERROR: invalid git HEAD (reference 'refs/heads/master' not found)

```

Nevertheless, it seems true that

> your command assumes the default branch is `master`

Based on this observation, it turns out that the following statement indeed worked:

```julia
Pkg.add(url="https://github.com/ShuhuaGao/GADNF#main", subdir="src/GADNF.jl")

```

though I don’t know how to write the same as a Pkg command in REPL.

---

<div class="post-metadata">

### Author: ![kellertuer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kellertuer/32/220707_2.png) [@kellertuer](https://discourse.julialang.org/u/kellertuer)
#### Post date: [August 17, 2024, 11:23am UTC](https://discourse.julialang.org/t/how-to-add-package-from-a-github-repos-subfolder/118223/8 "2024-08-17T11:23:26Z")

</div>

Well, I was hoping my adaption would work but at least the long version then did work,I did not find much of how to otherwise specify this for `] add`, so I do not now that either.

The error message of your first command seems to indicate the branch is not recognised there (the `#main` part).
