简体   繁体   中英

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.

The project requires python3.8 and the latest pip version and virtualenv to be set up from scratch.

The jenkins server already has a global python2.7 installed and I don't want to disturb that setup and namespace.

So the new python should be invoked with new alias say, python3.8 --version .

I see a lot of resources for installing python, pip, and vituralenv separately. The official setup processes does not install pip with python and does not address the installation in the build server point of view.

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)

What are the best practices and recommended ways to install python and its dependencies in a build server.

The following script creates a new python3.8 installation along with all other dependencies need for the install in 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. 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 .

For creating a new virtual environmentment, I used the standard venv library.

python3 -m venv test_env
source test_env/bin/activate

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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