WASHINGTON — In a major statement on privacy in the digital age, the Supreme Court ruled on Friday that the government generally needs a warrant to collect troves of location data about the customers of cellphone companies.

“We decline to grant the state unrestricted access to a wireless carrier’s database of physical location information,” Chief Justice John G. Roberts Jr. wrote for the majority.

The 5-to-4 ruling will protect “deeply revealing” records associated with 400 million devices, the chief justice wrote. It did not matter, he wrote, that the records were in the hands of a third party. That aspect of the ruling was a significant break from earlier decisions.

The Constitution must take account of vast technological changes, Chief Justice Roberts wrote, noting that digital data can provide a comprehensive, detailed — and intrusive — overview of private affairs that would have been impossible to imagine not long ago.