SolarCity, the second-largest U.S. solar company by market value, posted a first-quarter loss as it bore higher costs from installing rooftop solar systems at little or no charge to customers.

The loss was $31 million, or 41 cents a share, compared with income of $2.76 million, or 4 cents, a year earlier, San Mateo-based SolarCity said Monday in a statement. That was 9 cents more than the average of six analysts' estimates compiled by Bloomberg.

Sales rose 21 percent to $30 million. This is the company's second earnings report since its December initial public offering.

SolarCity invested $138.2 million to install solar systems, up 65 percent, as demand for rooftop panels from homeowners and businesses surged. The company is boosting spending now to build projects that will deliver long-term revenue.

"Revenue is climbing but their cash outflows for financing new systems was up significantly more," Pavel Molchanov, an analyst at Raymond James & Associates in Houston, said. He has the equivalent of a hold rating on the shares. "The stock is outperforming the results."

SolarCity installs power systems, often at no upfront cost for customers, who sign decades-long contracts to purchase the power. It said in January that installations will climb 60 percent to 250 megawatts this year, from 156 megawatts in 2012.

The company installed 46 megawatts of panels in the quarter, and a cumulative total of 333 megawatts. For the second quarter, SolarCity expects to install 48 megawatts to 53 megawatts of solar systems, up from 46 megawatts in the first quarter.

Customers include Wal-Mart Stores, which said in March that SolarCity had installed 4.7 megawatts of panels atop 12 stores in Ohio.

Shares SolarCity fell 6.6 percent to $33.50 in after-hours trading Monday. This was after shares jumped a record 24 percent in regular trading Monday.