Before you start creating GUI applications with PySide2, you need to have a working installation of PySide2 on your system. In this step-by-step tutorial, you'll learn how to install PySide2 on Ubuntu Linux, including setting up virtual environments and verifying the installation.
This guide is also available for macOS and Windows.
Prerequisites: Preparing Ubuntu to Install PySide2
Before installing PySide2, we need to prepare our operating system and install a couple of tools. Python best practices recommend using virtual environments to isolate our Python projects and manage their dependencies.
Unfortunately, the default Ubuntu installation doesn't include tools like the venv module, which allows us to create virtual environments, and the pip command, which lets us install external packages. So, we need to install them from the distro's repositories.
Installing venv and pip on Ubuntu
To install the venv module and the pip command in Ubuntu, run the following commands in your terminal:
$ sudo apt update
$ sudo apt install python3-venv python3-pip
The first command updates the package information from all sources of software we are using in our Ubuntu system. The second command downloads and installs the venv module and the pip command. Now, we are ready to create and activate a Python virtual environment.
Creating a Virtual Environment for PySide2
Once we've installed the venv module, we can proceed to create and activate a Python virtual environment:
$ mkdir project/ && cd project/
$ python3 -m venv ./venv
$ source venv/bin/activate
(venv) $
First, we create a new working directory named project/ as a root for a hypothetical PySide2 project. Then, we create a new virtual environment with venv and activate it. Note how the prompt indicator changes to signal that we're in an active virtual environment.
Install PySide2 With pip
The quickest way to install PySide2 in a virtual environment is to use pip. Run the following command in your active virtual environment:
(venv) $ pip3 install PySide2
This command downloads PySide2 from the Python package index (PyPI) and installs it in our virtual environment.
Verify the PySide2 Installation
After the PySide2 installation is finished, we can verify everything is working correctly. Run the python3 command in your virtual environment to start an interactive session and import the library:
>>> import PySide2
If no errors appear, PySide2 has been installed successfully.
You can also check the installed version of PySide2 by running:
>>> import PySide2.QtCore
>>> print(PySide2.QtCore.__version__)
This prints the version string, confirming which release of PySide2 is installed in your environment.
Troubleshooting Common PySide2 Installation Issues on Ubuntu
If you run into problems during installation, here are a few common issues and solutions:
ModuleNotFoundError: No module named 'PySide2'— Make sure your virtual environment is activated before running your script. You should see(venv)in your terminal prompt.pip3 installfails with a permissions error — Ensure you're installing inside a virtual environment rather than system-wide. Avoid usingsudowithpip.- Missing system libraries — Some Ubuntu versions may require additional system packages. If you see errors related to missing shared libraries, try installing the Qt dependencies with
sudo apt install libgl1-mesa-glx.
Next Steps
Now that you've completed the PySide2 installation on your Ubuntu Linux system, you can start creating Python GUI applications with PySide2.
Bring Your PyQt/PySide Application to Market
Stuck in development hell? I'll help you get your project focused, finished and released. Benefit from years of practical experience releasing software with Python.