apt-get install Command | Beginner’s Linux Tutorial

Digital download bar on a computer screen showing a package being installed symbolizing apt-get install

Understanding package management in Linux is foundational to effective software development, especially at IOFLOOD. One of the fundamental commands in this domain is ‘apt-get install,’ which enables the installation of software packages and dependencies. In today’s article we have gathered our tips and best practices of using ‘apt-get install’ in Linux, to equip our cloud server hosting customers and fellow developers with the knowledge to confidently manage software on their servers.

In this guide, we’ll walk you through the process of using apt-get install, from the basics to more advanced techniques. We’ll cover everything from installing simple software packages to dealing with more complex scenarios, as well as alternative approaches.

So, let’s dive in and start mastering apt-get install!

TL;DR: How Do I Use apt-get install in Linux?

The apt-get install command is a powerful tool in Linux used for installing software packages. To use it, you simply replace [package-name] with the name of the software package you want to install, like this: sudo apt-get install [package-name].

Here’s a simple example:

sudo apt-get install vim

# Output:
# Reading package lists... Done
# Building dependency tree
# Reading state information... Done
# vim is already the newest version (2:8.0.1453-1ubuntu1.4).
# 0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

In this example, we’re using the apt-get install command to install the text editor ‘vim’. The command sudo apt-get install vim tells the system to install the vim package. The output informs us that vim is already installed and is the newest version.

This is a basic way to use apt-get install in Linux, but there’s much more to learn about installing software packages efficiently. Continue reading for more detailed instructions and advanced usage scenarios.

Basic Use of apt-get install

To start with, let’s understand the basic use of the apt-get install command. This command is primarily used to install software packages on a Linux system. It’s a straightforward process that even beginners can master quickly.

Here is a simple example of how you can use the apt-get install command to install a software package. In this case, we will install the wget package, which is a free utility for non-interactive download of files from the web.

sudo apt-get install wget

# Output:
# Reading package lists... Done
# Building dependency tree
# Reading state information... Done
# The following additional packages will be installed:
#  libpsl5 publicsuffix
# Suggested packages:
#  wget-ssl
# The following NEW packages will be installed:
#  libpsl5 publicsuffix wget
# 0 upgraded, 3 newly installed, 0 to remove and 0 not upgraded.
# Need to get 700 kB of archives.
# After this operation, 2,410 kB of additional disk space will be used.
# Do you want to continue? [Y/n]

In this example, sudo apt-get install wget is the command that instructs the system to install the wget package. The output informs us about the package details, additional packages that will be installed, the space required, and finally, it asks for our confirmation to proceed with the installation.

This is a basic example of how you can use the apt-get install command. However, it has many more functionalities that we will explore in the following sections.

Advanced Usage of apt-get install

The apt-get install command is not only for installing the latest version of a software package. You can also use it to install a specific version of a package, or use different flags or options for more control over the installation process.

Installing Specific Versions

You can specify the version of a package you want to install using the = operator. This can be useful when a certain version of a package is required for compatibility reasons. Here’s an example where we install version 1.18.0-1 of the nginx package:

sudo apt-get install nginx=1.18.0-1

# Output:
# Reading package lists... Done
# Building dependency tree
# Reading state information... Done
# The following additional packages will be installed:
#  libnginx-mod-http-geoip libnginx-mod-http-image-filter libnginx-mod-http-xslt-filter libnginx-mod-mail libnginx-mod-stream
# Suggested packages:
#  fcgiwrap nginx-doc ssl-cert
# The following NEW packages will be installed:
#  libnginx-mod-http-geoip libnginx-mod-http-image-filter libnginx-mod-http-xslt-filter libnginx-mod-mail libnginx-mod-stream nginx
# 0 upgraded, 6 newly installed, 0 to remove and 0 not upgraded.
# Need to get 3,546 kB/3,669 kB of archives.
# After this operation, 12.3 MB of additional disk space will be used.
# Do you want to continue? [Y/n]

In this example, sudo apt-get install nginx=1.18.0-1 tells the system to install the specified version of nginx. The output informs us about the additional packages that will be installed along with nginx and the space required for the installation.

Using Different Flags or Options

You can use different flags or options with the apt-get install command for more control over the installation. For instance, the -y flag can be used to automatically answer yes to all prompts, which can be helpful in scripts or other automation scenarios. Here’s an example:

sudo apt-get install -y nginx

# Output:
# Reading package lists... Done
# Building dependency tree
# Reading state information... Done
# The following additional packages will be installed:
#  libnginx-mod-http-geoip libnginx-mod-http-image-filter libnginx-mod-http-xslt-filter libnginx-mod-mail libnginx-mod-stream
# Suggested packages:
#  fcgiwrap nginx-doc ssl-cert
# The following NEW packages will be installed:
#  libnginx-mod-http-geoip libnginx-mod-http-image-filter libnginx-mod-http-xslt-filter libnginx-mod-mail libnginx-mod-stream nginx
# 0 upgraded, 6 newly installed, 0 to remove and 0 not upgraded.
# Need to get 3,546 kB/3,669 kB of archives.
# After this operation, 12.3 MB of additional disk space will be used.

In this example, sudo apt-get install -y nginx automatically confirms the installation of nginx, as indicated by the absence of the ‘Do you want to continue?’ prompt in the output.

Other Linux Software Install Methods

While apt-get install is a powerful tool for managing software on Linux, it’s not the only method. Let’s explore some alternative approaches to software installation, such as using other package managers like yum or dnf, and compiling software from source.

Using YUM Package Manager

Yum (Yellowdog Updater Modified) is a package manager used primarily on Red Hat based systems like CentOS. Here’s how you would install a package, let’s say wget, using yum:

sudo yum install wget

# Output:
# Loaded plugins: fastestmirror, ovl
# Loading mirror speeds from cached hostfile
# Resolving Dependencies
# --> Running transaction check
# ---> Package wget.x86_64 0:1.14-18.el7_6.1 will be installed
# --> Finished Dependency Resolution
# Installed size: 547 k
# Downloading packages:
# Running transaction check
# Running transaction test
# Transaction test succeeded
# Running transaction
# Installing : wget-1.14-18.el7_6.1.x86_64
# Verifying  : wget-1.14-18.el7_6.1.x86_64
# Installed:
# wget.x86_64 0:1.14-18.el7_6.1

In the above example, sudo yum install wget instructs the system to install the wget package using yum. The output gives us information about the package dependencies, the transaction status, and the final installation of the package.

Using DNF Package Manager

Dnf (Dandified Yum) is the next-generation version of yum used in newer Fedora distributions. Here’s an example of installing wget using dnf:

sudo dnf install wget

# Output:
# Last metadata expiration check: 0:12:33 ago on Mon 20 Dec 2021 08:34:22 AM EST.
# Dependencies resolved.
# ================================================================================
#  Package       Architecture   Version               Repository          Size
# ================================================================================
# Installing:
#  wget          x86_64         1.21.1-3.fc34         updates            735 k
# Transaction Summary
# ================================================================================
# Install  1 Package
# Total download size: 735 k
# Installed size: 2.8 M
# Is this ok [y/N]: y
# Downloading Packages:
# wget-1.21.1-3.fc34.x86_64.rpm                    1.6 MB/s | 735 kB     00:00
# Running transaction check
# Transaction check succeeded.
# Running transaction test
# Transaction test succeeded.
# Running transaction
# Preparing        :                                                        1/1
# Installing       : wget-1.21.1-3.fc34.x86_64                              1/1
# Running scriptlet: wget-1.21.1-3.fc34.x86_64                              1/1
# Verifying        : wget-1.21.1-3.fc34.x86_64                              1/1
# Installed:
#  wget-1.21.1-3.fc34.x86_64

In this example, sudo dnf install wget instructs the system to install the wget package using dnf. The output gives us information about the package details, the transaction status, and the final installation of the package.

Compiling Software from Source

Another method to install software in Linux is by compiling it from source. This approach gives you the most control over the installation process, but it can be more complex and time-consuming. Here’s an example of how you might compile a program from a .tar.gz source file:

tar xzf sourcefile.tar.gz

cd sourcefile

./configure

make

sudo make install

# Output:
# [Expected output depends on the specific software being compiled]

In this example, we first extract the source files with tar xzf sourcefile.tar.gz. We then navigate into the source file directory with cd sourcefile. The ./configure command checks the system for the necessary components to compile the software. make compiles the software, and sudo make install installs it.

These are just a few of the alternative methods to install software in Linux. Depending on your specific needs and the distribution you’re using, one method may be more suitable than the others.

Troubleshooting Errors: apt-get install

As with any command, you may encounter some errors or obstacles when using apt-get install. Let’s discuss some common issues and their solutions.

Unmet Dependencies

One common problem is unmet dependencies. This happens when the package you’re trying to install depends on another package that isn’t installed or has a different version than required. Here’s what an unmet dependencies error might look like:

sudo apt-get install package-name

# Output:
# Some packages could not be installed. This may mean that you have
# requested an impossible situation or if you are using the unstable
# distribution that some required packages have not yet been created
# or been moved out of Incoming.
# The following information may help to resolve the situation:
#
# The following packages have unmet dependencies:
#  package-name : Depends: other-package but it is not going to be installed

You can solve this issue by installing the dependent package first, or you can use the -f option with apt-get install to fix broken dependencies:

sudo apt-get install -f

# Output:
# Reading package lists... Done
# Building dependency tree
# Reading state information... Done
# Correcting dependencies... Done
# The following packages were automatically installed and are no longer required:
#  libpackage-name
# Use 'sudo apt autoremove' to remove them.
# The following additional packages will be installed:
#  other-package
# The following NEW packages will be installed:
#  other-package
# 0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
# 1 not fully installed or removed.
# Need to get 0 B/123 kB of archives.
# After this operation, 345 kB of additional disk space will be used.
# Do you want to continue? [Y/n]

In this example, sudo apt-get install -f fixes the broken dependencies and allows the installation of the package-name to proceed.

Package Not Found

Another common issue is trying to install a package that doesn’t exist in the repositories your system is aware of. The error will look something like this:

sudo apt-get install non-existent-package

# Output:
# Reading package lists... Done
# Building dependency tree
# Reading state information... Done
# E: Unable to locate package non-existent-package

To solve this issue, you can search for the package in other repositories and add them to your system, or you could check for typos in the package name.

These are just a couple of the common issues you might encounter when using apt-get install. Remember, the Linux community is vast and helpful, so don’t hesitate to search for your error online or ask for help in Linux forums.

What is Linux Package Management?

To fully grasp the power of the apt-get install command, it’s important to understand the fundamentals of the Linux package management system and the concept of software repositories.

The Linux Package Management System

In Linux, a package management system is a collection of tools that automates the process of installing, upgrading, configuring, and removing software packages. This system keeps track of all installed software, ensuring that the software is working correctly and is up to date.

A key part of this system is the package manager, which is a program that handles packages. The apt-get command is part of the APT (Advanced Package Tool) package manager, which is used in Debian-based systems like Ubuntu.

Here’s an example of using apt-get to update the package list:

sudo apt-get update

# Output:
# Hit:1 http://archive.ubuntu.com/ubuntu bionic InRelease
# Get:2 http://archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 kB]
# Get:3 http://archive.ubuntu.com/ubuntu bionic-backports InRelease [74.6 kB]
# Fetched 163 kB in 2s (73.0 kB/s)
# Reading package lists... Done

In this example, sudo apt-get update updates the list of available packages and their versions, but it does not install or upgrade any packages. The output shows the repositories that were hit or fetched during the update.

Software Repositories and apt-get

Software repositories are servers that store and distribute software packages. Each Linux distribution has its own set of repositories, which are defined in the /etc/apt/sources.list file or in separate files in the /etc/apt/sources.list.d/ directory.

When you run an apt-get install command, apt-get looks for the specified package in the repositories listed in these files. It then downloads the package along with any dependencies and installs them on your system.

Here’s an example of checking the repositories in the sources.list file:

cat /etc/apt/sources.list

# Output:
# deb http://archive.ubuntu.com/ubuntu bionic main universe
# deb http://archive.ubuntu.com/ubuntu bionic-security main universe
# deb http://archive.ubuntu.com/ubuntu bionic-updates main universe

In this example, cat /etc/apt/sources.list displays the contents of the sources.list file. The output shows the repositories that apt-get will check when installing packages.

Understanding these fundamentals can help you make the most of the apt-get install command and the Linux package management system as a whole.

Practical Usage: apt-get install

The apt-get install command is a fundamental part of Linux system administration, but its utility goes beyond just individual package installations. It can be a crucial part of larger scripts or projects, and it often works in tandem with other commands for efficient system management.

Integrating apt-get install in Shell Scripts

Shell scripts are a powerful tool for automating tasks in Linux. You can include apt-get install commands in your scripts to ensure necessary packages are installed. Here’s an example of a simple script that checks if a package is installed and installs it if it’s not:

#!/bin/bash

PACKAGE='wget'

if dpkg --get-selections | grep -q "^$PACKAGE[[:space:]]*install$" >/dev/null; then
  echo "$PACKAGE is already installed"
else
  echo "$PACKAGE is not installed. Installing..."
  sudo apt-get install $PACKAGE
fi

# Output:
# 'wget is already installed' or 'wget is not installed. Installing...'

In this example, the script first checks if the wget package is installed. If it is, it prints a message saying so. If it’s not, it prints a message and then runs sudo apt-get install wget to install the package.

Common Companions to apt-get install

In typical use cases, apt-get install is often used with other related commands or functions. For instance, it’s common to update the package list with apt-get update before installing a new package. Similarly, you might use apt-get upgrade to upgrade all upgradable packages after installing a new package.

Further Resources for Mastering apt-get install

To dive deeper into the world of apt-get install and related commands, consider exploring the following resources:

  • Debian Handbook: A comprehensive guide to the Debian operating system, including detailed information about the APT package management system.

  • Ubuntu Server Guide: This guide provides a thorough overview of package management in Ubuntu, with a focus on server environments.

  • Linux Journey: A beginner-friendly resource that covers the basics of package management in Linux, including apt-get commands.

Recap: apt-get install Usage Guide

In this comprehensive guide, we’ve delved deep into the world of apt-get install, a fundamental command for managing software packages in Linux.

We began with the basics, understanding how to use apt-get install to install software packages in Linux. We then moved on to more advanced usage scenarios, exploring how to install specific versions of packages and how to use different flags for more control over the installation process.

We also discussed alternative approaches to software installation in Linux, such as using other package managers like yum or dnf, and even compiling software from source. Along the way, we tackled common challenges you might face when using apt-get install, such as unmet dependencies and package not found errors, and provided solutions to these issues.

Here’s a quick comparison of the methods we’ve discussed:

MethodProsCons
apt-get installRobust, supports many librariesMay require troubleshooting for some packages
yum/dnfUsed in Red Hat based systemsDifferent syntax and usage than apt-get
Compiling from sourceMost control over installationMore complex and time-consuming

Whether you’re a Linux beginner just starting out with apt-get install or an experienced user looking for a handy reference, we hope this guide has equipped you with the knowledge and skills to manage software packages efficiently in Linux.

The apt-get install command is a powerful tool in the world of Linux. Mastering it can help you maintain your system, install new software, and troubleshoot issues more effectively. Happy coding!