libgit2sharp: System.DllNotFoundException: lib/linux/x86_64/libgit2-1196807.so on Ubuntu Linux

I have difficulties with LibGit2Sharp while running Cake (with Cake.Git addin) build on Linux Ubuntu 16.04 in Docker - https://github.com/cake-contrib/Cake_Git/issues/68 But I managed to isolate issue with LibGit2Sharp only.

Reproduction steps

touch gt.cs

using LibGit2Sharp;
using System;

public class Program
{
    public static void Main()
    {
        Console.WriteLine("HI");
        var repo = new Repository("/opt/buildagent/work/e1fb3d71588668ec");
    }
}

csc gt.cs /r:LibGit2Sharp.dll

mono gt.exe

HI

Unhandled Exception:
System.TypeInitializationException: The type initializer for 'LibGit2Sharp.Core.NativeMethods' threw an exception. ---> System.DllNotFoundException: lib/linux/x86_64/libgit2-1196807.so
  at (wrapper managed-to-native) LibGit2Sharp.Core.NativeMethods.git_libgit2_init()
  at LibGit2Sharp.Core.NativeMethods+LibraryLifetimeObject..ctor () [0x00006] in <c31a5a5a3e0a424683609ecf6c55de77>:0
  at LibGit2Sharp.Core.NativeMethods..cctor () [0x00054] in <c31a5a5a3e0a424683609ecf6c55de77>:0
   --- End of inner exception stack trace ---
  at LibGit2Sharp.Core.Proxy.git_repository_open (System.String path) [0x00008] in <c31a5a5a3e0a424683609ecf6c55de77>:0
  at LibGit2Sharp.Repository..ctor (System.String path, LibGit2Sharp.RepositoryOptions options, LibGit2Sharp.Repository+RepositoryRequiredParameter requiredParameter) [0x00312] in <c31a5a5a3e0a424683609ecf6c55de77>:0
  at LibGit2Sharp.Repository..ctor (System.String path) [0x00000] in <c31a5a5a3e0a424683609ecf6c55de77>:0
  at Program.Main () [0x0000c] in <f40fe756e0ec4a4d9aed452251bd0a53>:0
[ERROR] FATAL UNHANDLED EXCEPTION: System.TypeInitializationException: The type initializer for 'LibGit2Sharp.Core.NativeMethods' threw an exception. ---> System.DllNotFoundException: lib/linux/x86_64/libgit2-1196807.so
  at (wrapper managed-to-native) LibGit2Sharp.Core.NativeMethods.git_libgit2_init()
  at LibGit2Sharp.Core.NativeMethods+LibraryLifetimeObject..ctor () [0x00006] in <c31a5a5a3e0a424683609ecf6c55de77>:0
  at LibGit2Sharp.Core.NativeMethods..cctor () [0x00054] in <c31a5a5a3e0a424683609ecf6c55de77>:0
   --- End of inner exception stack trace ---
  at LibGit2Sharp.Core.Proxy.git_repository_open (System.String path) [0x00008] in <c31a5a5a3e0a424683609ecf6c55de77>:0
  at LibGit2Sharp.Repository..ctor (System.String path, LibGit2Sharp.RepositoryOptions options, LibGit2Sharp.Repository+RepositoryRequiredParameter requiredParameter) [0x00312] in <c31a5a5a3e0a424683609ecf6c55de77>:0
  at LibGit2Sharp.Repository..ctor (System.String path) [0x00000] in <c31a5a5a3e0a424683609ecf6c55de77>:0
  at Program.Main () [0x0000c] in <f40fe756e0ec4a4d9aed452251bd0a53>:0

Mono looks up for native module in subfolder bacause of config

cat LibGit2Sharp.dll.config

<configuration>
    <dllmap os="linux" cpu="x86-64" wordsize="64" dll="git2-1196807" target="lib/linux/x86_64/libgit2-1196807.so" />
    <dllmap os="osx" cpu="x86,x86-64" dll="git2-1196807" target="lib/osx/libgit2-1196807.dylib" />
</configuration>

the file lib/linux/x86_64/libgit2-1196807.so does exist. also tried to copy it besides my .exe module.

Expected behavior

no error

Actual behavior

System.TypeInitializationException: The type initializer for ‘LibGit2Sharp.Core.NativeMethods’ threw an exception. —> System.DllNotFoundException: lib/linux/x86_64/libgit2-1196807.so

Version of LibGit2Sharp (release number or SHA1)

libgit2-1196807.so

Operating system(s) tested; .NET runtime tested

# cat /etc/os-release
NAME="Ubuntu"
VERSION="16.04.3 LTS (Xenial Xerus)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 16.04.3 LTS"
VERSION_ID="16.04"
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
VERSION_CODENAME=xenial
UBUNTU_CODENAME=xenial
# mono --version
Mono JIT compiler version 5.12.0.226 (tarball Thu May  3 09:48:32 UTC 2018)
Copyright (C) 2002-2014 Novell, Inc, Xamarin Inc and Contributors. www.mono-project.com
        TLS:           __thread
        SIGSEGV:       altstack
        Notifications: epoll
        Architecture:  amd64
        Disabled:      none
        Misc:          softdebug
        Interpreter:   yes
        LLVM:          supported, not enabled.
        GC:            sgen (concurrent by default)

About this issue

  • Original URL
  • State: open
  • Created 6 years ago
  • Reactions: 1
  • Comments: 22 (8 by maintainers)

Most upvoted comments

I got hit with this same problem of libgit2sharp not being able to load the native library (In my case, I had all dependencies needed for the library, but running on Arch Linux instead of Ubuntu. Which is probably not officially supported).

I was able to solve this by adding a new dllmap in the LibGit2Sharp.dll.config config file to fallback to the system installed libgit2.so library.

I appended the following to the configuration file (without removing anything)

<dllmap os="linux" dll="git2-6311e88" target="libgit2.so" />

Hopefully this helps someone else coming across this issue with the same problem.

On debian 9 with v0.26.0 and libgit2sharp.nativebinaries.2.0.278 I needed to change from

<dllmap os="linux" cpu="x86-64" wordsize="64" dll="git2-572e4d8" target="lib/linux-x64/libgit2-572e4d8.so" />
to
<dllmap os="linux" cpu="x86-64" wordsize="64" dll="git2-572e4d8" target="lib/debian.9-x64/libgit2-572e4d8.so" />

The latest 0.26.0 preview release includes both an Ubuntu 18.04 and Debian 9 binary, so it should work.

As a workaround for me worked this trick with installing libgit2-24 globally and setup its target path in LibGit2Sharp.dll.config (as /usr/lib/x86_64-linux-gnu/libgit2.so.24). But as I’m using it via Cake/Cake.Git it’s very hard to use this workaround.