How to install a specific version of Angular with Angular CLI?

327,336

Solution 1

You can just have package.json with specific version and do npm install and it will install that version.

Also you don't need to depend on angular-cli to develop your project.

Solution 2

To answer your question, let's assume that you are interested in a specific angular version and NOT in a specific angular-cli version (angular-cli is just a tool after all).

A reasonnable move is to keep your angular-cli version alligned with your angular version, otherwise you risk to stumble into incompatibilities issues. So getting the correct angular-cli version will lead you to getting the desired angular version.

From that assumption, your question is not about angular-cli, but about npm.

Here is the way to go:

[STEP 0 - OPTIONAL] If you're not sure of the angular-cli version installed in your environment, uninstall it.

npm uninstall -g @angular/cli

Then, run (--force flag might be required)

npm cache clean

or, if you're using npm > 5.

npm cache verify

[STEP 1] Install an angular-cli specific version

npm install -g @angular/[email protected]

[STEP 2] Create a project

ng new you-app-name

The resulting white app will be created in the desired angular version.

NOTE: I have not found any page displaying the compatibility matrix of angular and angular-cli. So I guess the only way to know what angular-cli version should be installed is to try various versions, create a new project and checkout the package.json to see which angular version is used.

angular versions changelog Here is the changelog from github reposition, where you can check available versions and the differences.

Hope it helps

Solution 3

Edit #2 ( 7/2/2017)

If you install the angular cli right now, you'd probably have the new name of angular cli which is @angular/cli, so you need to uninstall it using

npm uninstall -g @angular/cli

and follow the code above. I'm still getting upvotes for this so I updated my answer for those who want to use the older version for some reasons.


Edit #1

If you really want to create a new project with previous version of Angular using the cli, try to downgrade the angular-cli before the final release. Something like:

npm uninstall -g angular-cli
npm cache clean
npm install -g [email protected]

Initial

You can change the version of the angular in the package.json . I'm guessing you want to use older version of angular but I suggest you use the latest version. Using:

ng new app-name

will always use the latest version of angular.

Solution 4

The angular/cli versions and their installed angular/compiler versions:

  • 1.0 - 1.4.x = ^4.0.0
  • 1.5.x = ^5.0.0
  • 1.6.x - 1.7.x = ^5.2.0
  • 6.x = ^6.0.0
  • 7.x = ^7.0.0

Can be confirmed by reviewing the angular/cli's package.json file in the repository newer repository master repository. One would have to install the specific cli version to get the specific angular version:

npm -g install @angular/[email protected].* # For ^5.0.0

Solution 5

Yes, it's possible to install a specific version of Angular using npm:

npm install -g @angular/[email protected]

Next, you need to use the ng new command to create an Angular project based on the specific version you used when installing the CLI:

ng new your-project-name

This will generate a project based on Angular v8.3.19, the version which was specified when installing Angular CLI.

Share:
327,336
Sajad
Author by

Sajad

Updated on February 17, 2022

Comments

  • Sajad
    Sajad about 2 years

    I searched through google and angular cli doc but couldn't find any way to install a specific version of Angular using Angular CLI. is it even possible?

  • Sajad
    Sajad about 7 years
    I'm using a BaaS provider which still does not support ng4.
  • brijmcq
    brijmcq about 7 years
    @sajad that's unfortunate. I think you can create a previous version of angular from cli. I'll edit my answer
  • Ben Racicot
    Ben Racicot almost 7 years
    But you should be depending on the CLI IMO. BTW no longer angular-cli but angular/cli now. Also, it's npm install @angular/{common,compiler,compiler-cli,core,forms,http,platf‌​orm-browser,platform‌​-browser-dynamic,pla‌​tform-server,router,‌​animations}@latest
  • Jason Simpson
    Jason Simpson almost 7 years
    @brijmq I have personally never seen angular-cli "always use the latest version of angular". For example, right now if you update the cli (1.1.3) globally and create a new project with it, you will get angular 4.0.0 even though the latest is 4.2.4. So what I've been trying to figure out for ages is does the version get updated periodically with new cli releases?
  • brijmcq
    brijmcq almost 7 years
    @epiphanatic my bad and sorry for the confusion.the cli will use the latest version of angular at that time when the cli update was made. You will need to manually update it on your package.json on your app if you want to update it. The release of updates in angular is much faster than angular-cli. Take a look at this link to know more angularjs.blogspot.com/2016/10/… . Hope it clears your confusion
  • Sajad
    Sajad about 6 years
    Does not answers the question.
  • MPPNBD
    MPPNBD almost 6 years
    While following your steps, during npm install -g @[email protected] getting error like this npm ERR! Invalid package name "@angular-cli": name can only contain URL-friendly characters
  • Esther Lalremruati
    Esther Lalremruati almost 6 years
    @MPPNBD it should be npm install -g @angular/[email protected]
  • MPPNBD
    MPPNBD almost 6 years
    @avi.elkharrat , Oh sorry for that,i missed that. npm install -g @angular/[email protected] worked for me. Thanks a lot avi.elkharrat
  • Kaloyan Stamatov
    Kaloyan Stamatov almost 6 years
    Just to add that npm cache clean did not work for me and I had to use npm cache clean --force
  • JabberwockyDecompiler
    JabberwockyDecompiler almost 6 years
    @brijmcq The install is also new, it needs to be in the format like npm install -g @angular/[email protected] also noted by the answer by avi.elkharrat.
  • Yagnesh bhalala
    Yagnesh bhalala almost 6 years
    As you said edit package.json file, But where is located this package.json file?
  • Mario Petrovic
    Mario Petrovic almost 6 years
    You add package.json automatically by doing npm init or manually creating it in your root folder of the project
  • Paramvir Singh Karwal
    Paramvir Singh Karwal about 5 years
    I suppose npm cache clean is not required because it gives you npm ERR! As of npm@5, the npm cache self-heals from corruption issues and data extracted from the cache is guaranteed to be valid. If you want to make sure everything is consistent, use 'npm cache verify' instead. On the other hand, if you're debugging an issue with the installer, you can use npm install --cache /tmp/empty-cache to use a temporary cache instead of nuking the actual one.
  • Sajad
    Sajad over 4 years
    Thanks, but i think you answere is irrelevant.
  • slucas
    slucas almost 4 years
    The package angular-cli has been renamed @angular/cli so you need to do something like npm install -g @angular/[email protected]
  • Crusader
    Crusader over 3 years
    using angular cli
  • Shayma Pathan
    Shayma Pathan about 3 years
    After changing version in package.json, make sure to perform npm install.
  • Andrew Koper
    Andrew Koper about 3 years
    I have Angular 11 installed globally on my computer, but I needed to create a new project in 6. Based on above info, I did this in three steps: 1) created a new dir 2) npm install @angular/cli@6.* 3) ng new [angular-six-project-name]
  • ni8mr
    ni8mr about 3 years
    Thanks for the detailed clarification.
  • ankit singh
    ankit singh almost 3 years
    npm uninstall -g @angular/cli -> after I run this cmd, I found that angular is still there.
  • avi.elkharrat
    avi.elkharrat almost 3 years
    I did this on a windows desktop. No sudo.
  • Eliezer Berlin
    Eliezer Berlin almost 3 years
    This works great, but after doing this, it's a good idea to delete the extra outer package-lock.json and node_modules folder. (The extra ones which get created outside the angular project.)
  • Jonathan
    Jonathan over 2 years
    This answer should have much more votes. Worse to mention that you might get an error: npm version 7.20.5 detected. The Angular CLI currently requires npm version 6.. That can be fixed with npm install --global npm@6
  • seedme
    seedme over 2 years
    This plus @EliezerBerlin remind = logical and works great.