Installation Guide TensorRT with pip on Ubuntu

TensorRT can be installed via pip for Python-based inference on Ubuntu systems with NVIDIA GPUs, provided that the NVIDIA driver and CUDA Toolkit are properly installed and compatible. The installation process involves adding NVIDIA’s Python package index using nvidia-pyindex, installing nvidia-tensorrt via pip, and verifying the setup by importing the tensorrt module in Python. This approach is suitable for accelerating ONNX, PyTorch, and TensorFlow inference workloads, but it is not intended for C++ TensorRT development or advanced engine compilation scenarios.

By Ruby Abdullah · · tutorial
nvidiatensorrtpippython

TensorRT is an inference optimization library from NVIDIA for accelerating deep learning models (ONNX, PyTorch, TensorFlow) on NVIDIA GPUs. The pip install method is suitable for Python-based inference, not C++ development.

This tutorial focuses on TensorRT via pip, not via the .deb or full SDK.

1. Mandatory Prerequisites

Ensure all of the following components are available:

Ubuntu 20.04 / 22.04

NVIDIA GPU

NVIDIA drivers installed and working

CUDA Toolkit installed

Python 3.8 – 3.11

NVIDIA Docker not required, but recommended

Check GPU:

nvidia-smi

Check CUDA:

nvcc --version

If CUDA is missing, TensorRT will not work even if pip install is successful.

2. Check the CUDA Version Used

TensorRT is very sensitive to CUDA version. Note the CUDA version from:

nvidia-smi

Example:

CUDA Version: 12.2

This is important for selecting a compatible version of TensorRT.

3. Upgrade pip and base dependencies

python3 -m pip install --upgrade pip setuptools wheel

Using a virtual environment is recommended:

python3 -m venv trt-env

source trt-env/bin/activate

4. Install TensorRT Using pip

TensorRT in pip is not available on the public PyPI, but is in the NVIDIA repository.

Step 1: Install NVIDIA PyPI index

pip install nvidia-pyindex

Step 2: Install TensorRT

pip install nvidia-tensorrt

If you want a specific version:

pip install nvidia-tensorrt==9.2.0

5. Verify TensorRT Installation

Enter Python:

python

Then run:

import tensorrt as trt

print(trt.version)

If the TensorRT version appears without errors, the installation was successful.

6. Verify CUDA Access from TensorRT

import tensorrt as trt

logger = trt.Logger(trt.Logger.WARNING)

print("TensorRT OK, CUDA ready")

If no libnvinfer.so or libcuda.so errors appear, the environment is correct.

7. Common Errors and Solutions

Error: libnvinfer.so not found

Cause:

CUDA not detected

NVIDIA driver mismatch

Library path not readable

Solution:

export LDLIBRARYPATH=/usr/local/cuda/lib64:$LDLIBRARYPATH

Permanently add:

echo 'export LDLIBRARYPATH=/usr/local/cuda/lib64:$LDLIBRARYPATH' >> ~/.bashrc

source ~/.bashrc

Error: CUDA driver version is insufficient

Cause:

NVIDIA driver version is lower than TensorRT requirements

Solution:

Upgrade NVIDIA driver

Use an older version of TensorRT and Compatible

8. Important Notes (Must Read)

pip install nvidia-tensorrt is only for Python inference

Not suitable for:

Custom C++ plugins

Compiling the TensorRT engine via C++

For heavy production, it is recommended:

NVIDIA Docker + TensorRT image

  • Or install TensorRT via .deb

9. Brief Summary

NVIDIA + CUDA Drivers

pip install nvidia-pyindex

pip install nvidia-tensorrt

import tensorrt

TensorRT is ready for inference

Related Articles

Complete Pinecone Tutorial: Vector Database for AI and Semantic Search

Tutorial Lengkap Pinecone: Vector Database untuk AI dan Semantic Search Pinecone adalah managed vector database yang dir...

Composio Tutorial: Tool Integration Platform for AI Agents

Tutorial Composio: Platform Integrasi Tool untuk AI Agents Composio adalah platform open-source yang memungkinkan AI age...

Mirascope Tutorial: A Pythonic Toolkit for Building LLM Applications

Tutorial Mirascope: Toolkit Pythonic untuk Membangun Aplikasi LLM Pendahuluan Mirascope adalah library Python yang menye...

NeMo Guardrails Tutorial: Building Safety Rails for LLM Applications

Tutorial NeMo Guardrails: Membangun Pagar Pengaman untuk Aplikasi LLM Dalam era adopsi Large Language Model (LLM) yang s...