Abstract Structural, electronic and optical properties of Cd and Mg doped single walled ZnO nanotubes are investigated by using density functional theory. Our calculated formation energy shows that Mg doped ZnO nanotubes are stable than pristine and Cd doped ZnO nanotubes. It is found that semiconducting energy gap of Cd doped single walled ZnO nanotubes is smaller than their undoped ZnO nanotubes while it is bigger for Mg doped single walled ZnO nanotubes. This could be explained in terms of electronegativity difference of Cd with respect to Zn in the Cd doped and Mg with respect to Zn in the Mg doped case. A red shift is observed in the Cd doped case while a blue shift is seen in the Mg doped single walled ZnO nanotubes. Both static dielectric constant and static refraction index increased by increasing Cd doped concentration while for doped Mg single walled ZnO nanotubes they are reduced.