NPX is a package manager similar to NPM, the only difference being that it allows you to execute packages without installing them first.

When using NPX to execute a package, it will first search for it locally or globally; if the required package is not found, it will download it from the NPX registry.

Now, the big question is: how can you install it on your Ubuntu system? The answer is pretty simple, as it comes bundled with NPM. So, if you have NPM installed on your system, you can use it without any additional steps.

How to Install NPX on Ubuntu

To install NPX on Ubuntu, all you need to do is install NPM, which already comes bundled with NPX package, and you can do so by running the following command:

  • sudo apt install npm

Once the installation is complete, you can run the following command to check the version of NPX:

  • npx -v

Output:

checking the NPX version

How to Use NPX on Ubuntu

Once NPX is installed, you can begin using it to execute packages. To help kickstart your journey, I'll share some examples of using NPX while working with Node.js projects.

1. Running a Locally Installed Package

To demonstrate how to run a package using NPX, I will use the popular package known as http-server, which is used to start an HTTP server. So, to use it, we need to install it using NPM.

  • npm i http-server

Output:

installing npm package

Once done, you can use the NPX command to run the http-server:

  • npx http-server

Output:

executing package without installing using npx

2. Running a Package Without Local Installation

In the previous example, we first installed the package and then attempted to run it using NPX. However, you can run packages without installing them first with NPX by simply appending the package name to the NPX command.

For example, I'll attempt to run the cowsay package without installing it first, but before that, I'll remove the existing cowsay installed package.

  • npm rm --silent cowsay
  • npm ls

Output:

removing and listing the installed npm package

Now, by using the NPX command, I will invoke the cowsay utility to print the message Ubuntu Shell.

  • npx cowsay "Ubuntu Shell"

Output:

running package without installing using npx

3. Create a New React App Using NPX

Creating a new React app without first installing it using NPX shows how powerful and beneficial this tool could be. For example, I'll use the create-react-app package with the NPX command to create a new React app.

  • npx create-react-app my-app

Output:

creating react app using npx

4. Useful Options for NPX

To get more out of NPX's and prepare for various situations, familiarize yourself with the available options that could come in handy in specific scenarios.

OptionDescription
--no-installIt will prompt an error if the reference package is not installed locally.
--ignore-existingDownload the referenced package even if it already exists.
-p <package-1> -p <package-2>Running multiple packages with NPX.
-cPipe the NPM environment variables into the NPX command.
--quietRun the package by surprising any output of NPX itself.

Wrap Up

I hope you enjoyed the article and that it was able to fulfill all the important information that a beginner should know before using NPX.