Register to get access to free programming courses with interactive exercises

Package Manager CLI fundamentals

The usual way of installing programs for Windows users is to wander the Internet in search of the right applications. In *nix systems, this method, although used, is much rarer. Package managers were invented to install programs, especially those related to development.

Some operating systems have a default package manager, others do not, and you have to install it yourself:

  • macOS - brew
  • Windows - Chocolatey

From here on out, all examples will use the apt package manager that comes with Ubuntu. For example, installing PHP in Ubuntu looks like this:

# It requires sudo because the installation goes in the system directories
sudo apt install php
# There will be a lot of text, and it will ask for your confirmation. If yes, it will install PHP

php -v

PHP 7.4.3 (cli) (built: Feb 20 2020 08:51:50) ( NTS )

The package manager consists of several parts. One of them communicates directly with the user. This is the console utility apt. To install the program you need, type apt install and then enter its name. Package managers almost always require a superuser (root) to run, because they install programs in system directories that are not writable to by a normal user (but this is not always the case, in macOS you do not need to use sudo).

apt install php

E: Could not open lock file /var/lib/dpkg/lock - open (13: Permission denied)
E: Unable to lock the administration directory (/var/lib/dpkg/), are you root?

Installed packages can be uninstalled. To do this, use the apt remove command:

apt remove php
# There's also a lot of output, followed by the question of whether or not you'd like to remove it

On the other end of the wire is a directory of what are called packages. When trying to install a program, the apt utility queries the directory and tries to see if there is a package with that name. If it's not there, you'll be told about it:

# wrongname package does not exist
sudo apt install wrongname

Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package wrongname

To save network resources and speed up operation, apt saves an index (a list of packages in the catalog) on the local disk and uses it for its work. Sometimes this can lead to situations where the package actually exists, but apt claims otherwise. If you sense things are not right, run a manual update of the local index:

sudo apt update
# Now you can install it
sudo apt install php

After reading the first part, you should have at least three questions:

  1. How do I know which package to install?

    When there is a need to install a certain program, the most obvious way to do this is to find the program's website and follow the steps indicated on the Download page. Typically, this page has instructions on how you can use the package manager, with examples of commands that include package names. Example: Node.js.

    Another way is to google articles, e.g., php install ubuntu. In articles like this, the installation always goes through the package manager. Over time you will remember the names of many packages and can install them almost automatically, figuratively speaking.

  2. How is a package different from a program?

    The program itself knows nothing about the existence of the package manager. But to be able to install it through the package manager, it must be specially "packaged". This can be done either by the author of the program or by enthusiasts. Ubuntu uses a special package format called deb. This format has detailed documentation on how to produce packaging.

  3. How do programs get into the catalog?

    Here it all depends very much on the policies of those who support him. Some directories are very hard to get into and programs are slow to update. This is due to the fact that the developers are trying to add only verified software there. In others, everything happens quickly and easily. In either case, you need to go through a certain procedure, after which the program will be added. This is one of the key aspects by which Linux distributions differ from one another. In any case, these directories are huge, containing tens and hundreds of thousands of programs and libraries.

Package managers are not the prerogative of operating systems; any ecosystem within which it is possible to distribute libraries or programs has its own package manager. All programming languages have batch managers, sometimes several, and most of them are included by default. In Node.js (server-side JavaScript), the package manager is npm, in PHP — Composer, in Ruby — RubyGems, in Python — pip.


Do it yourself

  • Install the tree program using the package manager and run it. It displays the list of files as a tree.

Sign up

Programming courses for beginners and experienced developers. Start training for free

  • 130 courses, 2000+ hours of theory
  • 1000 practical tasks in a browser
  • 360 000 students
By sending this form, you agree to our Personal Policy and Service Conditions

Our graduates work in companies:

Bookmate
Health Samurai
Dualboot
ABBYY