Highly recommend checking out Packer and/or Packer tutorials. They will teach you what to do to an OS image to reset it before you turn it into an image. Which differs for each OS slightly. I highly recommend installing everything AFTER you deploy it, but there are definitely certain scenarios where it is less of a big deal and depending on how long it takes to setup the environment then it might make sense to bake it into an image. It is definitely less of a concern with Linux compared to Windows, but there are still things you need to do to a Linux image to "reset" it before you turn it into an image.