From d379ea9f902c0c08a0e31db3838e07eaff4bb684 Mon Sep 17 00:00:00 2001 From: Xiang ZHU Date: Sat, 13 Jan 2024 23:50:10 +0100 Subject: [PATCH] improve installing python3 on ubuntu --- ...2019-10-27-installing-python3-on-ubuntu.md | 87 +++++-------------- 1 file changed, 24 insertions(+), 63 deletions(-) diff --git a/docs/posts/2019/2019-10-27-installing-python3-on-ubuntu.md b/docs/posts/2019/2019-10-27-installing-python3-on-ubuntu.md index f8c94172..4d231826 100644 --- a/docs/posts/2019/2019-10-27-installing-python3-on-ubuntu.md +++ b/docs/posts/2019/2019-10-27-installing-python3-on-ubuntu.md @@ -16,9 +16,7 @@ Most of tutorials on the Internet about installing Python3.6 on Ubuntu are by [u -## Installing Python3.6 on Ubuntu 16.04 - -### Disabling IPv6 +## Disabling IPv6 IPv6 is enabled by default on Ubuntu 16.04, in some cases, your Ubuntu network connection might be very low due to IPv6. Use `ip a | grep inet6` to check if IPv6 is enabled. @@ -26,69 +24,19 @@ Ref: [How to disable ipv6 address on ubuntu 18 04 bionic beaver linux](https://l To disable IPv6 in a persist way, add following 2 lines in the file `/etc/sysctl.conf` and [reload the sysctl by `sudo sysctl --system`](https://www.cyberciti.biz/faq/reload-sysctl-conf-on-linux-using-sysctl/) or reboot the server: -``` +```bash net.ipv6.conf.all.disable_ipv6=1 net.ipv6.conf.default.disable_ipv6=1 ``` -### Installing build packages - -```bash -sudo apt install -y build-essential zlib1g-dev libssl-dev -``` - -!!! note - - without `libssl-dev` package, pip install will throw TLS/SSL error. - -!!! note - - From this point of view, installing Python on Windows by Scoop is much more pleasant :) - -### Installing Python3.6 from official source - -The latest Python3.6 version at the time of this writing is 3.6.9. - -```bash -# You may download the Python source to a local shared location (S3 or Artifactory, etc.) if you need to deploy Python to many servers. -wget https://www.python.org/ftp/python/3.6.9/Python-3.6.9.tgz -tar xzvf Python-3.6.9.tgz -cd Python-3.6.9 -sudo ./configure --prefix=/opt/python3.6 -make -j $(nproc) -sudo make install -sudo ln -s /opt/python3.6/bin/python3.6 /usr/bin/python3.6 -``` - -!!! warning - - Python3.5 is preinstalled by default on Ubuntu 16.04, `python3 -V` gives `Python 3.5.2`, many system tools rely on it, please **DO NOT** bind python3 to any versions other than Python3.5, otherwise your system might have unexpected problems. - -!!! note - - For a general Python installation not only for this Python3.6, if you have `gcc v8+`, you can add the flag `--enable-optimizations` to `./configure` to gain an extra runtime speed, otherwise you might encounter `Could not import runpy module` error - -### Using Python3.6 pip - -```bash -python3.6 -m pip install [a python module] -``` - -### Prevent pip install without an active venv - -```bash -echo 'export PIP_REQUIRE_VIRTUALENV=true' >> ~/.bashrc -``` - -## Installing Python3.7 on Ubuntu 16.04 - -Just tested installing Python3.7.5 with the same procedure, all works. - ## Installing Python3.10.10 with sqlite3 on Ubuntu 20.04 in WSL +!!! note "Same procedure applied to other Python versions" + ```bash # install build packages sudo apt update +# without `libssl-dev` package, pip install will throw TLS/SSL error. sudo apt install -y build-essential zlib1g-dev libssl-dev libffi-dev # install sqlite3 from source, if you need a specific sqlite3 version in Python, you must install it before compiling Python, because the compilation needs the lib libsqlite3.so @@ -113,14 +61,27 @@ wget https://www.python.org/ftp/python/3.10.10/Python-3.10.10.tgz tar xvf Python-3.10.10.tgz cd Python-3.10.10/ -# ubuntu 20.04 has gcc v9, so you can add the flag --enable-optimizations to ./configure +# ubuntu 20.04 has gcc v9 (v8+ are OK), so you can add the flag --enable-optimizations to ./configure +# to gain an extra runtime speed, otherwise you might encounter `Could not import runpy module` error # --with-bz2 is for pandas, otherwise modulenotfounderror: no module named '_bz2' pandas -./configure --prefix=$HOME/opt/python3.10 --with-bz2 +version=$(basename "$(pwd)" | cut -d'-' -f2 | cut -d'.' -f1-2) +sudo ./configure --prefix=$HOME/opt/python$version --with-bz2 --enable-optimizations make -j $(nproc) sudo make install make clean -sudo ln -s ~/opt/python3.10/bin/python3.10 /usr/bin/python3.10 -ll $(which python3.10) -echo -e '\nexport PIP_REQUIRE_VIRTUALENV=true' >> ~/.bashrc -python3.10 -c 'import sqlite3 ; print(sqlite3.sqlite_version)' +sudo ln -sf ~/opt/python$version/bin/python$version /usr/bin/python$version +ll $(which python$version) +if ! grep -q 'export PIP_REQUIRE_VIRTUALENV=true' ~/.bashrc; then + echo 'export PIP_REQUIRE_VIRTUALENV=true' >> ~/.bashrc + echo "added export PIP_REQUIRE_VIRTUALENV=true" +fi +python$version -c 'import sqlite3 ; print(sqlite3.sqlite_version)' +``` + +## Prevent pip install without an active venv + +```bash +if ! grep -q 'export PIP_REQUIRE_VIRTUALENV=true' ~/.bashrc; then + echo 'export PIP_REQUIRE_VIRTUALENV=true' >> ~/.bashrc +fi ```