Stereotaxic surgery has been an invaluable tool in systems neuroscience, applied in many experiments for the creation of site-targeted lesions, injection of anatomical tracers or implantation of electrodes or microdialysis probes. In this protocol, we describe stereotaxic surgery optimized for gene delivery by recombinant adeno-associated viruses and lentiviruses in mice and rats. This method allows the manipulation of gene expression in the rodent brain with excellent spatiotemporal control; essentially any brain region of choice can be targeted and cells (or a subpopulation of cells) in that region can be stably genetically altered at any postnatal developmental stage up to adulthood. Many aspects of the method, its versatility, ease of application and high reproducibility, make it an attractive approach for studying genetic, cellular and circuit functions in the brain. The entire protocol can be completed in 1-2 hours.