That’s two years earlier than originally planned because of about 373,000 preorders for the Model 3 sedan, which the company will start delivering in late 2017.

And that’s no small task. When the factory was announced in 2014, Tesla said it would produce more lithium ion batteries annually by 2020 than were produced worldwide in 2013. The accelerated schedule to supply the Model 3, the automaker’s first mass-market car, doesn’t leave much time to create a complex supply chain that includes expanded mining and exploration operations. It also pits Tesla against consumer-electronics companies, which use the batteries in everything from mobile phones to laptops, and carmakers in China, where the government wants 5 million electric and other new-energy models on the road by 2020. “The world is going to need a lot more lithium ion batteries. Tesla knows that it’s going to have to source the raw materials themselves, and they are competing with China,” says Simon Moores, a managing director at Benchmark Mineral Intelligence. “They need to invest in new supply, and they are conducting a global search.”

Tesla’s Model S sedan, which starts at $66,000 before federal or state incentives, contains more than 7,000 battery cells, which Tesla developed with Japan’s Panasonic. They’re working on larger cells for the Model 3, which will have a high-capacity battery with enhanced energy density, Panasonic said in an e-mail. Energy density, measured as kilowatt hours per kilogram, helps determine range: The more hours of power packed into a car’s battery, the more miles a vehicle can travel on a single charge.

To secure the huge number of cells it needs and drive down the cost, Tesla is collapsing the supply chain and bringing battery-cell production in-house in a move reminiscent of Henry Ford in the 1920s. Ford’s massive Rouge complex in Michigan made most of the components, including engines, glass, and steel, used in its assembly plants and was supplied by Ford-owned iron mines and limestone quarries. Ford even owned and operated a rubber plantation in Brazil.

Musk’s vision now includes Tesla buying SolarCity, of which he is also chairman, so his most passionate customers can get rooftop solar panels, electricity storage units, electric cars, and charging units from Tesla.

For its batteries, Tesla typically uses formulations including lithium, nickel, cobalt, and aluminum oxide. To find a steady and affordable supply of these materials—key to keeping the base price of the Model 3 at about $35,000—the company is recruiting staffers to scour the globe. It hired Rene LeBlanc, a former engineer at FMC Lithium, earlier this year. It’s also looking for a Tokyo-based supply-chain analyst willing to travel frequently to China and South Korea to work closely with suppliers.

Despite all the buzz about lithium, Musk reminded investors at Tesla’s May 31 shareholder meeting that the metal is “just the salt on the salad,” accounting for only about 2 percent of the material in Tesla cells. Yet it’s key to making batteries rechargeable, and even that small percentage doesn’t exempt Tesla from the laws of supply and demand. It’s competing for the metal with companies in Asia, where China, Japan, and Korea account for more than 85 percent of current lithium ion battery output, according to researcher CRU Group.

“Tesla has spent a lot of time working with all the different lithium companies,” including some tiny startups, JB Straubel, the chief technology officer, said at the shareholder meeting. The goal is to make sure “they’re investing on the right timeline to have the capacity ready when we need it.” Tesla has signed supply agreements with Pure Energy Minerals, which is exploring a lithium deposit in Nevada’s Clayton Valley, and Bacanora Minerals, which plans to develop a lithium deposit in Sonora, Mexico.

Macquarie Research estimates the lithium market will show a deficit of about 4,500 metric tons before turning to a slight surplus in 2017 and 2018. But without significant new supply coming online, Macquarie predicts a global shortage of almost 46,000 tons, equal to 16 percent of total demand, by 2021.

The cost of nickel is a bigger factor. At the May 31 meeting, Musk said “the main determinant of the cost of the cell is the price of nickel in the form that we need it.” Prices in May 2014 surged past $21,000 a metric ton after Indonesia, the biggest producer of the mined metal until 2013, announced it would ban the export of all unrefined ores. Since then prices have plummeted more than 50 percent, to about $9,190 a ton on June 21, as other countries, notably the Philippines, increased output and stockpiles remained near record highs.

Tesla, which doesn’t use much cobalt in its batteries, is trying to get rid of it and add nickel, Straubel says in an interview. Supplies outpaced demand in 2015 by about 1,285 tons but should turn to a deficit this year, lasting through 2020 and helping boost the price to $15 a pound by then, from about $10.80 now, according to Macquarie.

One metal for which Tesla is unlikely to see shortages or higher costs is aluminum. China, with 55 percent of the metal’s output last year, is the world’s biggest producer and consumer of it. (The U.S. and Canada accounted for about 8 percent of production.) Production in China has continued at a steady pace despite a global glut, partly to avoid massive job cuts and losses at banks that sank huge loans into smelting facilities. Goldman Sachs expects a surplus of about 1.25 million metric tons this year, while Harbor Intelligence, an aluminum researcher, predicts the glut will continue through 2021. If that’s the case, few will be happier than Musk.