Building & Installing Xorg Server on Ubuntu 22.04
Desktop ApplicationsI've been interested in learning about the X11 protocol and how Xorg Server works. However, after searching around the internet for quite a bit, I’ve found that there isn't a decent guide on how to to actually build Xorg Server on the latest Linux distros. So I’m writing this down here so that it might appear on search engines the next time someone searches "How to build Xorg Server on Ubuntu." There are quite a few pitfalls to trying to build Xorg Server so at least with this guide you'll have a consistent method for building Xorg Server on a brand new Ubuntu 22.04 installation.
DO NOT TRY THIS ON YOUR HOST MACHINE UNLESS YOU KNOW WHAT YOU'RE DOING. I AM NOT RESPONSIBLE FOR ANY DAMAGES TO YOUR SYSTEM.
Environment
I'm going to keep things simple and build Xorg Server inside Virtualbox. So just set up a Virtual machine like normal, and install Ubuntu.
Steps
Keep in mind that doing any of these steps out of order may end with different results.
1. Install the build tools
To start, we'll install git, cmake, and meson with the command
sudo apt update sudo apt install cmake meson git
1.1 Install your preferred editor
If you have a preferred editor you can install it now too.
sudo apt install neovim
2. Clone the repository
I'm going to create a directory called "Code" and clone the Xorg Server repo inside there.
mkdir Code && cd Code git clone https://gitlab.freedesktop.org/xorg/xserver.git cd xserver
3. Checkout the correct version
I'm going to checkout the tagged version xorg-server-21.1.8 for consistency
git checkout tags/xorg-server-21.1.8
4. Install the dependencies
No where on any internet guides have I found a concrete list of dependencies that you need in order to build Xorg Server. But I understand because there's a million things that Xorg Server depends on. After rooting around in the Xorg Server repo, I stumbled upon the Gitlab installation scripts, which makes installing all the dependencies that we need a ton easier. However the script doesn't quite work out of the box for Ubuntu, so we need to make a couple modification to it. So open it up with our editor.
vim .gitlab-ci/debian-install.sh
Then delete lines 106 to 187 (End of file).
The call to that particular file on 106 gives an error and we don't require anything after it, so we'll delete it.
Then make the script executable with
chmod +x .gitlab-ci/debian-install.sh
Then run the script
.gitlab-ci/debian-install.sh
Then we'll need to install libxcvt with
sudo apt install libxcvt-dev
5. Run Meson and Ninja
First we need to create a build directory for Meson
mkdir build && cd build
Then we'll run the command
meson setup .. \ --prefix=/usr \ --localstatedir=/var \ -Dsuid_wrapper=true \ -Dxkb_output_dir=/var/lib/xkb \ -Dmodule_dir=/usr/lib/xorg/modules
I took that command from the Beyond Linux From Scratch project and added that module_dir line at the bottom. It's there because the default modules directory path when building Xorg Server on this configuration is /usr/lib/x86_64-linux-gnu/xorg/modules, but on Ubuntu the default module path is the one above. If you don't set the module path, then Xorg Server will not be able to find the right drivers, you won't have any input, and you'll have to force shutdown your virtual machine. Alternatively you could define a different module path in an xorg.conf file in /etc/X11 if you don't want to change the default module path.
We'll then build with ninja
ninja
Then we'll install Xorg Server with the command
ninja install
Then we'll log out and log in again (make sure that Ubuntu on Xorg is selected on the login screen).
You probably won't notice much of a difference though, so let's change something.
6. Make a Test Change
I'm going to open the file hw/xfree86/common/xf86Config.c with
vim hw/xfree86/common/xf86Config.c
And then I'm going to steal a log message function call, paste it on line 2315, and change it like so
xf86MsgVerb(X_DEFAULT, 0, "Hello :-)");
Then run ninja and install it like from above, log out, log back in, open up a terminal, and open up the Xorg Server log with
vim ~/.local/share/xorg/Xorg.0.log
Then you should see
Troubleshooting
1. If meson is saying that you're missing a dependency or ninja is complaining that it can't find a header, you can first try searching for the package with
apt-cache search [PACKAGE NAME]
Replacing [PACKAGE NAME] with what is giving you an error.
2. If you can't find the package to solve your issue, you can try going back to the debian-install.sh script mentioned above to try to find the steps to build the particular dependency you may be missing.
3. ERROR: Clock skew detected - Make sure that your system clock is synced correctly with:
sudo timedatectl set-ntp off sudo timedatectl set-ntp on
Source: Ask Ubuntu
4. If you're getting some other issue not mentioned here, you can try asking in the Xorg server IRC channel.
Tips
If you're going to go beyond this tutorial and do more tinkering, I would suggest making a shared folder between Virtualbox and your host machine so you can use your favorite editor outside of the virtual machine.
Conclusion
I hope you've found this tutorial helpful and informative. Enjoy tinkering with Xorg Server!