Apt Install Specific Version of a Package: How to Do It? [9 Easy Steps]

Written by

Reviewed by

Last updated: May 21, 2024

Expert verified

SVG Image

TL;DR

To apt install specific version of a Package, you can try the following steps.

  1. Determine the package name and desired version. The open a Terminal window.
  2. Check the available versions of the package using apt-cache policy package_name.
  3. Create a pinning configuration file using sudo nano /etc/apt/preferences.
  4. Add package-specific lines to the pinning configuration file.
  5. Update the package cache with sudo apt update and install the desired version using sudo apt install package_name.

Read the step-by-step guide below to apt install specific version of a package and explore some common errors with possible solutions.

We’ve all been there, you need a specific version of a package for your project, but apt keeps installing the latest version, causing compatibility issues. Thankfully, there’s a solution, and I’m here to help you through it. In this post, you’ll learn how to use apt to install a specific version of a package. I’ll guide you step-by-step, from checking available versions to setting up a pinning configuration. Plus, I’ll share tips for handling common errors that might pop up. By the end, you’ll be able to manage your package versions with confidence and ease.

How to Apt Install Specific Version of a Package?

To apt install a specific version of a package, first update your package lists with sudo apt update. Then, check available versions with apt-cache policy package_name. Create a pinning configuration file in /etc/apt/preferences.d/ and add rules to specify the desired version and priority.

Save the file, update your package lists again, and finally, install the package using sudo apt install package_name=version_number. Verify the installation with apt-cache policy package_name or by checking the version directly with the package’s command. This method ensures you install and maintain the exact version you need.

That was the quick answer. Here is the detailed step-by-step guide to apt install specific version of a package:

  1. 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.
  2. Open a Terminal window. 
opening terminal 13
  1. Use the apt-cache command to check the available versions of the package:
apt-cache policy package_name

Replace package_name with the name of the package you want to install.

checking the available version
  1. If you don’t already have a pinning configuration file, create one using a text editor:
sudo nano /etc/apt/preferences

The command will create a pinning configuration file.

    creating a pinning configuration file
    1. In the pinning configuration file, add the following lines:
    Package: package_name
    Pin: version desired_version
    Pin-Priority: 1001

    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.

    adding lines to pinning file
    1. Save the changes and close the pinning configuration file by pressing Ctrl+x and then confirming by pressing y.
    saving the configuration file
    1. Run the following command to update the package cache with the newly added pinning configuration:
    sudo apt update

    The command will update the package cache.

      updating package for newly added configuration
      1. Finally, install the desired version of the package using the apt install command:
      sudo apt install package_name

      Replace package_name with the name of the package.

      installing desired package
      1. After the installation completes, verify that the desired version is installed by checking the package’s version:
      apt-cache show package_name | grep Version

      The command will show the installed package version.

        verifying the desired installation

        How to Hold the Package and Prevent Upgrade?

        Holding a package stops it from upgrading when you run sudo apt upgrade, ensuring that it stays at the current version.To hold a package and prevent it from upgrading, follow these steps:

        1. Press Ctrl+Alt+T to open a terminal window.
        2. Type the following command to hold the package:
        sudo apt-mark hold package_name

        Replace package_name with the name of the package you want to hold.

        marking a package on hold
        1. To check if the package is held, use this command:
        apt-mark showhold

        This will list all the packages currently on hold.

        listing packages currently on hold

        What is Pinning Configuration?

        A pinning configuration in apt allows you to specify which versions of packages you want to install and maintain on your system. By creating rules in a configuration file, you can control the priority of different versions of packages from various repositories. This helps ensure that apt installs the exact versions you need, rather than always upgrading to the latest available version.

        The configuration typically involves setting priorities (or “pins”) for specific packages or package versions. A higher priority ensures that apt prefers those versions over others. This is especially useful for maintaining compatibility and stability in environments where specific versions are required, such as in development or production systems.

        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:

        1. 🔍 “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.

        2. ❌ “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.

        3. ⚠️ 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.

        4. 🔄 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.

        5. 💥 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.

          Apt Install a Specific Version: To Sum Up

          In this guide, I walked you through how to install a specific version of a package using apt. By following these steps, you can easily find available versions, set up a pinning configuration, and handle dependencies to get the version you need.

          For more learning, I suggest checking out some related articles:

          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.

          Ojash

          Author

          Ojash is a skilled Linux expert and tech writer with over a decade of experience. He has extensive knowledge of Linux's file system, command-line interface, and software installations. Ojash is also an expert in shell scripting and automation, with experience in Bash, Python, and Perl. He has published numerous articles on Linux in various online publications, making him a valuable resource for both seasoned Linux users and beginners. Ojash is also an active member of the Linux community and participates in Linux forums.

          Akshat

          Reviewer

          Akshat is a software engineer, product designer and the co-founder of Scrutify. He's an experienced Linux professional and the senior editor of this blog. He is also an open-source contributor to many projects on Github and has written several technical guides on Linux. Apart from that, he’s also actively sharing his ideas and tutorials on Medium and Attirer. As the editor of this blog, Akshat brings his wealth of knowledge and experience to provide readers with valuable insights and advice on a wide range of Linux-related topics.

          Share this article
          Shareable URL
          Prev Post

          3 Easy Ways to Show Line Numbers in Vim

          Next Post

          18 Easy Steps to Install Brave Web Browser on Linux

          Leave a Reply

          Your email address will not be published. Required fields are marked *

          Read next