NEW DELHI — India’s prime minister ordered all 1.3 billion people in the country to stay inside their homes for three weeks starting Wednesday — the biggest and most severe action undertaken anywhere to stop the spread of the coronavirus.

“There will be a total ban of coming out of your homes,” the prime minister, Narendra Modi, announced on television Tuesday night, giving Indians less than four hours’ notice before the order took effect at 12:01 a.m.

“Every district, every lane, every village will be under lockdown,” Modi said.

“If you can’t handle these 21 days, this country will go back 21 years,” he said. “The only option is social distancing, to remain away from each other. There is no way out to escape from coronavirus besides this.”

Modi did not make clear how people would get food, water and other necessities during the lockdown, or how they would maintain a safe distance from one another in the cramped spaces many now live. Nor did the prime minister make clear whether exceptions would be made for workers deemed critical to keep the country — the world’s most populous nation after China — functioning.

The breadth and depth of such a challenge is staggering in a country where hundreds of millions of citizens are destitute and countless millions live in packed urban areas with poor sanitation and weak public health care.

Advertising

Though India’s number of reported coronavirus cases remains relatively low — around 500 — the fear is that, should the virus hit as it has in the United States, Europe or China, the consequences would lead to a disaster far bigger than anywhere else.

Modi announced the total lockdown after having already decreed a series of increasingly harsh restrictions on many of India’s more than 30 states and territories.

Earlier Tuesday, several states were already under lockdown. The long straight boulevards of New Delhi, the capital, looked like deserted racetracks. But in the poorer neighborhoods, it was a different story.

People were still out, jostling with each other in narrow lanes and still crowded into bus shelters, sleeping eight to a room in shabby tenements, and showing the impossibility of maintaining social distance.

Long lines of migrant workers streamed out of recently closed railway stations, with thousands of men, almost none of them wearing masks, marching close together to far-off villages, potentially spreading the virus deep into the countryside.

In the densely populated quarters that ring all sides of the capital, many people are laborers and can’t work from home or purchase essential items online. And when they step out to get food for their families, they enter viselike canyons between three and four story cement block buildings built so close to each other that it is difficult to see even a little slice of sky.

Advertising

“How can we practice social distancing here?’’ asked Amit Kumar, a shopkeeper.

He glanced around the lanes littered with garbage. Nearby, one man cleared his throat and spat an oyster of phlegm into the middle of the sidewalk.

Coronavirus, Kumar said, is creating “a mountain of problems.’’

There has also been a lot of confusion. Police officers have aggressively shut down some food stores, despite government directives to keep them open. Officers have also beaten up journalists, accusing them of violating lockdown rules even though government directives printed on the front page of all the major newspapers explicitly allow journalists to do their work.

Westerners have been thrown out of hotels across the country, and some European embassies have reported that a few of their citizens have been assaulted; many Indians believe that Westerners could be carrying the virus.

And it’s not entirely unfounded. So far, most of India’s cases have come from foreign travelers or Indians returning home from overseas. Community transmission remains very low, if it’s even happening at all, Indian officials have said.

Before India shut down international flights over the weekend, Indians landing back at home described a scene of utter chaos. Huge crowds were pushing and shoving and passengers were squeezed close together in jammed arrival halls. People waited on their feet for hours with little food or water.

Aaliyah Khan, a researcher at a military research organization, arrived at New Delhi’s international airport on Saturday afternoon and was shuffled around by different health and immigration authorities for more than 30 hours before being admitted to a government quarantine center 50 miles outside the city.

“They had no idea where to take me and they started treating me like an untouchable,’’ she said. “The person in charge was shouting at me all the time: ‘Stand there! Do this! do that!’”

Still, many experts agree that putting India on lockdown, however harshly, is the country’s only hope to contain the spread of the virus.

“There is no option but to go for a complete lockdown,” said Lt. Gen. D.S. Hooda, a former senior army commander. “With India’s population density and the state of public health infrastructure, we may not be able to handle a large scale outbreak.”

The lockdown includes schools, offices, factories, parks, temples, railways, even the airspace. Borders are being sealed between the states.Economists said a prolonged lockdown could devastate India, where slowing growth rates have already deeply wounded the economy.

In a recent column for The Hindu, Jean Drèze, a prominent Belgian-Indian economist, said almost everybody in India’s informal economy — a huge share of the country’s workforce — has been hit by an “economic tsunami.”

Advertising

As news of the lockdown spread, migrant workers stuck in cities rushed to book train tickets to their villages — or risk being trapped indefinitely. With severe supply chain disruptions, farmers worried that the coming wheat harvest would fail to reach millions of Indians who depend on their crops for survival.

“This situation is worse than war,” said Arun Kumar, an economics professor at the Institute of Social Sciences in New Delhi. “If we are not able to provide essentials to the bottom 50% of the population, then there will be social revolt.”

And yet, India has won praise from international public health experts for its swift movement, and it has some built-in advantages as well.

Young people have a better chance of coping with the virus. And the population in India is considerably younger — the median age is around 28 — compared to a country like Italy, where the median age is around 45. Lockdown measures and travel restrictions were put into place comparatively earlier than other nations. And the authorities in many states have already started making plans for cash and food handouts.

In Punjab state, the government is bracing for the virus to spread after thousands of Punjabis living in Europe recently returned. On Tuesday, a strict round-the-clock curfew was enforced in many areas. A government milk cooperative delivered milk door to door to ensure people had no reason go outside.

Dr. Jayaprakash Muliyil, one of India’s leading epidemiologists, urged an even more aggressive response to the spread of the virus, and said the government needed to move quickly to reach millions of Indians in isolated communities where formal education is lacking and information about the virus and its severity remains limited.

Advertising

“The bulk of the population hasn’t understood what the hell is going on,” he said. “If we do nothing, we’ll have millions of cases and millions of deaths in the next three months. India’s health care system is absolutely unprepared.”

India’s public health sector is underfunded and overstretched even in the best of times. India has around 0.5 hospital beds for every 1,000 people, according to data from the Organization for Economic Cooperation and Development. By comparison, Italy has 3.2 and China has 4.3 beds.

Density is another challenge. India has some of Asia’s biggest slums, and experts fear what would happen if the coronavirus spreads in a city like Mumbai, home to 20 million people, where space is a premium and it is common to find people living half a dozen to a room.

Dr. S.D. Gupta, a public health expert and the chairman of the Indian Institute of Health Management Research, said India’s social structure, in which several generations of a family often live together, complicated social distancing guidelines and put the older people who suffer substantially higher mortality rates at risk.

Still, he said India’s extraordinary ability to mobilize in times of uncertainty — from blunting the force of cyclones to eradicating smallpox — suggested that the country could get ahead of the coronavirus if strict measures were kept in place and the populace obeyed them.

So far, Gupta said, many have.

“This country has great resilience and people come together in an emergency,” he said. “We can beat this.”

Advertising

In eastern Delhi, people are not so sure. The area is the single most densely populated district in all of India, with 36,155 people per square kilometer.

On Tuesday, people were bracing for more restrictions. And in a place full of daily laborers and causal workers, it was the restrictions, more than the virus, that scared them.

“All I’m thinking about right now is how to put food in my children’s stomachs,’’ said Majid Khan, a house painter. He had not worked in days, he had 3,000 rupees (less than $50) in his pocket and zero in his bank account, and his rent was overdue.

“These are my problems,” he said.