Before you start creating GUI applications with PyQt6, you need to have a working installation of PyQt6 on your system. In this step-by-step tutorial, you'll learn how to install PyQt6 on Ubuntu Linux using both pip and apt, so you can choose the method that best fits your project.
This guide is also available for macOS and Windows.
Note that the following instructions are for installing the PyQt6 version registered under the GPL license. If you need to use PyQt in a non-GPL project, then you will need to purchase an alternative license from Riverbank Computing to release your software.
Preparing Ubuntu to Install PyQt6
Before installing PyQt6, 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:
$ 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 Python Virtual Environment
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 PyQt6 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 PyQt6 in a Virtual Environment With pip
Once we've created and activated our virtual environment, we can install PyQt6 with the following command:
PyQt6 Crash Course by Martin Fitzpatrick — The important parts of PyQt6 in bite-size chunks
(venv) $ pip3 install pyqt6
This command downloads PyQt6 from the Python package index (PyPI) and installs it in our virtual environment. Using pip is the recommended approach because it gives you access to the latest version of PyQt6.
Install PyQt6 System-Wide Using apt
Sometimes, we may need to install PyQt6 across the entire operating system rather than in a virtual environment. In Ubuntu's repositories, we'll find packages for PyQt6, although they may be out of date. Check first to ensure you're getting a version that meets your requirements, and if not, use the pip method above.
In Ubuntu, we can install either from the command line with the apt command or via the Software Center. The package we are looking for is python3-pyqt6.
To install PyQt6 from the command line, execute the following command:
$ sudo apt install python3-pyqt6
Verify the PyQt6 Installation
After the PyQt6 installation is finished, we can verify everything is working correctly by starting a Python interactive session and importing the library:
>>> import PyQt6
If you're using the pip installation, make sure you are in the terminal session with the active virtual environment. If you are using the apt installation, you can import the library from any terminal session.
If the import completes without errors, PyQt6 is installed correctly. Now that you've completed the installation on your Ubuntu Linux system, you can start creating Python GUI applications with PyQt6. To build your first window, take a look at our guide on creating your first app with PyQt6.
Create GUI Applications with Python & Qt6 by Martin Fitzpatrick — (PyQt6 Edition) The hands-on guide to making apps with Python — Save time and build better with this book. Over 15K copies sold.