How to Install and configure Qt5 manually on Ubuntu 18.04

Lynn
5 min readJun 24, 2022

--

This article will introduce the method to install Qt5 on Ubuntu 18.04 by .run files and configure its bin and lib path so that the system can easily identify them and apply them to your software development.

The Qt company

1. Install

Notice that Ubuntu 18.04 isn’t a supported development host for Qt6. So here, we choose to install Qt 5.14.2.

Go to the official website and choose qt-opensource-linux-x64-5.14.2.run to download. Run

$ cd ~/Download
$ chomd +x qt-opensource-linux-x64-5.14.2.run
$ sudo ./qt-opensource-linux-x64-5.14.2.run

Here you have two options. If you run it as a normal user, it will be installed in the current user’s home directory, making the install suitable for use only by the user who installed it. If you run the installer as root, it will be installed globally and be available to all users on the machine, which is located in /opt/Qt5.14.2 by default.

Keep pressing until the following page appears.

Select all except the Desktop gcc 64-bit, which is already installed in our system.

Then, begin to install and quit the installation wizard.

However, since the Ubuntu OS usually comes with the pre-installed Qt version, whose files are scattered in the /usr/share, /usr/bin, and /usr/lib directory.

2. Configure the path

step 0: locate the qtchooser configuration files in your system.

$ locate qtchooser | grep conf/usr/lib/x86_64-linux-gnu/qt-default/qtchooser/default.conf
/usr/lib/x86_64-linux-gnu/qtchooser/4.conf
/usr/lib/x86_64-linux-gnu/qtchooser/5.conf
/usr/lib/x86_64-linux-gnu/qtchooser/qt4.conf
/usr/lib/x86_64-linux-gnu/qtchooser/qt5.conf
/usr/share/qtchooser/qt4-x86_64-linux-gnu.conf
/usr/share/qtchooser/qt5-x86_64-linux-gnu.conf

step1: find out the file locations the symbolic link files linked to

Above is the output of my own run. Those *.conf files represent all of qt versions qtchooser could recognized in our system. And each *.conf file in the /usr/lib/x86_64-linux-gnu/ directory that contains qt4 and qt5 is respectively a symbolic link to the two *.conf files under the /usr/share/qtchooser.

To verify these, Run ls -l followed by any paths like:

$ ls -l /usr/lib/x86_64-linux-gnu/qt-default/qtchooser/default.conflrwxrwxrwx 1 root root 50 22 December 2017 /usr/lib/x86_64-linux-gnu/qtchooser/4.conf -> ../../../share/qtchooser/qt4-x86_64-linux-gnu.conf

Thus, in order to tell the system the specific version we would like to use, the only thing we should do is just to modify the contents in the two original files, or create a new conf file corresponding to our version of Qt, and redirect the default.conf to the configuration files we just modified.

step 2: Create your qt version conf file

Now we just need to create a conf file that describes the paths of our qt version. For example, my Qt is installed at /opt/Qt5.14.2 and I want to set this Qt version as the default one in my system. We could create a file named like qt5.14.2.conf:

$ cd /usr/share/qtchooser
$ sudo vim qt5.14.2.conf

The vim will create the qt5.14.2.conf file automatically. Then fill in the following two lines: the first line means the qmake location and another represents the library path.

/opt/Qt5.14.2/5.14.2/gcc_64/bin
/opt/Qt5.14.2/5.14.2/gcc_64/lib

step 3: Set your qt as the default one

Just redirect the default.conf symbolic link to our qt conf file created at step 2.

$ cd /usr/lib/x86_64-linux-gnu/qt-default/qtchooser/
$ sudo ln -snf ../../../../share/qtchooser/qt5.14.2.conf default.conf

The difference between symbolic links and hard links can refer to this answer:

Briefly speaking, the soft link points to the name, while the hard link points to the contents on disk. Thus, whether the source file is renamed or deleted, the hard link will still be valid, but the soft link will not.

As for the function of the -snf, refer to this answer:

In a word, -s means create a symbolic link apparently. -f means that if the target file already exists, then unlink it so that the new link may occur. -n is alse used as -no-dereference, which means do not resolve an existing link and place the new link inside that directory, but rather just update it.

After configuring all the above steps, the default Qt has been configured and you can type the following command to verify and it should be your Qt as the default.

$ qmake --versionQMake version 3.1
Using Qt version 5.14.2 in /opt/Qt5.14.2/5.14.2/gcc_64/lib

step 4: set the shared libarary path of Qt

In the subsequent development process, we should let the system know where to find our Qt libraries. Just add the following lines into ~/.bashrc.

#Qt
export LD_LIBRARY_PATH=/opt/Qt5.14.2/5.14.2/gcc_64/lib:$LD_LIBRARY_PATH

3. Uninstall

If you want to update, add or remove any components, you can run the maintenance tool, which can be found under the installation directory as MaintenanceTool.

$ cd /opt/Qt5.14.2/
$ ./MaintenanceTool

And you’ll see this:

Just tick the Uninstall only entry, and press Next.

4. Reference

https://www.ics.com/blog/getting-started-qt-and-qt-creator-linux#:~:text=For%20the%20per%20user%20install,location%20is%20%2Fopt%2FQt.

--

--