[Fixed] “bash: ng: command not found” Error

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

The “ng: command not found” error in Bash indicates that the Angular CLI is either not installed or inaccessible. To fix this error, install Angular CLI using:

sudo apt update && sudo apt install -y nodejs npm
sudo npm install -g @angular/cli

The “ng” is a powerful tool for developing Angular applications. It provides a set of commands and functionalities to scaffold, build, test, and deploy Angular applications. However, encountering “bash: ng: command not found” indicates that either the Angular command-line tool is not installed on your system or the PATH variable is not set for ng. Let’s see the reasons for this error in detail and explore the possible fixes.

Reasons for “ng: command not found” Error in Bash

Some common reasons for the “ng” command not found error are:

  • Angular CLI not Installed: If you are unable to use the “ng” command, it could be that your system does not have Angular CLI installed.
  • js and npm not Installed: Angular CLI depends on Node.js and npm. Make sure they are installed on your system.
  • Incorrect Path: The PATH on your system may not contain “ng”.
  • Inadequate Permissions: “ng” might not have the authorization it needs to function.
  • Binary Not Found: The Angular binary might be missing or corrupted.

4 Methods to Solve “bash: ng: command not found” Error

Follow the below methods to solve the ng command not found error in Bash:

1. Install Angular CLI

Most of the time, the absence of Angular CLI on your system causes the “bash: ng: command not found” error. In this instance, APT in Ubuntu, YUM in RedHat, and Pacman in Arch Linux can install it on your system. To do that, follow the steps:

  1. Press CTRL+ALT+T to open the terminal in Ubuntu.
  2. Update the repository using sudo apt update.
  3. Run the command to install npm (Node Package Manager) in Ubuntu:
    sudo apt install -y nodejs npm

    installing nodejs and npm using apt

  4. Now, run the below command to install Angular CLI globally:
    sudo npm install -g @angular/cli

    installing angular cli using npm

  5. Execute ng --version to verify the installation.executing ng --version

Now, the ng command is executed successfully.

2. Link “npm” to Angular CLI

To fix the “ng: command not found” error, you can create a link between npm and Angular CLI. Here’s how to do it:

  1. Go to the directory where your Angular CLI project is located.
  2. Run the following command to link the Angular CLI globally:
    npm link @angular/cli

    creating link from npm to angular cli

The above command establishes a symbolic link between the Angular CLI package and the npm global directory, allowing you to use the ng command globally from any terminal session.

3. Set “ng” PATH Variable

A prevalent reason behind the “ng command not found” is it is not present in your system PATH environment variables. To set the PATH variable for “ng”, follow the steps below:

  1. Press CTRL+ALT+T to open the terminal.
  2. Use which command to check the current path of ng:
    which ng

    executing which ng

  3. Open bashrc file using:
    nano ~/.bashrc
  4. Now, replace <path to ng> in export PATH=$PATH:<path to ng> code with the which command’s output and paste it at the end of the file. For example:
    export PATH=$PATH:/usr/local/bin/ng

    adding ng path variable to bashrc file

  5. Press CTRL+S to save, and CTRL+X to exit.
  6. Then, execute the .bashrc file using:
    source ~/.bashrc
  7. Now you can check running a ng command. For example:
    ng --version

    executing bashrc file and checking ng version

After adding the path, the ng command ran without raising any error.

4. Set an Alias to “ng”

To fix the “ng: command not found” error, you can create a temporary solution by setting up an alias for ng. Here’s how you can do it:

echo "alias ng='$(which ng)'" >> ~/.bashrc; . ~/.bashrc

This command appends an alias for the ng command to the ~/.bashrc file and immediately applies the changes in the current terminal session. The alias ensures that the ng command points to the correct path by using the $(which ng) syntax, which dynamically retrieves the actual path to the ng executable.

How to Solve “ng command not found” Error in MacOS?

To solve “ng: command not found” error in MacOS, follow the steps below:

  1. First, open your Mac terminal and run ng --version to check whether Angular is installed or not. If Angular CLI is installed, this command will display information about the installed Angular CLI version.

  2. If it’s not installed, run the below command to install it:
    npm install -g @angular/cli

    However, if you get the error even after installing Angular CLI, here’s how to solve it:

  3. Open the paths file using:
    sudo vim ~/etc/paths
  4. Enter the user password if prompted.
  5. Press Fn+i to enable insert mode.
  6. Paste the below line at the end:
    ~/.npm-global/bin
  7. Press ESC and type :wq to save the file.

Conclusion

In conclusion, resolving the “bash: ng: command not found” issue involves ensuring the installation of Angular CLI and configuring the system’s PATH appropriately. Applying these solutions allows you to seamlessly access ng commands within the Bash environment and streamline various aspects of Angular development which are an integral part of the Angular development workflow.

People Also Ask

What to do when ng command is not working?

Ensure Angular CLI is installed globally If not, install it using npm install -g @angular/cli. Then confirm the Angular CLI installation using ng --version. Also, you can run echo $PATH to ensure the npm global binary directory is in the PATH.

How to install ng for Angular?

To install Angular CLI (ng), open a terminal or command prompt and run sudo npm install -g @angular/cli. After that, use ng --version to confirm the installation by checking the Angular CLI version.

What is the Ng command?

The ng command is associated with the Angular CLI (Command Line Interface). It streamlines various tasks in Angular development, such as project creation, component generation, server start, building, testing, and more. It streamlines Angular development tasks by providing a set of commands to manage and scaffold Angular projects efficiently.

How do I use ng command locally?

To use the ng command locally in an Angular project, open a terminal and navigate to your Angular project’s root directory. Then, execute ng commands directly within your project, for example: ng generate component my-component. This runs the ng generate command locally for your Angular project.

What is the use of ng command?

The ng command is the Angular CLI (Command Line Interface) used for various tasks in Angular development, including:

  1. Create new Angular projects: ng new <project-name>
  2. Generate components: ng generate component <component-name>
  3. Generate modules: ng generate module <module-name>
  4. Generate services: ng generate service <service-name>
  5. Start a development server: ng serve
  6. Build the application for deployment: ng build
  7. Run unit tests: ng test
  8. Lint code for style and syntax: ng lint

How to Solve “sudo: ng: command not found” in Linux?

To solve “sudo: ng: command not found” in Linux, use sudo with full path. The syntax is:

sudo $(which ng) [command]

If you still find the error, follow the steps:

  1. Install nodejs and npm:
    sudo apt update && sudo apt install -y nodejs npm
  2. Install Angular CLI globally:
    sudo npm install -g @angular/cli
  3. Confirm npm global binary directory is in sudo’s secure_path.

Related Articles


<< Go Back to [Solved] “bash: command not found” Error | Bash Error Handling and Debugging | Bash Scripting Tutorial

Rate this post
Ashikur Rahman

Hello, I’m Ashikur Rahman, currently working as a Linux Content Developer Executive at SOFTEKO. I have completed my graduation in Computer Science and Engineering from Khulna University of Engineering & Technology (KUET). Also, I’m pursuing my master’s in the same department at Bangladesh University of Engineering & Technology (BUET). I like to learn new technologies, contribute, and share those with others. Here my goal is to provide beneficial and user-friendly articles on Linux distribution for everyone. Read Full Bio

Leave a Comment