I found two useful installation tutorials. The basic process is the same. One has a more detailed routine and it is more customary to create a build folder. The other one has pictures when configuring two text files, so it is not easy to make mistakes. You can refer to:
Official website download: Releases - OpenCV
Reference 1:
Reference 2:
Leave it here in case you forget to install it later.
In addition, opencv2 and 3 conflict, but 4 and 23 do not conflict. Sometimes the dependencies may be different. My Ubuntu installed 3.4.11 and 4.6.0, but when I actually used it (rgbd code in ch5 of slambook2), an error was reported. In fact, it was because that code did not use opencv4.
Both versions are installed. You can refer to this tutorial to select the version: