Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I install python 3.7 in ubuntu 18.04 without having python 3.6 in the system?

Please read the question carefully before closing as duplicate, I believe the use case to be unique.

I'm trying to create a docker image that only has python 3.7 installed, the problem is that if I try to install pip, the command also installs python 3.6 which I do not want.

The relevant part of the ideal docker file I'm tinkering is as follows

FROM ubuntu:18.04

# Upgrade installed packages
RUN apt-get update && apt-get upgrade -y && apt-get clean

# (...)

# Python package management and basic dependencies
RUN apt-get install -y python3.7 python3.7-dev python3.7-pip

# Register the version in alternatives
RUN update-alternatives --install /usr/bin/python python /usr/bin/python3.7 1

# Set python 3 as the default python
RUN update-alternatives --set python /usr/bin/python3.7

# Upgrade pip to latest version
RUN python -m ensurepip --upgrade

# (...)

This would fail as python3.7-pip doesn't seem to exist; only python3-pip does, which is what installs python 3.6 for some reason.

I tried not installing pip at all and doing it manually, like so

# (...)

RUN apt-get install -y python3.7 python3.7-dev

# (...)

RUN curl 'https://bootstrap.pypa.io/get-pip.py' > get-pip.py

RUN python get-pip.py pip --no-setuptools --no-wheel

Which fails with this error:

Traceback (most recent call last):
  File "get-pip.py", line 21492, in <module>
    main()
  File "get-pip.py", line 197, in main
    bootstrap(tmpdir=tmpdir)
  File "get-pip.py", line 82, in bootstrap
    import pip._internal
  File "/tmp/tmpbez2vju9/pip.zip/pip/_internal/__init__.py", line 40, in <module>
  File "/tmp/tmpbez2vju9/pip.zip/pip/_internal/cli/autocompletion.py", line 8, in <module>
  File "/tmp/tmpbez2vju9/pip.zip/pip/_internal/cli/main_parser.py", line 8, in <module>
  File "/tmp/tmpbez2vju9/pip.zip/pip/_internal/cli/cmdoptions.py", line 14, in <module>
ModuleNotFoundError: No module named 'distutils.util'

Again, installing python3-distutils results in python 3.6 appearing in the system

So, is there a way to install ONLY a fully functional python 3.7 in ubuntu 18.04, WITHOUT having to install python 3.6?

like image 676
juan Avatar asked May 14 '19 17:05

juan


People also ask

Can you have two versions of Python installed Linux?

It can install multiple Python versions, specify the version that's used system-wide, and specify the version that's used in specific directories. It can also create and manage virtual environments using specific versions.

How do I install a specific version of Python in Ubuntu?

Installing a specific version of Python. The first step to installing Python is to install the necessary dependencies and packages that are required for its installation. However, to install these dependencies, you must have the multiverse repository enabled. You can enable it by running the command given below.


2 Answers

In case someone else is ok with getting Python3.6 installed as a side effect (python3.7-distutils introduces it as pointed out by OP). This will install Python3.7 making it default and have the latest available pip using your python3.7 installation

FROM ubuntu:18.04

# Upgrade installed packages
RUN apt-get update && apt-get upgrade -y && apt-get clean

# (...)

# Python package management and basic dependencies
RUN apt-get install -y curl python3.7 python3.7-dev python3.7-distutils

# Register the version in alternatives
RUN update-alternatives --install /usr/bin/python python /usr/bin/python3.7 1

# Set python 3 as the default python
RUN update-alternatives --set python /usr/bin/python3.7

# Upgrade pip to latest version
RUN curl -s https://bootstrap.pypa.io/get-pip.py -o get-pip.py && \
    python get-pip.py --force-reinstall && \
    rm get-pip.py


# (...)
like image 187
donhector Avatar answered Oct 21 '22 12:10

donhector


I see two choices:

  1. Using a Ubuntu image, leave the Python from system untouched. Install pyenv (https://github.com/pyenv/pyenv), then download a python 3.7 install, completely separated from system's Python.

or

  1. Use the official Python image labeled 3.7.3-stretch or 3.7.3-slim-stretch (Debian)
like image 35
Hildeberto Avatar answered Oct 21 '22 12:10

Hildeberto