cbc662f
diff --git a/setup.py b/setup.py
d25013b
index 80267e8..164cabf 100644
cbc662f
--- a/setup.py
cbc662f
+++ b/setup.py
d25013b
@@ -26,6 +26,8 @@ prefer_system_libb2 = True
d25013b
 prefer_system_libxxhash = True
cbc662f
 
cbc662f
 # prefer_system_msgpack is another option, but you need to set it in src/borg/helpers.py.
cbc662f
+prefer_system_msgpack = bool(os.environ.get('BORG_EXTERNAL_MSGPACK'))
cbc662f
+
cbc662f
 
d25013b
 min_python = (3, 5)
cbc662f
 my_python = sys.version_info
cbc662f
@@ -38,6 +40,20 @@ if my_python < min_python:
cbc662f
 on_rtd = os.environ.get('READTHEDOCS')
cbc662f
 
cbc662f
 install_requires = []
cbc662f
+if prefer_system_msgpack:
cbc662f
+    install_requires.extend([
cbc662f
+        # we are rather picky about msgpack versions, because a good working msgpack is
cbc662f
+        # very important for borg, see https://github.com/borgbackup/borg/issues/3753
cbc662f
+        # best versions seem to be 0.4.6, 0.4.7, 0.4.8 and 0.5.6:
cbc662f
+        'msgpack-python >=0.4.6, <=0.5.6, !=0.5.0, !=0.5.1, !=0.5.2, !=0.5.3, !=0.5.4, !=0.5.5',
cbc662f
+        # if you can't satisfy the above requirement, these are versions that might
cbc662f
+        # also work ok, IF you make sure to use the COMPILED version of msgpack-python,
cbc662f
+        # NOT the PURE PYTHON fallback implementation: ==0.5.1, ==0.5.4
cbc662f
+        #
cbc662f
+        # Please note:
cbc662f
+        # using any other version is not supported by borg development and
cbc662f
+        # any feedback related to issues caused by this will be ignored.
cbc662f
+    ])
cbc662f
 
cbc662f
 # note for package maintainers: if you package borgbackup for distribution,
cbc662f
 # please add llfuse as a *requirement* on all platforms that have a working
d25013b
@@ -88,9 +104,9 @@ cython_c_sources = [
cbc662f
 
cbc662f
 cython_cpp_sources = [
cbc662f
     # these .pyx will get compiled to .cpp
cbc662f
-    msgpack_packer_source,
cbc662f
-    msgpack_unpacker_source,
cbc662f
 ]
cbc662f
+if not prefer_system_msgpack:
cbc662f
+    cython_cpp_sources.extend([msgpack_packer_source, msgpack_unpacker_source])
cbc662f
 
cbc662f
 try:
cbc662f
     from Cython.Distutils import build_ext
d25013b
@@ -121,9 +137,12 @@ try:
0fdfa36
                 'src/borg/platform/syncfilerange.c',
cbc662f
                 'src/borg/platform/freebsd.c',
cbc662f
                 'src/borg/platform/darwin.c',
cbc662f
-                'src/borg/algorithms/msgpack/_packer.cpp',
cbc662f
-                'src/borg/algorithms/msgpack/_unpacker.cpp',
cbc662f
             ])
cbc662f
+            if not prefer_system_msgpack:
cbc662f
+                self.filelist.extend([
cbc662f
+                    'src/borg/algorithms/msgpack/_packer.cpp',
cbc662f
+                    'src/borg/algorithms/msgpack/_unpacker.cpp',
cbc662f
+                ])
cbc662f
             super().make_distribution()
cbc662f
 
cbc662f
 except ImportError:
d25013b
@@ -146,11 +165,15 @@ except ImportError:
cbc662f
     msgpack_packer_source = msgpack_packer_source.replace('.pyx', '.cpp')
cbc662f
     msgpack_unpacker_source = msgpack_unpacker_source.replace('.pyx', '.cpp')
cbc662f
 
cbc662f
-    from distutils.command.build_ext import build_ext
cbc662f
-    if not on_rtd and not all(os.path.exists(path) for path in [
cbc662f
+    cythonized_sources = [
cbc662f
         compress_source, crypto_ll_source, chunker_source, hashindex_source, item_source, checksums_source,
0fdfa36
         platform_posix_source, platform_linux_source, platform_syncfilerange_source, platform_freebsd_source, platform_darwin_source,
cbc662f
-        msgpack_packer_source, msgpack_unpacker_source]):
cbc662f
+    ]
cbc662f
+    if not prefer_system_msgpack:
cbc662f
+        cythonized_sources.extend([msgpack_packer_source, msgpack_unpacker_source])
cbc662f
+
cbc662f
+    from distutils.command.build_ext import build_ext
cbc662f
+    if not on_rtd and not all(os.path.exists(path) for path in cythonized_sources):
cbc662f
         raise ImportError('The GIT version of Borg needs Cython. Install Cython or use a released version.')
cbc662f
 
cbc662f
 
d25013b
@@ -809,26 +832,29 @@ if not on_rtd:
d25013b
                                                system_prefix=libxxhash_prefix, system=libxxhash_system,
cbc662f
                                                **crypto_ext_kwargs)
cbc662f
 
cbc662f
-    msgpack_endian = '__BIG_ENDIAN__' if (sys.byteorder == 'big') else '__LITTLE_ENDIAN__'
cbc662f
-    msgpack_macros = [(msgpack_endian, '1')]
cbc662f
-    msgpack_packer_ext_kwargs = dict(
cbc662f
-        sources=[msgpack_packer_source],
cbc662f
-        include_dirs=include_dirs,
cbc662f
-        library_dirs=library_dirs,
cbc662f
-        define_macros=msgpack_macros,
cbc662f
-        language='c++',
cbc662f
-    )
cbc662f
-    msgpack_unpacker_ext_kwargs = dict(
cbc662f
-        sources=[msgpack_unpacker_source],
cbc662f
-        include_dirs=include_dirs,
cbc662f
-        library_dirs=library_dirs,
cbc662f
-        define_macros=msgpack_macros,
cbc662f
-        language='c++',
cbc662f
-    )
cbc662f
+    if not prefer_system_msgpack:
cbc662f
+        msgpack_endian = '__BIG_ENDIAN__' if (sys.byteorder == 'big') else '__LITTLE_ENDIAN__'
cbc662f
+        msgpack_macros = [(msgpack_endian, '1')]
cbc662f
+        msgpack_packer_ext_kwargs = dict(
cbc662f
+            sources=[msgpack_packer_source],
cbc662f
+            include_dirs=include_dirs,
cbc662f
+            library_dirs=library_dirs,
cbc662f
+            define_macros=msgpack_macros,
cbc662f
+            language='c++',
cbc662f
+        )
cbc662f
+        msgpack_unpacker_ext_kwargs = dict(
cbc662f
+            sources=[msgpack_unpacker_source],
cbc662f
+            include_dirs=include_dirs,
cbc662f
+            library_dirs=library_dirs,
cbc662f
+            define_macros=msgpack_macros,
cbc662f
+            language='c++',
cbc662f
+        )
cbc662f
+        ext_modules += [
cbc662f
+            Extension('borg.algorithms.msgpack._packer', **msgpack_packer_ext_kwargs),
cbc662f
+            Extension('borg.algorithms.msgpack._unpacker', **msgpack_unpacker_ext_kwargs),
cbc662f
+        ]
cbc662f
 
cbc662f
     ext_modules += [
cbc662f
-        Extension('borg.algorithms.msgpack._packer', **msgpack_packer_ext_kwargs),
cbc662f
-        Extension('borg.algorithms.msgpack._unpacker', **msgpack_unpacker_ext_kwargs),
cbc662f
         Extension('borg.compress', **compress_ext_kwargs),
cbc662f
         Extension('borg.crypto.low_level', **crypto_ext_kwargs),
cbc662f
         Extension('borg.hashindex', [hashindex_source]),