ApluUAlberta
ApluUAlberta

Reputation: 154

Setting up ONNX Runtime on Ubuntu 20.04 (C++ API)

I am currently in the midst of trying to get my image processing programs to work on Ubuntu (coming from windows).

I have successfully built and linked the OpenCV and Boost libraries to work with my cpp programs but I have yet to find any instructions as to setting up Onnx Runtime C++ on Ubuntu 20.04 other than using the following command with NuGet Package manager for a specific Visual Studio Project:

Install-Package Microsoft.ML.OnnxRuntime -Version 1.4.0

When on Windows, I would only need use the NuGet package manager to download the library for the given visual studio project. It seems possible to do this on Ubuntu using NuGet, but I was wondering if I could do it more "manually" like the boost and OpenCV building and installing. Thanks!

Upvotes: 5

Views: 16871

Answers (2)

Jan Ca
Jan Ca

Reputation: 173

Installing the NuGet Onnxruntime Release on Linux

Tested on Ubuntu 20.04

For the newer releases of onnxruntime that are available through NuGet I've adopted the following workflow: Download the release (here 1.7.0 but you can update the link accordingly), and install it into ~/.local/. For a global (system-wide) installation you may put the files in the corresponding folders under /usr/local/.

mkdir /tmp/onnxInstall
cd /tmp/onnxInstall
wget -O onnx_archive.nupkg https://www.nuget.org/api/v2/package/Microsoft.ML.OnnxRuntime/1.7.0
unzip onnx_archive.nupkg
cp runtimes/linux-x64/native/libonnxruntime.so ~/.local/lib/
cp -r build/native/include/ ~/.local/include/onnxruntime/

Cmake

Now if you want to be able to find_package(onnxruntime) from your Cmake package, I suggest you place my self-created onnx cmake files in ~/.local/share/cmake/onnxruntime. The files are:

cat ~/.local/share/cmake/onnxruntime/onnxruntimeVersion.cmake:

# Custom cmake version file by jcarius

set(PACKAGE_VERSION "1.7.0")

# Check whether the requested PACKAGE_FIND_VERSION is compatible
if("${PACKAGE_VERSION}" VERSION_LESS "${PACKAGE_FIND_VERSION}")
  set(PACKAGE_VERSION_COMPATIBLE FALSE)
else()
  set(PACKAGE_VERSION_COMPATIBLE TRUE)
  if("${PACKAGE_VERSION}" VERSION_EQUAL "${PACKAGE_FIND_VERSION}")
    set(PACKAGE_VERSION_EXACT TRUE)
  endif()
endif()

cat ~/.local/share/cmake/onnxruntime/onnxruntimeConfig.cmake

# Custom cmake config file by jcarius to enable find_package(onnxruntime) without modifying LIBRARY_PATH and LD_LIBRARY_PATH
#
# This will define the following variables:
#   onnxruntime_FOUND        -- True if the system has the onnxruntime library
#   onnxruntime_INCLUDE_DIRS -- The include directories for onnxruntime
#   onnxruntime_LIBRARIES    -- Libraries to link against
#   onnxruntime_CXX_FLAGS    -- Additional (required) compiler flags

include(FindPackageHandleStandardArgs)

# Assume we are in <install-prefix>/share/cmake/onnxruntime/onnxruntimeConfig.cmake
get_filename_component(CMAKE_CURRENT_LIST_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)
get_filename_component(onnxruntime_INSTALL_PREFIX "${CMAKE_CURRENT_LIST_DIR}/../../../" ABSOLUTE)

set(onnxruntime_INCLUDE_DIRS ${onnxruntime_INSTALL_PREFIX}/include)
set(onnxruntime_LIBRARIES onnxruntime)
set(onnxruntime_CXX_FLAGS "") # no flags needed


find_library(onnxruntime_LIBRARY onnxruntime
    PATHS "${onnxruntime_INSTALL_PREFIX}/lib"
)

add_library(onnxruntime SHARED IMPORTED)
set_property(TARGET onnxruntime PROPERTY IMPORTED_LOCATION "${onnxruntime_LIBRARY}")
set_property(TARGET onnxruntime PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${onnxruntime_INCLUDE_DIRS}")
set_property(TARGET onnxruntime PROPERTY INTERFACE_COMPILE_OPTIONS "${onnxruntime_CXX_FLAGS}")

find_package_handle_standard_args(onnxruntime DEFAULT_MSG onnxruntime_LIBRARY onnxruntime_INCLUDE_DIRS)

Upvotes: 3

Lifu Huang
Lifu Huang

Reputation: 12869

For the default CPU version, you can download the tgz file here.

For other flavors of ONNX runtime that are not available as tgz or NuGet (e.g. TensorRT), you can build them locally:

./build.sh --cudnn_home <path to cuDNN e.g. /usr/lib/x86_64-linux-gnu/> --cuda_home <path to folder for CUDA e.g. /usr/local/cuda> --use_tensorrt --tensorrt_home <path to TensorRT home>

More instructions can be found at Building ONNX Runtime

Upvotes: 1

Related Questions