When I rebuild a server, I want the packages I chose, not the hundreds that arrived as dependencies.
apt-mark showmanual > packages.txt
xargs -a packages.txt sudo apt install -y
apt records whether a package was requested by a person or pulled in as a dependency. showmanual prints the first group, showauto the second and showhold the held ones. A fresh Ubuntu install already has many manual packages from the installer, so the list starts long. Open packages.txt and trim it before you feed it to apt install, because it can name packages that do not exist on the new release. apt-mark auto marks a package as a dependency so that autoremove can clean it up.
Further reading: apt-mark manual page