Baeldung Pro – Linux – NPI EA (cat = Baeldung on Linux)
announcement - icon

Learn through the super-clean Baeldung Pro experience:

>> Membership and Baeldung Pro.

No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.

Partner – Orkes – NPI EA (tag=Kubernetes)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

1. Overview

When managing packages in a Debian-based system like Ubuntu, verifying the package version to be installed can be essential due to potential differences in features, bug fixes, or compatibility. Thankfully, Linux provides several ways to view the package version before proceeding.

In this tutorial, we’ll discuss possible approaches to check the package version before installing them in Linux using apt. This will involve:

Lastly, we’ll also discuss installing a specific package version using apt.

2. Using apt-cache

To start, the apt-cache command performs a variety of operations on APT’s package cache. Moreover, it enables us to search and generate useful information from the package metadata without making any changes to the system.

In particular, we can use it with the policy subcommand and the package name to retrieve its version details before installation. This is helpful in the scenarios when comparing the installed versions of a package with the version available in the repositories.

Let’s say, we want to check the version for the package golang. For this purpose, we’ll use this command:

$ apt-cache policy golang
golang:
  Installed: (none)
  Candidate: 2:1.18~0ubuntu2
  Version table:
     2:1.18~0ubuntu2 500
        500 http://archive.ubuntu.com/ubuntu jammy/main amd64 Packages

In the output, we can see that the package golang is not installed, and the available candidate version is 2:1.18~0ubuntu2.

Here’s the explanation of version number:

  • The 2: is the epoch number
  • The 1.18 is the upstream version of the package that developers released
  • Finally, the ~0ubuntu2 represents the revision number specific to the Ubuntu distribution

2.1. With showpkg Subcommand

Alternatively, instead of policy, we can also use the showpkg subcommand to get detailed package information, including versions, dependencies, and reverse dependencies. This is useful when we’re troubleshooting or investigating a particular version for selection.

So, let’s try obtaining the golang version details using the showpkg subcommand this time:

$ apt-cache showpkg golang
Package: golang
Versions: 
2:1.18~0ubuntu2 (/var/lib/apt/lists/archive.ubuntu.com_ubuntu_dists_jammy_main_binary-amd64_Packages)
 Description Language: 
                 File: /var/lib/apt/lists/archive.ubuntu.com_ubuntu_dists_jammy_main_binary-amd64_Packages
                  MD5: 8650a23f44e2d60a785ad3123733d6d8
 Description Language: en
                 File: /var/lib/apt/lists/archive.ubuntu.com_ubuntu_dists_jammy_main_i18n_Translation-en
                  MD5: 8650a23f44e2d60a785ad3123733d6d8
Reverse Depends: 
  laby,golang
  laby,golang
Dependencies: 
2:1.18~0ubuntu2 - golang-1.18 (0 (null)) golang-doc (2 2:1.18~0ubuntu2) golang-go (2 2:1.18~0ubuntu2) golang-src (2 2:1.18~0ubuntu2) 
Provides: 
2:1.18~0ubuntu2 - 
Reverse Provides: 

This output shows that the golang package is available in version 2:1.18~0ubuntu2, along with its dependencies and reverse dependencies (packages that require golang to function).

3. Using apt With –just-print

Also, we can simply use the apt command itself to check the package version before installation via the –just-print option. This option performs a simulation of events that would occur based on the current system state without actually making any changes to the system.

For instance, the following command will simulate the installation of the package nodejs without actually installing it. The grep command filters the output to display lines related to the installation package:

$ apt install --just-print nodejs | grep 'Inst'
WARNING: apt does not have a stable CLI interface. Use with caution in scripts.
Inst libjs-highlight.js (9.18.5+dfsg1-1 Ubuntu:22.04/jammy [all])
Inst libc-ares2 (1.18.1-1ubuntu0.22.04.3 Ubuntu:22.04/jammy-updates, Ubuntu:22.04/jammy-security [amd64])
Inst libnode72 (12.22.9~dfsg-1ubuntu3.6 Ubuntu:22.04/jammy-updates, Ubuntu:22.04/jammy-security [amd64])
Inst nodejs-doc (12.22.9~dfsg-1ubuntu3.6 Ubuntu:22.04/jammy-updates, Ubuntu:22.04/jammy-security [all])
Inst nodejs (12.22.9~dfsg-1ubuntu3.6 Ubuntu:22.04/jammy-updates, Ubuntu:22.04/jammy-security [amd64])

In the above output, we can see the line starting from Inst nodejs, which shows that version 12.22.9 would be installed.

4. Using apt show

At a high level, apt provides a command line interface for the package management system.

When used with the show subcommand, it displays the given package information including its version, dependencies, installation, and download size. Therefore, we can use the apt show command with the package or tool name to display its version details before installation.

Let’s go ahead and use the apt show command to retrieve the version information for the nginx server in the shell:

$ apt show nginx
Package: nginx
Version: 1.18.0-6ubuntu14.4
Priority: optional
Section: web
Origin: Ubuntu
Maintainer: Ubuntu Developers <[email protected]>
Original-Maintainer: Debian Nginx Maintainers <[email protected]>
Bugs: https://bugs.launchpad.net/ubuntu/+filebug
Installed-Size: 50.2 kB
Depends: nginx-core (<< 1.18.0-6ubuntu14.4.1~) | nginx-full (<< 1.18.0-6ubuntu14.4.1~) | nginx-light (<< 1.18.0-6ubuntu14.4.1~) | nginx-extras (<< 1.18.0-6ubuntu14.4.1~), nginx-core (>= 1.18.0-6ubuntu14.4) | nginx-full (>= 1.18.0-6ubuntu14.4) | nginx-light (>= 1.18.0-6ubuntu14.4) | nginx-extras (>= 1.18.0-6ubuntu14.4)
Breaks: libnginx-mod-http-lua (<< 1.18.0-6ubuntu5)
Homepage: https://nginx.net
Download-Size: 3,872 B

As expected, we can see various details about the nginx package, including its version, 1.18.0.

5. Using apt list

The apt list command provides a quick overview of the available package versions, showing both installed and candidate versions in a straightforward list format. In particular, we can use the apt list command and specify the package name to get its version details.

For example, to know about the available version of the ruby package, we can use this command:

$ apt list ruby
Listing... Done
ruby/jammy 1:3.0~exp1 amd64
ruby/jammy 1:3.0~exp1 i386

In return, we can see the available ruby package version, which is 1:3.0~exp1. Here, 1: is the epoch, while 3.0 is the actual version number of ruby.

6. Using apt-show-versions

The apt-show-versions command provides detailed package information on the installed version, all available versions in the repository, and potential upgrade options within the specific distribution. Specifically, we can use the apt-show-versions command with the -a option and specify the package name to list all available versions.

Additionally, if apt-show-versions is not available in the system, we can use the sudo apt install apt-show-versions command to install it.

Let’s try running this command in a shell to list all available versions for the package mysql-server:

$ apt-show-versions -a mysql-server
mysql-server:all 8.0.28-0ubuntu4         jammy          archive.ubuntu.com
mysql-server:all 8.0.39-0ubuntu0.22.04.1 jammy-security archive.ubuntu.com
mysql-server:all 8.0.39-0ubuntu0.22.04.1 jammy-updates  archive.ubuntu.com
mysql-server:all not installed

In this output, we can see all available versions of mysql-server from different repositories (jammy, jammy-security, and jammy-updates). It also indicates that it’s not currently installed.

7. Installing a Specific Package Version Using apt

After checking the available version of the package, we can restrict apt to install that particular version only, even if a newer version is available in the repositories. To achieve this, we can specify the package name along with the version number in the installation command.

7.1. Syntax

First, let’s take a look at the syntax of apt to install a specific version of a package:

$ sudo apt install <package_name>=<version_number>

In this syntax, we can replace the <package_name> and <version_number> with the package name and exact version number.

7.2. Installing mysql-server

For example, to install the version 8.0.28-0ubuntu4 of the package mysql-server, we’ll execute this:

$ sudo apt install mysql-server=8.0.28-0ubuntu4
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following NEW packages will be installed:
  mysql-server
0 upgraded, 1 newly installed, 0 to remove and 3 not upgraded.
Need to get 0 B/9,526 B of archives.
After this operation, 111 kB of additional disk space will be used.
Selecting previously unselected package mysql-server.
dpkg: warning: files list file for package 'linux-hwe-6.2-tools-6.2.0-37' missin
g; assuming package has no files currently installed
(Reading database ... 197721 files and directories currently installed.)
Preparing to unpack .../mysql-server_8.0.28-0ubuntu4_all.deb ...
Unpacking mysql-server (8.0.28-0ubuntu4) ...
Setting up mysql-server (8.0.28-0ubuntu4) ...

In this output, we can see that mysql-server version 8.0.28-0ubuntu4 is installed and ready for use.

8. Conclusion

In this article, we learned various methods to check the package version before installing it on Linux systems using apt. We discussed the apt-cache with policy and showpkg subcommand, apt with –just-print option, apt show, apt list, and apt-show-versions commands.

Additionally, we also covered installing a specific package version using apt.