This method does not use
apt-get, but it is, I believe, the best option available today, as it can easily compile any Python version from source for you, so you don't have to rely on any PPAs.
Pyenv allows you to manage multiple Python versions without sudo for a single user, much like Node.js NVM and Ruby RVM.
curl https://pyenv.run | bash
Then add to your
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
Find Python version to install:
pyenv install --list
Install the python version you want:
# Increase the chances that the build will have all dependencies.
sudo apt build-dep python3
sudo apt-get install -y make build-essential libssl-dev zlib1g-dev libbz2-dev \
libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev \
xz-utils tk-dev libffi-dev liblzma-dev python-openssl git
# Build and install a Python version from source.
pyenv install 3.8.0
List available Python versions:
We now have:
* system (set by /home/cirsan01/.pyenv/version)
Select a different python version:
pyenv global 3.8.0
We can now proceed to install and use packages normally:
pip install cowsay
python -c 'import cowsay; cowsay.tux("Python is fun")'
We can confirm that everything is locally installed in our clean environemnt with:
python -c 'import cowsay; print(cowsay.__file__)'
We see that
which python points to:
~/.pyenv/shims is prepended to
PATH with the rc scripts.
TODO understand further. The global version is somethow determined by:
which now contains:
Per project usage
In the previous section, we saw how to use pyenv in a global setup.
However, what you usually want is to set a specific python and package version on a per-project basis. This is how to do it.
First install your desired Python version as before.
Then, from inside your project directory, set the desired python version with:
pyenv local 3.8.0
which creates a file
.python-version containing the version string.
And now let's install a package locally just for our project: TODO: there is no nice way it seems: https://stackoverflow.com/questions/30407446/pyenv-choose-virtualenv-directory/59267972#59267972
Now, when someone wants to use your project, they will do:
which sets the Python version to the correct one.
Tested on Ubuntu 18.04, pyenv 1.2.15.