Exhibit 5.1

September 29, 2017

CarGurus, Inc.

2 Canal Park, 4th Floor

Cambridge, Massachusetts 02141

Re: CarGurus, Inc., Registration Statement on Form S-1 (File No. 333-220495)

Ladies and Gentlemen:

We have acted as counsel to CarGurus, Inc., a Delaware corporation (the “Company”), in connection with the filing of the Registration Statement on Form S-1 referenced above (as amended prior to being declared effective, the “Registration Statement”) under the Securities Act of 1933, as amended (the “Act”), with the Securities and Exchange Commission (the “Commission”). The Registration Statement relates to the proposed offering and sale of up to 10,810,000 shares of Class A common stock, par value $0.001 per share, of the Company (the “Common Stock”), which includes up to 3,205,000 shares to be issued and sold by the Company (including 705,000 shares subject to the underwriters’ over-allotment option described in the Registration Statement) (the “Primary Shares”), and up to 7,605,000 shares to be offered and sold by certain selling stockholders named in the Registration Statement (the “Selling Stockholders”) (including 705,000 shares subject to the underwriters’ over-allotment option described in the Registration Statement) (the “Secondary Shares” and, together with the Primary Shares, the “Shares”). The number of Shares shall include all shares of Common Stock registered in connection with the offering contemplated by the Registration Statement, including any additional shares of Common Stock registered by the Company pursuant to Rule 462(b) under the Act.

In connection with this opinion letter, we have examined the Registration Statement and originals, or copies certified or otherwise identified to our satisfaction, of the Company’s Fourth Amended and Restated Certificate of Incorporation and Amended and Restated Bylaws and such other documents, records, and instruments as we have deemed necessary or appropriate for purposes of the opinion set forth herein.

We have assumed the genuineness of all signatures, the legal capacity of all natural persons, the authenticity of the documents submitted to us as originals, the conformity with the originals of all documents submitted to us as certified, facsimile, or photostatic copies and the authenticity of the originals of all documents submitted to us as copies.

Based upon the foregoing, and subject to the qualifications, assumptions and limitations stated herein, we are of the opinion that:

1. The Primary Shares have been duly authorized and, when issued and sold by the Company and delivered against receipt of the purchase price therefor, at a price not less than the par value of the Common Stock and not less than a price per share at which the total number of Shares would exceed the total number of shares of Common Stock available under the Company’s Fourth Amended and Restated Certificate of Incorporation, in the manner contemplated by the Registration Statement, the Primary Shares will be validly issued, fully paid and non-assessable.

2. The Secondary Shares have been duly authorized and validly issued and are fully paid and non-assessable.

The opinions expressed herein are limited to the Delaware General Corporation Law.

We hereby consent to the use of this opinion as Exhibit 5.1 to the Registration Statement and any post-effective amendment to the Registration Statement, and to the reference to us under the caption “Legal Matters” in the prospectus included in the Registration Statement. In giving such consent, we do not hereby admit that we are acting within the category of persons whose consent is required under Section 7 of the Act or the rules or regulations of the Commission thereunder.

Very truly yours,

/s/ Morgan, Lewis & Bockius LLP

2