Ubuntu apt、pip离线安装包

项目要部署在远程服务器上,而服务器不能访问外网,所以只能将需要安装的包下载下来再放到Ubuntu上

下载安装文件

apt

由于目标服务器是Ubuntu 18.04,所以本机的容器也使用18.04

# 拉取镜像
>>> docker pull ubuntu:18.04

# 运行容器并将本机cache目录映射到容器内的cache目录以便存放下载的依赖包
>>> docker run -i -t -v /Users/microfat/Downloads/cache:/cache --name env ubuntu:18.04

root@4899fed3e4e0:/# apt -d install --no-install-recommends python3.8-dev

root@4899fed3e4e0:/# apt -d install --no-install-recommends python3-pip

root@4899fed3e4e0:/# ls /var/cache/apt/archives
ca-certificates_20201027ubuntu0.18.04.1_all.deb        libpython3.8-dev_3.8.0-3~18.04_amd64.deb      mime-support_3.60ubuntu1_all.deb                    python3.6_3.6.9-1~18.04ubuntu1.3_amd64.deb
libc-dev-bin_2.27-3ubuntu1.3_amd64.deb                 libpython3.8-minimal_3.8.0-3~18.04_amd64.deb  openssl_1.1.1-1ubuntu2.1~18.04.7_amd64.deb          python3.8-dev_3.8.0-3~18.04_amd64.deb
libc6-dev_2.27-3ubuntu1.3_amd64.deb                    libpython3.8-stdlib_3.8.0-3~18.04_amd64.deb   partial                                             python3.8-minimal_3.8.0-3~18.04_amd64.deb
libexpat1-dev_2.2.5-3ubuntu0.2_amd64.deb               libpython3.8_3.8.0-3~18.04_amd64.deb          python-pip-whl_9.0.1-2.3~ubuntu1.18.04.4_all.deb    python3.8_3.8.0-3~18.04_amd64.deb
libexpat1_2.2.5-3ubuntu0.2_amd64.deb                   libreadline7_7.0-3_amd64.deb                  python3-distutils_3.6.9-1~18.04_all.deb             python3_3.6.7-1~18.04_amd64.deb
libmpdec2_2.4.2-1ubuntu1_amd64.deb                     libsqlite3-0_3.22.0-1ubuntu0.4_amd64.deb      python3-lib2to3_3.6.9-1~18.04_all.deb               readline-common_7.0-3_all.deb
libpython3-stdlib_3.6.7-1~18.04_amd64.deb              libssl1.1_1.1.1-1ubuntu2.1~18.04.7_amd64.deb  python3-minimal_3.6.7-1~18.04_amd64.deb             zlib1g-dev_1%3a1.2.11.dfsg-0ubuntu2_amd64.deb
libpython3.6-minimal_3.6.9-1~18.04ubuntu1.3_amd64.deb  linux-libc-dev_4.15.0-128.131_amd64.deb       python3-pip_9.0.1-2.3~ubuntu1.18.04.4_all.deb
libpython3.6-stdlib_3.6.9-1~18.04ubuntu1.3_amd64.deb   lock                                          python3.6-minimal_3.6.9-1~18.04ubuntu1.3_amd64.deb

root@4899fed3e4e0:/# mv /var/cache/apt/archives/*.deb /cache/

至此所有python3.8和pip所需要的依赖都下载到了本机的cache目录中

pip

由于python3-pip默认会安装python3.6且pip版本较老(9.0.1),因而还需要下载最新的pip安装包
https://pypi.org/project/pip/#files
Ubuntu apt、pip离线安装包_第1张图片

root@4899fed3e4e0:/# python3.8 -m pip download -i https://pypi.tuna.tsinghua.edu.cn/simple pandas

root@4899fed3e4e0:/# ls
numpy-1.19.4-cp38-cp38-manylinux2010_x86_64.whl  pandas-1.1.5-cp38-cp38-manylinux1_x86_64.whl  python_dateutil-2.8.1-py2.py3-none-any.whl  pytz-2020.4-py2.py3-none-any.whl  six-1.15.0-py2.py3-none-any.whl

root@4899fed3e4e0:/# mv *.whl /cache/

将.whl文件一并传到远程服务器

传输到远程服务器

此处省略传输到远程服务器的过程,你可以通过scp、xftp、u盘等等方式

安装

apt

默认你已经将依赖包传输至远程服务器的 /var/cache目录下
登录到远程服务器

>>> sudo touch /var/cache/Packages.gz

>>> sudo chmod -R 777 /var/cache/

>>> cd /var

>>> sudo dpkg-scanpackages cache  /dev/null  | gzip > cache/Packages.gz

>>> sudo vi /etc/apt/sources.list
将所有内容注释掉
在结尾添加:
deb file:/var cache/

>>> sudo apt-get update

>>> sudo apt install python3.8-dev

>>> sudo apt install python3-pip

>>> pip3 --version
pip 9.0.1 from /user/lib/python3/dist-packages (python 3.6)

>>> python3 -m pip install pip-20.3.3-py2.py3-none-any.whl

>>> python3.8 -m pip --version
pip 20.3.3 from /home/user/.local/lib/python3.8/site-packages/pip (python 3.8)

pip

Ubuntu apt、pip离线安装包_第2张图片

参考:https://stackoverflow.com/a/14447068

你可能感兴趣的:(工具,ubuntu,apt,离线)