A search for the standard model (SM) Higgs boson decaying to two muons is presented using data recorded by the CMS experiment at the LHC in 2016 at a center-of-mass energy of 13~\mathrm{TeV}, corresponding to an integrated luminosity of 35.9~\mathrm{fb}^{-1}. Limits are set on the cross section of the Higgs boson decaying to two muons for mass hypotheses between and 130~\mathrm{GeV}. For a Higgs boson with a mass of 125~\mathrm{GeV} decaying to two muons, the confidence level (CL) observed (expected) upper limit on the production rate is found to be () times the SM value. The combination with data recorded at center-of-mass energies of and 8~\mathrm{TeV} yields a CL observed (expected) upper limit of () times the SM value. This corresponds to an upper limit on the Higgs boson branching fraction to muons of , assuming the SM production cross sections.