Linux - Manually Creating a Virtual Environment


Unlike Windows, LibreOffice on Linux done not use embedded Python. Instead, it use the system Python3. This means LibreOffice on Linux has access to full python.

Using Install Zaz-Pip LibreOffice Extension would be the recommended way to install pip and python packages in LibreOffice.

Another option is to use the Include Python Path for LibreOffice extension to add virtual environment paths to LibreOffice, this would work with all LibreOffice versions after Version 7.0 on all operating systems.

Note that this guide for of an apt installed version of LibreOffice. It does not cover the Flatpak version or the Snap version. See: Linux - Manually Creating a Virtual Environment for LibreOffice Snap.


The libreoffice-script-provider-python apt package must be installed. This package allows scripts to connect to LibreOffice. On Windows this in not needed because LibreOffice embeds Python. However, on Linux LibreOffice requires it Even for Snaps.

sudo apt install libreoffice-script-provider-python


Get LibreOffice Python Path

We need to know exactly where the system Python3 is located that LibreOffice is using. Perhaps the easiest way is to use the APSO extension for LibreOffice. See Install APSO LibreOffice Extension for more information.

Start LibreOffice and open the APSO extension. In this case we are using Writer.

Tools -> Macros -> Organize python scripts

APSO Extension

Start the Python Console

APSO python console [LibreOffice]
3.10.6 (main, May 29 2023, 11:10:38) [GCC 11.3.0]
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.executable

import sys and then use sys.executable to get the path to the system Python3. In this case the system Python3 is located at /usr/bin/python3. This is the path needed to create the virtual environment.

Close the Python Console and Writer.

Create Project Directory

Create a Directory for your project.

In this case we will use a directory called myproject in the home directory.

mkdir ~/myproject
cd ~/myproject

Create Virtual Environment

In your project folder run the following command.

/usr/bin/python3 -m venv .venv

If you get an error about python3.10-venv not being installed, then install it.

sudo apt install python3.10-venv

Activate Virtual Environment

Activate the virtual environment.

source .venv/bin/activate

Install OOOENV

The virtual environment has been created but it does not yet have access to and which are needed to use the LibreOffice API.

For this we will use the oooenv package.

Make sure you have activated the virtual environment.

oooenv is a Python package that allows you to auto configure a virtual environment to be used by LibreOffice.

Install oooenv in the virtual environment:

python -m pip install oooenv

Now that the package is installed we can use it to configure the virtual environment to use and

oooenv cmd-link -a

Now the virtual environment is configured to use and

Test installed package

First we will install ooo-dev-tools.

python -m pip install ooo-dev-tools

For a test we can write Hello World into a new Writer document.

With ooo-dev-tools installed we can now run LibreOffice python right on the command line and interact with LibreOffice. Alternatively run a script in the APSO console as seen in Test installed package. This simple script starts python, Loads LibreOffice Writer, and writes Hello World!.

Python 3.10.6 (main, May 29 2023, 11:10:38) [GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from ooodev.loader import Lo
>>> from ooodev.write import WriteDoc
>>> def say_hello():
...     doc = WriteDoc.from_current_doc()
...     cursor = doc.get_cursor()
...     cursor.append_para(text="Hello World!")
>>> _ = Lo.load_office(Lo.ConnectSocket())
>>> doc = WriteDoc.create_doc(visible=True)
>>> say_hello()
>>> doc.close()
>>> Lo.close_office()

The resulting document should look like this:

LibreOffice Writer Hello World