TL;DR
To install a specific version of a package using apt, you can try the following steps.
- Determine the package name and desired version.
- Open a Terminal window.
- Check the available versions of the package using
apt-cache policy package_name
. - Create a pinning configuration file using
sudo nano /etc/apt/preferences
. - Add package-specific lines to the pinning configuration file.
- Update the package cache with
sudo apt update
and install the desired version usingsudo apt install package_name
.
When installing a specific package version using apt, you may encounter common errors such as “Package not found,” “Version not found,” dependency conflicts, pinning priority conflicts, and incomplete package installation. To overcome these errors, double-check package names, verify repository availability, update conflicting packages, adjust pinning priorities, and ensure a stable internet connection.
Read the step-by-step guide below to install the specific version of a package using apt and explore some common errors with possible solutions.
Installing a specific version of a package using apt is a crucial skill for any Linux user or system administrator. It empowers you to control the software environment on your system precisely, ensuring compatibility, stability, and meeting specific requirements. Whether you need to revert to a previous version, ensure a specific feature set, or maintain a consistent development environment, apt provides the tools and flexibility to accomplish these tasks. However, the process is not without its challenges. In this article, I will delve into a step-by-step guide on how to install a specific version of a package using apt, while also addressing common errors that may arise along the way.
How to Install a Specific Version of a Package Using apt
To install a specific version of a package using apt, you’ll need a compatible Linux distribution, meeting minimum hardware requirements, and administrative privileges (root or sudo access). Refer to package documentation for any additional prerequisites.
- Determine the name of the package you want to install and the specific version you wish to target. Ensure that the version you intend to install is available in the repositories. In this case, I will install specific versions of the nginx package.
- Open a Terminal window.

- Use the
apt-cache
command to check the available versions of the package:
<strong>apt-cache policy package_name</strong>
Replace package_name with the name of the package you want to install.

- If you don’t already have a pinning configuration file, create one using a text editor:
<strong>sudo nano /etc/apt/preferences</strong>
- The command will create a pinning configuration file.

- In the pinning configuration file, add the following lines:
<strong>Package: package_name</strong>
<strong>Pin: version desired_version</strong>
<strong>Pin-Priority: 1001</strong>
Replace package_name with the name of the package and desired_version with the specific version you want to install. The Pin-Priority value of 1001 ensures the specified version takes precedence over others.

- Save the changes and close the pinning configuration file by pressing Ctrl+x and then confirming by pressing y.

- Run the following command to update the package cache with the newly added pinning configuration:
<strong>sudo apt update</strong>
- The command will update the package cache.

- Finally, install the desired version of the package using the
apt install
command:
<strong>sudo apt install package_name</strong>
Replace package_name with the name of the package.

- After the installation completes, verify that the desired version is installed by checking the package’s version:
<strong>apt-cache show package_name | grep Version</strong>
- The command will show the installed package version.

5 Common Errors with Possible Solutions
When installing a specific package version using apt, it’s possible to encounter some common errors. Remember to carefully review any error messages you encounter during the installation process, as they often provide valuable insights into the root cause of the problem. Here are five common errors that you may come across and possible solutions:
- 🔍 “Package not found” error: This error typically occurs when the apt cannot locate the specified package. You may encounter the message “E: Unable to locate package package_name.” To resolve this error, double-check the package name for any spelling errors or typos. Ensure that the package is available in the repositories you have enabled. If the package comes from a third-party repository, ensure it is properly configured in your system.
- ❌ “Version not found” error: When you receive the error message “E: Version ‘desired_version’ for ‘package_name’ was not found,” it indicates that the specified version of the package is not available in the repositories you have enabled. Verify that you have correctly specified the desired version in the pinning configuration file. You may need to explore alternative installation methods or sources if the version is unavailable.
- ⚠️ Dependency conflict error: This error arises when there is a conflict between the desired version of a package and its dependencies. You may encounter an error message such as “package_name : Depends: dependency_package (= version) but it is not going to be installed.” To resolve this error, try updating the conflicting packages or finding compatible versions of the dependencies. In some cases, virtual environments or containers can help isolate the dependencies and resolve conflicts.
- 🔄 Pinning priority conflict error: The error message “The pin priority specified in the pinning configuration conflicts with an existing pin priority for ‘package_name'” suggests a conflict in pinning priorities. Ensure that the pin priority specified in the pinning configuration file is unique and does not clash with other configurations. Adjust the pin priority value to resolve the conflict and correctly prioritize the desired version.
- 💥 Incomplete package installation error: This error occurs when the package installation process is interrupted or unable to locate the necessary archive. You may see a message such as “The package ‘package_name’ needs to be reinstalled, but I can’t find an archive for it.” To troubleshoot this error, ensure a stable internet connection, and run
sudo apt update
to refresh the package cache. If the problem persists, consider switching to a different repository or contacting the package maintainer for further assistance.
To Sum Up
This article has provided you with a detailed step-by-step guide on how to install a specific version of a package using apt. By following the outlined process, you can gain greater control over your system’s software environment and ensure you have installed the desired versions of packages. Throughout the article, I have also discussed common errors that may occur during installation.
To further expand your knowledge, consider exploring the following topics, Advanced package management techniques, Exploring alternative package managers, and Learning about package management best practices. Continuous learning and exploration are essential for mastering package management and Linux administration. Enjoy the journey of becoming a proficient package manager!
Frequently Asked Questions
Can I install multiple versions of a package simultaneously?
Yes, installing multiple versions of a package simultaneously using package pinning is possible. By specifying a particular version in the pinning configuration, you can prioritize it over others during installation. However, managing multiple versions can be complex and may lead to dependency conflicts. It is crucial to handle dependencies and ensure system stability carefully. Consider utilizing tools like virtual environments or containers to isolate different versions of packages when necessary.
How do I revert to a previous package version after installation?
To revert to a previous package version, check if the desired version is available in the package cache. Using the apt-cache policy package_name
command, verify its presence. If the previous version is found, reinstall it by running sudo apt install package_name=previous_version
. However, downgrading packages can introduce compatibility or stability issues, so thorough testing is essential to ensure the desired behavior and avoid potential problems.
Can I pin packages from third-party repositories?
Yes, you can pin packages from third-party repositories. By modifying the pinning configuration file, usually located at /etc/apt/preferences.d
/, you can assign priorities to packages from different repositories. Specify the repository and assign a higher priority to ensure that packages from the third-party repository take precedence over default ones. This way, you can pin specific packages from third-party repositories and have control over their installation and updates.
Is it possible to exclude a specific package from updates?
Indeed, it is possible to exclude a specific package from updates using the apt-mark
command. Identify the package you want to exclude by running apt list --upgradable
. Then, hold the package version with sudo apt-mark hold package_name
. This marks the package as held, preventing it from being updated during system-wide updates. Remember that holding a package can have implications for security and stability, as updates won’t be received. Exercise caution and consider the reasons for excluding a package, ensuring you have an alternative plan to address security or functionality concerns.