简体   繁体   English

通过 shell 脚本在 ubuntu 服务器中设置 python3.8,对应的 pip 和 virtualenv

[英]Setup python3.8, corresponding pip and virtualenv in ubuntu server via shell script

I am trying to build a python project in our Jenkins build server pulling the code from our git repo.我正在尝试在我们的 Jenkins 构建服务器中构建一个 python 项目,从我们的 git 存储库中提取代码。

The project requires python3.8 and the latest pip version and virtualenv to be set up from scratch.该项目需要从头开始设置python3.8最新的pip版本和virtualenv

The jenkins server already has a global python2.7 installed and I don't want to disturb that setup and namespace. jenkins 服务器已经安装了全局 python2.7,我不想打扰该设置和命名空间。

So the new python should be invoked with new alias say, python3.8 --version .因此,应该使用新别名调用新的 python , python3.8 --version

I see a lot of resources for installing python, pip, and vituralenv separately.我看到很多用于分别安装 python、pip 和 vituralenv 的资源。 The official setup processes does not install pip with python and does not address the installation in the build server point of view.官方设置过程不会安装 pip 和 python 并且没有从构建服务器的角度解决安装问题。

So I want a single shell script to do this installation without expecting any user input prompt or OS-specific packaging tools and installing tools in the alias i am expecting (eg python3.8)因此,我想要一个 shell 脚本来执行此安装,而不需要任何用户输入提示或特定于操作系统的打包工具以及在我期望的别名中安装工具(例如 python3.8)

What are the best practices and recommended ways to install python and its dependencies in a build server.在构建服务器中安装 python 及其依赖项的最佳实践和推荐方法是什么。

The following script creates a new python3.8 installation along with all other dependencies need for the install in Ubuntu 16.04.以下脚本创建一个新的 python3.8 安装以及 Ubuntu 16.04 中安装所需的所有其他依赖项。

#!/bin/sh

# install PPA
sudo add-apt-repository ppa:deadsnakes/ppa

# update and install
sudo apt update
sudo apt install python3.8 python3.8-dev python3.8-venv

# setup alternatives
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.5 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 2

# show menu for selecting the version
sudo update-alternatives --config python3

# or one command to set it
# sudo update-alternatives --set python3 /usr/bin/python3.8

# install the latest pip version to the python3 version. 
curl https://bootstrap.pypa.io/get-pip.py | sudo python3.8

A word of advice, never touch python2.7 dependencies installed or default python path in ubuntu even if you are concerned only with python3.x.提个建议,即使您只关心 python3.x,也不要触摸安装的 python2.7 依赖项或 ubuntu 中的默认 python 路径。 Hell breaks loose and install of all dependencies start falling after that.地狱爆发,所有依赖项的安装开始下降。 Always install the required python version in new location and invoke it via a new path, like python3 --version .始终在新位置安装所需的 python 版本并通过新路径调用它,例如python3 --version

For creating a new virtual environmentment, I used the standard venv library.为了创建一个新的虚拟环境,我使用了标准的venv库。

python3 -m venv test_env
source test_env/bin/activate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM