Manage multiple Python versions with update-alternatives (Ubuntu)

You need to use different third party libraries and want to achieve the following:

-(1) I want to use multiple Python versions properly (Python 2.7, 3.6, 3.8, etc.) -(2) I want to install the main library (shared between projects) such as numpy separately for each Python version. -(3) I want to create a Python environment for each project within the Python version.

(I want to run Ubuntu on a Mac with VirtualBox and experiment to achieve the above goals so as not to adversely affect ongoing projects.)

As the first step, we will perform (1) and (2).

(0) Environment

--Ubuntu 20.04 (using VirtualBox 6.1 on Mac)

(1) Use multiple Python versions properly (Python 2.7, 3.6, 3.8, etc.)


$ sudo apt update && apt upgrade -y

$ sudo apt-get install build-essential checkinstall
$ sudo apt-get install libreadline-gplv2-dev libncursesw5-dev libssl-dev \
    libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev libffi-dev zlib1g-dev

$ apt list --upgradable

$ sudo apt-get install build-essential checkinstall

$ sudo make altinstall 
$ python3 --version
Python 3.8.2

You can see that the python3 included at this stage is python3.8 (python 3.8.2). (I looked it up and found it to be in / usr / bin /.)

Then find the link for the required Python version in the list at https://www.python.org/ftp/python/ and use wget to download it to / opt /.


#First python-3.8.0
$ cd /opt
$ sudo wget https://www.python.org/ftp/python/3.8.0/Python-3.8.0.tgz 
$ sudo tar xzf Python-3.8.0.tgz 
$ cd Python-3.8.0
$ sudo ./configure --enable-optimizations
$ sudo make altinstall


#Then Python-3.6.10
$ cd /opt
$ sudo wget https://www.python.org/ftp/python/3.6.10/Python-3.6.10.tgz 
$ sudo tar xzf Python-3.6.10.tgz 
$ cd Python-3.8.0
$ sudo ./configure --enable-optimizations
$ sudo make altinstall

#Check directory
$ cd /opt
$ ls -l
total 45884
drwxr-xr-x 18   501   501     4096 May  9 18:33 Python-3.6.10
-rw-r--r--  1 root  root  23019480 Dec 18 14:48 Python-3.6.10.tgz
drwxr-xr-x 18 kohei kohei     4096 May  9 17:48 Python-3.8.0
-rw-r--r--  1 root  root  23949883 Oct 14  2019 Python-3.8.0.tgz
drwxr-xr-x  8 root  root      4096 May  8 18:04 VBoxGuestAdditions-6.1.6


$ sudo update-alternatives --install /usr/bin/python python /opt/Python-3.6.10/python 1
$ sudo update-alternatives --install /usr/bin/python python /opt/Python-3.8.0/python 2
$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.8 3
$ update-alternatives --list python
/opt/Python-3.6.10/python
/opt/Python-3.8.0/python
/usr/bin/python3.8


$ sudo update-alternatives --config python
There are 3 choices for the alternative python (providing /usr/bin/python).

  Selection    Path                       Priority   Status
------------------------------------------------------------
  0            /usr/bin/python3.8          3         auto mode
* 1            /opt/Python-3.6.10/python   1         manual mode
  2            /opt/Python-3.8.0/python    2         manual mode
  3            /usr/bin/python3.8          3         manual mode

Press <enter> to keep the current choice[*], or type selection number: 1


$ cd /usr/bin
$ ls -l pyt*
lrwxrwxrwx 1 root root      24 May  9 18:20 python -> /etc/alternatives/python
lrwxrwxrwx 1 root root       9 May  8 17:33 python3 -> python3.8
-rwxr-xr-x 1 root root 5457536 Apr 27 11:53 python3.8
lrwxrwxrwx 1 root root      33 Apr 27 11:53 python3.8-config -> x86_64-linux-gnu-python3.8-config
lrwxrwxrwx 1 root root      16 Mar 13 08:20 python3-config -> python3.8-config

$ ls -l /etc/alternatives/python
lrwxrwxrwx 1 root root 25 May  9 19:27 /etc/alternatives/python -> /opt/Python-3.6.10/python

$ which python
/usr/bin/python

$ python -V
Python 3.6.10

You can see that / etc / alternatives / python is a symbolic link to the path (in this case /opt/Python-3.6.10/python) corresponding to the user-specified Python.

Summary,

-(a) When you call python, -(b) / usr / bin / python is called. -(c) However, since / usr / bin / python is a symbolic link to / etc / alternatives / python, / etc / alternatives / python is called. -(d) In addition, / etc / alternatives / python is linked to /opt/Python-3.6.10/python (in the current settings of ʻupdate-alternatives). -(e) Eventually, /opt/Python-3.6.10/python` is called.

Next, update pip by following the steps below.

#Verification
$ which python
/usr/bin/python
#Verification
$ python -V
Python 3.6.10

$ apt install python3-pip
$ python -m pip install --upgrade pip
$ pip --version
pip 20.1 from /usr/local/lib/python3.6/site-packages/pip (python 3.6)

(2) Install the library separately for each Python version

From now on, I will put the library individually for each version of Python. The procedure is as follows.

#Verification
$ which python
/usr/bin/python
#Verification
$ python -V
Python 3.6.10
$ sudo python -m pip install numpy
Collecting numpy
  Downloading https://files.pythonhosted.org/packages/03/27/e35e7c6e6a52fab9fcc64fc2b20c6b516eba930bb02b10ace3b38200d3ab/numpy-1.18.4-cp36-cp36m-manylinux1_x86_64.whl (20.2MB)
    100% |████████████████████████████████| 20.2MB 2.3MB/s 
Installing collected packages: numpy
Successfully installed numpy-1.18.4

$ sudo python -m pip install scipy
Collecting scipy
  Downloading scipy-1.4.1-cp36-cp36m-manylinux1_x86_64.whl (26.1 MB)
     |████████████████████████████████| 26.1 MB 5.8 MB/s 
Requirement already satisfied: numpy>=1.13.3 in /usr/local/lib/python3.6/site-packages (from scipy) (1.18.4)
Could not build wheels for numpy, since package 'wheel' is not installed.
Installing collected packages: scipy
Successfully installed scipy-1.4.1

#Check the result.
$ python -c 'import sys; print(sys.version); import numpy; print(numpy.__file__); import scipy; print(scipy.__file__)'
3.6.10 (default, May  9 2020, 18:32:23) 
[GCC 9.3.0]
/usr/local/lib/python3.6/site-packages/numpy/__init__.py
/usr/local/lib/python3.6/site-packages/scipy/__init__.py

For reference, let's see what's in / usr / local / lib / python3.6 / site-packages /.

$ ls -l /usr/local/lib/python3.6/site-packages/
total 52
-rw-r--r--  1 root root  126 May  9 18:34 easy_install.py
drwxr-xr-x 17 root root 4096 May 10 18:45 numpy
drwxr-xr-x  2 root root 4096 May 10 18:45 numpy-1.18.4.dist-info
drwxr-xr-x  2 root root 4096 May 10 18:45 numpy.libs
drwxr-xr-x  5 root root 4096 May 10 18:46 pip
drwxr-xr-x  2 root root 4096 May 10 18:46 pip-20.1.dist-info
drwxr-xr-x  5 root root 4096 May  9 18:34 pkg_resources
drwxr-xr-x  2 root root 4096 May  9 18:34 __pycache__
-rw-r--r--  1 root root  119 May  9 18:33 README.txt
drwxr-xr-x 23 root root 4096 May 10 18:46 scipy
drwxr-xr-x  2 root root 4096 May 10 18:46 scipy-1.4.1.dist-info
drwxr-xr-x  6 root root 4096 May  9 18:34 setuptools
drwxr-xr-x  2 root root 4096 May  9 18:34 setuptools-40.6.2.dist-info

For further reference, let's see that this installation of numpy scipy only affects Python 3.6.10 (as intended). Of course, I haven't touched Python 3.8.0, so it looks like this:

$ /opt/Python-3.8.0/python -c 'import sys; print(sys.version); import numpy; print(numpy.__file__); import scipy; print(scipy.__file__)'
3.8.0 (default, May  9 2020, 17:47:23) 
[GCC 9.3.0]
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'numpy'

It's a bit arrogant, but the same can be seen by switching Python with ʻupdate-alternatives`.

$ sudo update-alternatives --config python
[sudo] password for kohei: 
There are 3 choices for the alternative python (providing /usr/bin/python).

  Selection    Path                       Priority   Status
------------------------------------------------------------
  0            /usr/bin/python3.8          3         auto mode
* 1            /opt/Python-3.6.10/python   1         manual mode
  2            /opt/Python-3.8.0/python    2         manual mode
  3            /usr/bin/python3.8          3         manual mode

Press <enter> to keep the current choice[*], or type selection number: 2
update-alternatives: using /opt/Python-3.8.0/python to provide /usr/bin/python (python) in manual mode
kohei@kohei-VirtualBox:~$ 
kohei@kohei-VirtualBox:~$ which python
/usr/bin/python
kohei@kohei-VirtualBox:~$ python -V
Python 3.8.0
kohei@kohei-VirtualBox:~$ 
kohei@kohei-VirtualBox:~$ python -c 'import sys; print(sys.version); import numpy; print(numpy.__file__); import scipy; print(scipy.__file__)'
3.8.0 (default, May  9 2020, 17:47:23) 
[GCC 9.3.0]
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'numpy'
kohei@kohei-VirtualBox:~$ 

Of course, don't forget to revert to the original Python version at the end.

kohei@kohei-VirtualBox:~$ sudo update-alternatives --config python
There are 3 choices for the alternative python (providing /usr/bin/python).

  Selection    Path                       Priority   Status
------------------------------------------------------------
  0            /usr/bin/python3.8          3         auto mode
  1            /opt/Python-3.6.10/python   1         manual mode
* 2            /opt/Python-3.8.0/python    2         manual mode
  3            /usr/bin/python3.8          3         manual mode

Press <enter> to keep the current choice[*], or type selection number: 1
update-alternatives: using /opt/Python-3.6.10/python to provide /usr/bin/python (python) in manual mode

kohei@kohei-VirtualBox:~$ which python
/usr/bin/python
kohei@kohei-VirtualBox:~$ python -V
Python 3.6.10

References

Recommended Posts

Manage multiple Python versions with update-alternatives (Ubuntu)
Use multiple versions of python environment with pyenv
Developed and verified with multiple python versions with direnv
Manage multiple context managers together with Python contextlib.ExitStack
Manage cron jobs with python
Manage python environment with virtualenv
Build python3 environment with ubuntu 16.04
Install multiple versions of Python
[Package cloud] Manage python packages with package cloud
Post multiple Twitter images with python
Animate multiple still images with Python
Manage each Python version with Homebrew
[Python] Creating multiple windows with Tkinter
Handle multiple python versions in one jupyter
[Pyenv] Building a python environment with ubuntu 16.04
[Ubuntu 18.04] Python environment construction with pyenv + pipenv
Process multiple lists with for in Python
Question: Multiple integrals with python don't work
FizzBuzz with Python3
Scraping with Python
Statistics with python
Build python environment with pyenv on EC2 (ubuntu)
Scraping with Python
Python with Go
[Python] How to draw multiple graphs with Matplotlib
You can manage overlapping characters with multiple plt.text.
Integrate with Python
Switch Python versions
AES256 with python
Tested with Python
python starts with ()
Manage AWS nicely with the Python library Boto
with syntax (Python)
Bingo with python
Zundokokiyoshi with python
[Python] Dealing with multiple call errors in ray.init
Convert multiple proto files at once with python
[Python] Mention to multiple people with Slack API
Excel with Python
Microcomputer with Python
Cast with python
I made a Python3 environment on Ubuntu with direnv.
Write multiple records to DynamoDB with Lambda (Python, JavaScript)
Troublesome story when using Python3 with VScode on ubuntu
Ubuntu 20.04 on raspberry pi 4 with OpenCV and use with python
Manage Python runtime packages and development environment packages with Poetry
Remove headings from multiple format CSV files with python
Serial communication with Python
Zip, unzip with python
Primality test with Python
Python with eclipse + PyDev.
Socket communication with Python
Data analysis with python 2
Multiple selections with Jupyter
Scraping with Python (preparation)
Try scraping with Python.
Sequential search with Python
"Object-oriented" learning with python
Run Python with VBA
Solve AtCoder 167 with python
Serial communication with python