Dark matter in the universe evolves through gravity to form a complex network of halos, filaments, sheets and voids, that is known as the cosmic web. Computational models of the underlying physical processes, such as classical N-body simulations, are extremely resource intensive, as they track the action of gravity in an expanding universe using billions of particles as tracers of the cosmic matter distribution. Therefore, upcoming cosmology experiments will face a computational bottleneck that may limit the exploitation of their full scientific potential. To address this challenge, we demonstrate the application of a machine learning technique called Generative Adversarial Networks (GAN) to learn models that can efficiently generate new, physically realistic realizations of the cosmic web. Our training set is a small, representative sample of 2D image snapshots from N-body simulations of size 500 and 100 Mpc. We show that the GAN-generated samples are qualitatively and quantitatively very similar to the originals. For the larger boxes of size 500 Mpc, it is very difficult to distinguish them visually. The agreement of the power spectrum $P_k$ is 1–2% for most of the range, between $k=0.06$ and $k=0.4$. For the remaining values of $k$, the agreement is within 15%, with the error rate increasing for $k>0.8$. For smaller boxes of size 100 Mpc, we find that the visual agreement to be good, but some differences are noticable. The error on the power spectrum is of the order of 20%. We attribute this loss of performance to the fact that the matter distribution in 100 Mpc cutouts was very inhomogeneous between images, a situation in which the performance of GANs is known to deteriorate. We find a good match for the correlation matrix of full $P_k$ range for 100 Mpc data and of small scales for 500 Mpc, with ∼20% disagreement for large scales. An important advantage of generating cosmic web realizations with a GAN is the considerable gains in terms of computation time. Each new sample generated by a GAN takes a fraction of a second, compared to the many hours needed by traditional N-body techniques. We anticipate that the use of generative models such as GANs will therefore play an important role in providing extremely fast and precise simulations of cosmic web in the era of large cosmological surveys, such as Euclid and Large Synoptic Survey Telescope (LSST).