Background
When upgrading internal network servers, it is often necessary to download the installation packages in advance. However, dependency issues can be challenging to resolve in a Linux environment. Therefore, we need a method that can download all dependencies of a software package during installation. In this article, we will introduce how to achieve this. There are many Linux distributions, but we will use CentOS 7 and Ubuntu as examples.
CentOS 7
This article provides two methods for CentOS 7:
Method 1 [Recommended]
1. Install the Tool
yum install yum-utils
2. Usage
yumdownloader <package>
Note:
By default, the packages are downloaded to the current directory. To specify a download directory, you can use –destdir.
To download dependencies, you need to add –resolve.
3. Usage Example
For example, to download vim:
yumdownloader --destdir=/download --resolve vim
After running, you will see all the installation packages as follows:
[root@2c6e5f3d0398 download]# ls
gpm-libs-1.20.7-6.el7.x86_64.rpm perl-File-Temp-0.23.01-3.el7.noarch.rpm perl-Pod-Simple-3.28-4.el7.noarch.rpm perl-Time-Local-1.2300-2.el7.noarch.rpm perl-threads-shared-1.43-6.el7.x86_64.rpm
groff-base-1.22.2-8.el7.x86_64.rpm perl-Filter-1.49-3.el7.x86_64.rpm perl-Pod-Usage-1.63-3.el7.noarch.rpm perl-constant-1.27-2.el7.noarch.rpm vim-common-7.4.629-8.el7_9.x86_64.rpm
perl-5.16.3-299.el7_9.x86_64.rpm perl-Getopt-Long-2.40-3.el7.noarch.rpm perl-Scalar-List-Utils-1.27-248.el7.x86_64.rpm perl-libs-5.16.3-299.el7_9.x86_64.rpm vim-enhanced-7.4.629-8.el7_9.x86_64.rpm
perl-Carp-1.26-244.el7.noarch.rpm perl-HTTP-Tiny-0.033-3.el7.noarch.rpm perl-Socket-2.010-5.el7.x86_64.rpm perl-macros-5.16.3-299.el7_9.x86_64.rpm vim-filesystem-7.4.629-8.el7_9.x86_64.rpm
perl-Encode-2.51-7.el7.x86_64.rpm perl-PathTools-3.40-5.el7.x86_64.rpm perl-Storable-2.45-3.el7.x86_64.rpm perl-parent-0.225-244.el7.noarch.rpm which-2.20-7.el7.x86_64.rpm
perl-Exporter-5.68-3.el7.noarch.rpm perl-Pod-Escapes-1.04-299.el7_9.noarch.rpm perl-Text-ParseWords-3.29-4.el7.noarch.rpm perl-podlators-2.5.1-3.el7.noarch.rpm
perl-File-Path-2.09-2.el7.noarch.rpm perl-Pod-Perldoc-3.20-4.el7.noarch.rpm perl-Time-HiRes-1.9725-3.el7.x86_64.rpm perl-threads-1.87-4.el7.x86_64.rpm
Method 2
1. Install the Tool
yum install yum-plugin-downloadonly
2. Usage
yum install --downloadonly --downloaddir= your-dir package-name
3. Usage Example
Again, using the vim installation package as an example:
yum install --downloadonly --downloaddir=/download vim
Ubuntu
The local practical version is Ubuntu 18.04.
Method 1 (Native) [Recommended]
Use apt-cache depends to recursively query all dependencies, then use apt-get download to batch download, suitable for most scenarios.
1. Usage Format
apt-get download $(
apt-cache depends --recurse \
--no-recommends --no-suggests \
--no-conflicts --no-breaks \
--no-replaces --no-enhances \
<package-name> | grep "^\w" | sort -u \
)
Parameter Explanation:
- –recurse: Recursively query all dependencies (including dependencies of dependencies);
- –no-recommends: Exclude recommended dependencies, keeping only core dependencies;
- –no-suggests: Exclude suggested dependencies, keeping only core dependencies;
- –no-breaks: Filter out packages marked as Breaks to avoid interference with the dependency list;
- –no-replaces: Do not display information about packages that can be replaced, focusing on dependencies themselves;
- –no-enhances: Filter out information about enhancement packages, keeping only core dependencies;
- grep “^\w”: Filter out non-package name lines (such as dependency relationship descriptions);
- sort -u: Remove duplicates to avoid downloading the same dependency multiple times.
This command will download the installation packages and dependencies to the current directory.
2. Installation Methods
2.1 dpkg Installation
Directly use dpkg to install deb packages.
dpkg -i <package-name>
2.2 apt Installation
This method essentially packages the locally downloaded deb packages onto the internal network server for use as a local repository.
The local practical directory is:<span>/download/pkg</span>, and all downloaded packages are placed here. Again, using the download of<span>vim</span> as an example.
2.1.1 Create Local<span>.deb</span> Index Table
Install the<span>dpkg-scanpackages</span> command on an internet machine to perform the operation.
apt install dpkg-dev
Operate in the<span>/download</span> directory
dpkg-scanpackages -m . /dev/null | gzip -9c > Packages.gz
mv Package.gz pkg/
Packages.gz: Records the name, version, dependencies, architecture, and other information of each package. apt uses it to identify which packages are available locally;
2.2.2 Create Local Source “Credentials”
Operate in the<span>/download/pkg</span> directory
cat > Release << EOF
Origin: Local Repository
Label: Local
Suite: stable
Codename: $(grep VERSION_CODENAME /etc/os-release | cut -d= -f2)
Version: 1.0
Architectures: amd64
Components: main
Description: Local APT Repository for Offline Packages
EOF
Codename: System release code name
Architectures: System architecture, 32-bit is i386
2.2.3 Configure Source on Internal Network Machine
Copy<span>/download</span> to the internal network machine for configuration.
- Source Configuration
cp /etc/apt/sources.list /etc/apt/sources.list.bak
echo "deb file:///download pkg/"| tee /etc/apt/sources.list
- Refresh Source
apt update --allow-insecure-repositories
<span>--allow-insecure-repositories</span>parameter temporarily allows unsigned sources.
2.2.4 Installation on Internal Network Machine
apt-get install vim
Method 2 [Not Recommended]
1. Install the Tool
apt install apt-offline
This method is relatively simple, but it requires a clean environment because this solution will only download missing dependencies from the system.
2. Tool Usage
Using vim as an example:
- 2.1 Generate Dependency Download List
apt-offline set ./vim-offline.sig --install-packages vim
Generate a dependency download list in the current directory.
- 2.2 Download Packages Based on Dependencies
apt-offline get ./vim-offline.sig -d /download
Method 3
This method has flaws and is for reference only.
[Note] Based on <span>apt-rdepends + apt-get download</span>, sometimes the package names of dependencies are incorrect, which may lead to download failures. A common error is:
E: Can't select candidate version from package debconf-2.0 as it has no candidate
Verification:
root@b8e208703627:/download/pkg# apt search debconf-2.0
Sorting... Done
Full Text Search... Done
root@b8e208703627:/download/pkg# apt search debconf
Sorting... Done
Full Text Search... Done
cdebconf/bionic 0.213ubuntu1 amd64
Debian Configuration Management System (C-implementation)
......
The real package name is<span>debconf</span>.
Here, we also provide a script for readers to use:
downloadPkg.sh
#!/bin/bash
# Check if parameters are correct
if [ $# -ne 1 ]; then
echo "Usage: $0 <package-name>"
echo "Example: $0 vim"
exit 1
fi
PACKAGE="$1"
SAVE_DIR=$(pwd)
# Check if apt-rdepends is installed
if ! command -v apt-rdepends > /dev/null; then
echo "Error: apt-rdepends not found. Please install it first:"
echo "sudo apt update && sudo apt install -y apt-rdepends"
exit 1
fi
echo "Starting to download all dependencies of $PACKAGE, saving to $SAVE_DIR ..."
# Recursively query all dependencies and download (deduplicate, skip invalid packages)
for pkg in $(apt-rdepends "$PACKAGE" | grep -v "^ " | sort -u); do
# Check if the package exists (exclude virtual packages or invalid ones)
if ! apt-cache show "$pkg" > /dev/null; then
echo "[Warning] Skipping invalid dependency: $pkg"
continue
fi
# Download the package to the specified directory
echo "Downloading: $pkg"
apt-get download "$pkg"
if [ $? -ne 0 ]; then
echo "[Failed] Failed to download $pkg"
fi
done
echo "[Success] Download completed. All dependencies are saved to $SAVE_DIR"
Usage:
sh downloadPkg.sh <package-name>
The current script can only download packages and dependencies to the current directory and needs improvement.
Conclusion
Thank you for reading. Creating content is not easy. If you like my content, please give a thumbs up and support me. Feel free to follow for more exciting shares and updates. Your support is my motivation to create. Let’s explore more technology together!