diff --git a/CMakeLists.txt b/CMakeLists.txt
index 9a8eb2e38296541a53c1664a9093c2b7e1957f4a..d6d6ee546c113f483d9e5f61b2ab5aeddc661652 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -28,6 +28,32 @@ set(CMAKE_CXX_STANDARD 17)
 set(CMAKE_CXX_STANDARD_REQUIRED ON)
 set(CMAKE_CXX_EXTENSIONS OFF)
 
+if(BUILD_SHARED_LIBS)
+    set(CMAKE_POSITION_INDEPENDENT_CODE ON)
+endif()
+
+if (BUILD_SHARED_LIBS)
+    # Always full RPATH
+    # use, i.e. don't skip the full RPATH for the build tree
+    set(CMAKE_SKIP_BUILD_RPATH FALSE)
+
+    # when building, don't use the install RPATH already
+    # (but later on when installing)
+    set(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)
+
+    set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
+
+    # add the automatically determined parts of the RPATH
+    # which point to directories outside the build tree to the install RPATH
+    set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
+
+    # the RPATH to be used when installing, but only if it's not a system directory
+    list(FIND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES "${CMAKE_INSTALL_PREFIX}/lib" isSystemDir)
+    if("${isSystemDir}" STREQUAL "-1")
+        set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
+    endif("${isSystemDir}" STREQUAL "-1")
+endif()
+
 file(GLOB MLIP_SRC CONFIGURE_DEPENDS "*.cpp" "*.h" "*.hpp")
 file(GLOB_RECURSE ANALYTICS_SRC CONFIGURE_DEPENDS 
     "analytics/*.cpp" "analytics/*.h" "analytics/*.hpp")