Methods and precautions for installing multiple versions of Python under ubuntu

  • 2020-05-12 06:42:32
  • OfStack

Today 1 accidentally broke the ubuntu system again, because I uninstalled python3, and then... Well, enough with that, let's talk about how to manage multiple versions of python under ubuntu. I'm using an Python version management tool called pyenv.

System environment: ubuntu14.04LTS, the default version of python is 2.7, I want to install another version of 3.4.3.

Before installing python again, we first need to install the administration tool pyenv:


$ git clone git://github.com/yyuu/pyenv.git ~/.pyenv
$ echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
$ echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
$ echo 'eval "$(pyenv init -)"' >> ~/.bashrc
$ exec $SHELL -l

You can also view the version of python you can install with this command:


$ pyenv install --list

If we decide to install python 3.4.3, then we can install python, but before we can install python, we must install the dependency package required by python. This must be installed, and the installation will fail:


$ sudo apt-get install libc6-dev gcc
$ sudo apt-get install -y make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm

Once the above dependencies are in place, we can install python:


$ pyenv install 3.4.3 -v

The command downloads the source code for python from github, unpacks it into the /tmp directory, and then compiles it in /tmp. If the dependency package is not installed, a compilation error occurs and the command needs to be reexecuted after the installation of the dependency package.

After the installation is complete, you need to update the database using the following command:


$ pyenv rehash

View the currently installed version of python


$ pyenv versions
* system (set by /home/seisman/.pyenv/version)
3.4.3

The asterisk indicates that the version of python currently in use is shipped with the system.

Set the global python version


$ pyenv global 3.4.3
$ pyenv versions
system
* 3.4.3 (set by /home/seisman/.pyenv/version)

From above, we can see that the current version of python has changed to 3.4.3. You can also use pyenv local or pyenv shell to temporarily change the version of python.

Confirm the python version


$ python
Python 3.4.3 (default, Apr 1 2015, 19:10:43) 
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.

Matters needing attention:

Enter python to use the new python;

The built-in script of the system will directly call the old version of python in the way of /usr/bin/python, so there will be no impact on the system script;

When using pip to install the third party module, it will be installed under ~/.pyenv /versions/3.4.3, and there will be no conflict with the system module;

After installing the module using pip, you may need to perform an pyenv rehash database update.


Related articles: