How to install and use the Angular CLI?
Tags: angular, typescript, javascript
Published: 2021-04-20 17:34:44

## What is the [Angular CLI](https://github.com/angular/angular-cli/)?
#### Here's the step-by-step guide to install the Angular CLI and build your first Angular application

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/de6bsxihbv9xnhzegacl.png)

The Angular CLI. 

Just exactly what is it? And what is its purpose?

Maybe a better question is _what can you do with the Angular CLI?_

This Angular CLI guide will teach you everything you need to know about the Angular CLI. We're going to cover how to install the Angular CLI, the different Angular CLI versions, how to use the Angular CLI to create components and the whole bang. 

Yes buddy! I'm going to show you how to grab the Angular CLI by the horns and make it obey your orders. 🥳 

## Table of Contents
* [What is the Angular CLI?](#what-is-the-angular-cli)
* [Do I have to use the Angular CLI?](#use-angular-cli)
* [Angular CLI versions](#angular-cli-versions)
* [How to install the Angular CLI](#install-angular-cli)
* [How to use the Angular CLI](#use-angular-cli)
* [How to create an Angular application with the CLI](#create-angular-application)
* [How to start the Angular application with the CLI](#start-angular-application)
* [How to create modules with the Angular CLI](#angular-cli-modules)
* [How to create components with the Angular CLI](#angular-cli-components)
* [How to create custom pipes with the Angular CLI](#angular-cli-pipes)
* [How to create a service with the Angular CLI](#angular-cli-services)
* [Cool Angular CLI commands worth remembering](#commands-to-memorize)
* [Conclusion](#conclusion)

<a id="what-is-the-angular-cli"></a>
## What is the Angular CLI?

The Angular CLI is a tool that was created by the Angular team to manage, build, maintain and test your Angular projects. 

You can think of it as the swiss-army knife for the Angular framework. 

The Angular CLI is a command-line tool, and maybe that makes you grumble because you want something that's "_easy to use_". 😋 

Actually, there is a [Visual Studio Code extension](https://marketplace.visualstudio.com/items?itemName=sasxa-net.angular-gui#:~:text=GUI%20for%20Angular%20helps%20you%20manage%20your%20Angular,allowing%20you%20to%20excute%20CLI%20commands%20from%20browser.) available to download and install. It adds a GUI interface that you can then use instead of the terminal. 

However, this guide focuses specifically on using the Angular CLI via the command line because that is the best way to use this powerful tool. 

<a id="use-angular-cli"></a>
## Do I have to use the Angular CLI?

And now that we know what the Angular CLI is maybe you're wondering if you have to use it to be an Angular developer?

The answer is actually _no, you don't **have to** use the Angular CLI_. But you'd be a... um... foolish Angular developer if you didn't use it. 🙀

Why?

Because the Angular CLI was created to turn **you** into a productive Angular-cranking-code-machine and automate all the time-consuming tasks. It generates starter Angular applications, complete with a `.gitignore` file. And generates all the core pieces of your application like skeleton modules, components, etc... as well as handling the testing, building and other common operations. 

Yes, buddy! You had better use the Angular CLI even if you don't necessarily have to.

<a id="angular-cli-versions"></a>
## Angular CLI versions

The first beta version of the Angular CLI was released in 2017. And since more then 450 versions have showed up.

A new Angular CLI version is released with every new release of the Angular framework. So if you're using Angular 12, then there will be an Angular CLI 12 as well. This doesn't mean that Angular CLI version has to be the same as your Angular project version. More times they're not. You can use Angular CLI version that's different from your Angular project version.

<a id="install-angular-cli"></a>
## How to install the Angular CLI

Before installing the Angular CLI, you'll need to install [Node.js](https://node.js.org).

If you're using a Windows or Mac [download it here](https://nodejs.org/en/download/). And for the amazing Linux guys, [here's the best way to install Node.js](https://github.com/nodesource/distributions).

And now that you've got Node.js installed, open a command prompt or terminal and type this command.

```bash
npm install -g @angular/cli
```

And that is how you globally install the Angular CLI.

Of course you'll need to update the Angular CLI to the late version. How do you do that?

```bash
npm update -g @angular/cli
```

We've installed the Angular CLI globally, meaning it's installed as a tool that you can use anywhere on your machine. Interestingly, when we create a new Angular application the CLI will also be installed locally, meaning that the Angular CLI is installed and available in the _node_modules/_ folder of the Angular application.

<a id="use-angular-cli"></a>
## How to use the Angular CLI

So now that we've installed the Angular CLI, how do you use it?

Open a terminal or command prompt and run this command to see the basic options and available commands for the Angular CLI.

```bash
ng help
```
For more details about a specific command you can type in the command name followed by `--help`.  For example...

```bash
ng new --help
```
...will give you all the command flags, details and options for creating a new Angular application.

<a id="create-angular-application"></a>
## How to create an Angular application with the CLI

Creating a bare-bones Angular application from scratch, generate all the files, install all the dependencies, etc... would take a loooong-wong time. Probably days.

With the Angular CLI, it's one simple-pimple command. Enough to make you swallow your gum.

```bash
ng new MyApplicationName
```

This will create all the needed files and download all the dependencies for us, while we kick back in our chair and sip some coffee. 😎

Yes buddy, ya better be using the Angular CLI. 😃

<a id="start-angular-application"></a>
## How to start the Angular application with the CLI

Now that we've created our Angular CLI, how do we start it?

The first step is to enter the new directory via the command line. In my case this is the command I need.

```bash
cd MyApplicationName
```
And now that I've entered the directory where my application is stored, here's how I start my Angular application.

```bash
ng serve
```

This command will compile my Angular application, setup live-reloading and launch it in a browser.

Yes buddy, ya better be using the Angular CLI. 😃

<a id="angular-cli-modules"></a>
## How to create modules with the Angular CLI

Angular modules are used to modularize your Angular application. In otherwords, Angular modules are used to divide your Angular application into specified pieces. You can read more about Angular modules [here](https://angular.io/guide/architecture-modules).

Here's the Angular CLI command to create a basic Angular module.

```bash
ng generate module MyModuleName
```
Depending on the case, you might want more then just a basic Angular module. Maybe you want an Angular module with its own routing module? Then here's how you make the Angular CLI do it for you.

```bash
ng generate module MyModuleName --routing
```

Yes buddy, ya better be using the Angular CLI. 😃

<a id="angular-cli-components"></a>
## How to create components with the Angular CLI

Here's the Angular CLI command to create a new Angular component.

```bash
ng generate component MyComponentName
```

And if for some strange reason you need to specify the module for this specific component, then here's how to do it.

```bash
ng generate component MyComponentName --module MyModuleName
```

Yes buddy, ya better be using the Angular CLI. 😃

<a id="angular-cli-pipes"></a>
## How to create custom pipes with the Angular CLI

Here's the Angular CLI command to create an Angular pipe.

```bash
ng generate pipe MyPipeName
```

Yes buddy, ya better be using the Angular CLI. 😃

<a id="angular-cli-services"></a>
## How to create a services with the Angular CLI

Here's the Angular CLI command to create an Angular service.

```bash
ng generate service MyServiceName
```

Yes buddy, ya better be using the Angular CLI. 😃

<a id="commands-to-memorize"></a>
## Cool Angular CLI commands worth memorizing

Want to turn your Angular app into a PWA (progressive web app)? Here's how to do it.

```bash
ng add @angular/pwa
```

Have some question about an Angular feature? Maybe a question about Angular pipes? Then use this command. 

```bash
ng docs pipe
```

This will query the Angular docs for whatever keyword you choose. And the best thing is that it reads your local Angular version and does the documentation lookup for that specific version of Angular. If you're using Angular 8, it will check the docs for Angular 8 even if the latest Angular release is version 12.

Here's how to check your Angular version. This will display local (if exists) and global versions.

```bash
ng --version
```

To generate a [route guard](https://angular.io/guide/router#preventing-unauthorized-access) that you can use to protect your routes.

```bash
ng generate guard your-guard
``` 

Yes buddy, ya better be using the Angular CLI. 😃

<a id="conclusion"></a>
## Conclusion

In this article we've covered what the Angular CLI is, what it is useful for and how to use it.

The Angular CLI is the most important tool as an Angular developer. Without it, building Angular apps would be like chewing on glass. 

Yup! Buddy! Ya better be using the Angular CLI. 😃

Questions? Comments? Don't hesitate to [reach out](https://danielk.tech/contact).

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l1q9wvja7jyeeom8b40m.png)