Install or Upgrade to Python 3.6.5 in Ubuntu 16.04 /14.04

Post Reply
User avatar
Eli
Senior Expert Member
Reactions: 183
Posts: 5334
Joined: 9 years ago
Location: Tanzania
Has thanked: 75 times
Been thanked: 88 times
Contact:

#1

Python 3.6.5 was released on 2018-03-28. This version of (Python 3.6.5) is the fifth maintenance release of Python 3.6. The Python 3.6 series contains many new features and optimizations. See more Python Release 3.6.5

Installation Procedure

Download Python 3.6.5:

Code: Select all

 $wget https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tgz
Uncompress/untar Python 3.6.5 and move to its directory:

Code: Select all

tar xvf Python-3.6.5.tgz
cd Python-3.6.5/

Install Python 3.6.5:

Code: Select all

./configure --enable-optimizations

Code: Select all

make -j8 #This step may be lengthy and/or slow
-j8 literally means use 8 threads to build in parallel. type "man make | grep jobs" in a terminal to see its documentation.

Use altinstall if you want to keep previously installed version(s)

Code: Select all

$sudo make altinstall
Use install if you want to replace previously installed version(s)

Code: Select all

$sudo make install
Test Python 3.6.5 by typing:

Code: Select all

$python3.6
You should see something like this:

Code: Select all

Python 3.6.5 (default, May 16 2018, 13:36:12) 
[GCC 4.8.5] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
Once the installation works, check its launching path:

Code: Select all

$ which python3.6
/usr/local/bin/python3.6
Finally, change the links for python3.6 binary in order to be able to evoke Python 3.6.5 by just typing python3 instead of python3.6:

Code: Select all

sudo ln -s /usr/local/bin/python3.6 /usr/local/python3
See also

Updating Python on Ubuntu System

Note: Be carefully when deciding to remove the previous/old versions of Python from Ubuntu Linux, Find out why:

Physically uninstalling Python 3.4

How to remove Python 2 in Ubuntu 16.04
0
TSSFL -- A Creative Journey Towards Infinite Possibilities!
User avatar
Eli
Senior Expert Member
Reactions: 183
Posts: 5334
Joined: 9 years ago
Location: Tanzania
Has thanked: 75 times
Been thanked: 88 times
Contact:

#2

You can now use pip to install packages that are particular to a certain Python version. If you have two versions of Python 3 installed in your system, say python3.4 and python3.6, be sure to specify the python3 version that you want to use the package with, for example, to install Python-Numpy for Python 3.6, execute

Code: Select all

pip3.6 install numpy

You can install other packages the same way:

healpy:

Code: Select all

sudo pip3.6 install healpy
h5py:

Code: Select all

sudo pip3.6 install h5py

Only use sudo if you need permission.

You can upgrade pip for python3.6 by simply

Code: Select all

$sudo pip3.6 install --upgrade pip
You may get this warning during installation using pip:

Code: Select all

The directory '/home/tssfl/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/home/tssfl/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
To get rid of the above warning, use sudo's -H flag:

Code: Select all

sudo -H pip3.6  install package_name
Python 3.6 seems to have issues installing tkinter (tk backend) needed for matplotlib pyplot plot/figure display . You may encounter the error message,

Code: Select all

File "/usr/local/lib/python3.6/tkinter/__init__.py", line 36, in <module>
import _tkinter # If this fails your Python may not be configured for Tk
ModuleNotFoundError: No module named '_tkinter'
The problem can temporarily be solved by telling the matplotlib to use a non-GUI backend, so that it cannot display the figure using tkinter by putting the following at the beginning of your Python code:

Code: Select all

import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as plt #This works without any error
0
TSSFL -- A Creative Journey Towards Infinite Possibilities!
User avatar
Eli
Senior Expert Member
Reactions: 183
Posts: 5334
Joined: 9 years ago
Location: Tanzania
Has thanked: 75 times
Been thanked: 88 times
Contact:

#3

Uninstall Python Compiled from source

It happens that you no longer need the Python 3.6.5 we installed by using make from the source as described above due to its packages conflicts with other installed version of Python, causing trouble or for whatever reason. The way this version was installed makes it difficult to uninstall by sudo apt-get remove pythonx, sudo apt-get purge pythonx, sudo apt autoremove python or sudo apt-get remove --purge pythonx or sudo apt purge pythonx-minimal method because the method has no idea that this version of Python exists. Here, Python 3.6.5 is compiled from source and thus it's not registered with the aptitude package.

In most cases, make uninstall or sudo make uninstall may fail to remove the installed Python 3.6.5.

You can also try to get a look at the steps used to install the software by running:

make -n install

If this method fails (because the python developers have not built for make uninstall), the easiest way to uninstall (most makefiles don't have an uninstall target) is to run make install again in our Python 3.6.5 source directory and capture what it sticks where and then remove them. The summary of this approach is as follows:

  1. Install checkinstall
  2. Use checkinstall to make a deb of your Python installation
  3. Use dpkg -r to remove the deb.
checkinstall monitors installation scripts such as "make install" and creates a deb package instead of directly installing the files.

So, we can use checkinstall to create the deb package, and then use the deb package to uninstall the python in /usr/local.

Install the checkinstall package and move to the Python 3.6.5 directory:

Code: Select all

sudo apt-get install checkinstall 
cd /home/user/Python-3.6.5
Make the deb package:

Code: Select all

sudo checkinstall -D --fstrans=no make install #This step can be lengthy

Please answer (choosing default answers should be fine) the questions asked by checkinstall. After answering the last question, the process will hang on for sometime and the continue,

Copying files to the temporary directory...

OK

Stripping ELF binaries and libraries...OK

Compressing man pages...OK

Building file list...OK

Building Debian package...

Installing Debian package...OK

Erasing temporary files...OK

Writing backup package...OK
OK

Deleting temp dir...OK


**********************************************************************

Done. The new package has been installed and saved to

/home/user/Python-3.6.5/python_3.6.5-1_amd64.deb

You can remove it from your system anytime using:

dpkg -r python

**********************************************************************


You should end up with a deb package called python_3.6.5-1_amd64.deb and backup-051820182104-pre-python.tgz.

Now, let's uninstall python from source:

Code: Select all

sudo dpkg -i Python-3.6.5.deb   # Reinstall python to /usr/local

Check the version of python3.6 installed

Code: Select all

$readlink -f $(which python3.6) | xargs -I % sh -c 'echo -n "%: "; % -V'
$which -a python3.6 #For less info

and then remove it

Code: Select all

sudo dpkg -r Python-3.6.5       # Remove python from /usr/local

See more about Installing software from source code with checkinstall

If you assume that a Python binary is always named python<something> (python*) and be a binary file, you can just search the entire system for files that match this criterion:

  1. $sudo find / -type f -executable -iname 'python*' -exec file -i '{}' \; | awk -F: '/x-executable; charset=binary/ {print $1}' | xargs readlink -f | sort -u | xargs -I % sh -c 'echo -n "%: "; % -V'
  2.  
  3. /home/tssfl/Environments/Django_Pro/bin/python3.4: Python 3.4.3
  4. /home/tssfl/flask-pycon2015/venv/bin/python: Python 2.7.6
  5. /home/tssfl/flasky/venv/bin/python: Python 2.7.6
  6. /home/tssfl/Python-3.3.7/python: Python 3.3.7
  7. /home/tssfl/Python-3.6.5/python: Python 3.6.5
  8. /home/tssfl/venv/bin/python: Python 2.7.6
  9. /home/tssfl/yowsup/bin/python: Python 2.7.6
  10. /usr/bin/python2.7: Python 2.7.6
  11. /usr/bin/python3.4: Python 3.4.3
  12. /usr/bin/python3.4m: Python 3.4.3
  13. /usr/local/bin/python3.3: Python 3.3.7
  14. /usr/local/bin/python3.3m: Python 3.3.7
  15. /usr/local/bin/python3.6: Python 3.6.5
  16. /usr/local/bin/python3.6m: Python 3.6.5


See the difference between pythonx and pythonxm.

The cheaper way to remove the versions of Python you don't want would be to sudo rm -rf /usr/local/bin/python3 and probably remove anything else in /usr/local/bin/py* including symlinks to various parts of the suite.

If your shell remembers removed versions of python,

Code: Select all

$sudo apt-get install --reinstall pythonx
or

Code: Select all

bash$hash -r
will clean them up.
0
TSSFL -- A Creative Journey Towards Infinite Possibilities!
CHB
Member
Reactions: 1
Posts: 2
Joined: 9 years ago

#4

Thank you for the useful posts, I however use the Windows Os and when I try to upgrade from python 2.7 to 3.6.5 it fails the first step I run the application what is the problem and may I get any support from the team? Thanks :pray: :pray: :pray:
1
1 Image
User avatar
Eli
Senior Expert Member
Reactions: 183
Posts: 5334
Joined: 9 years ago
Location: Tanzania
Has thanked: 75 times
Been thanked: 88 times
Contact:

#5

CHB wrote: 6 years ago Thank you for the useful posts, I however use the Windows Os and when I try to upgrade from python 2.7 to 3.6.5 it fails the first step I run the application what is the problem and may I get any support from the team? Thanks :pray: :pray: :pray:

Hi @CHB

Welcome to Open Discussion Forums.

Python has two major series, Python 2.x and Python 3.x, so you cannot upgrade from a version of one series to another. You can however install Python 3.6.5 in your system even though you have Python 2.7. For some Linux-based distributions, such as Ubuntu Linux, every Python version has its own importance, depending on the system version, there are applications that depend solely on a particular version of Python, so uninstalling it may break the system.

The best way for you to install Python 3.5.6 on Windows OS is to use virtual environments (virtualenv or virtualenvwrapper) python-virtual-environments-virtualenv-5558 and install with pip3.

Python venv is a great way for separate Python environments and their specific dependencies, this way you will avoid to break your system, for example, due to package conflicts.

Let me know if you still need any help.
0
TSSFL -- A Creative Journey Towards Infinite Possibilities!
User avatar
Eli
Senior Expert Member
Reactions: 183
Posts: 5334
Joined: 9 years ago
Location: Tanzania
Has thanked: 75 times
Been thanked: 88 times
Contact:

#6

The previous procedure shows how to install Python 3 from the source. We can however quickly install and update Python to the latest version as follows:


1. If not installed, add software-properties-common. This software provides some additional scripts that are useful when adding and/or removing a PPA repositories.

  1. sudo apt install software-properties-common


2. Add the PPA repository, deadsnakes and update system to save and recognize changes:

  1. sudo add-apt-repository ppa:deadsnakes/ppa
  2. sudo apt-get update


3. Install Python 3.9.x.

  1. sudo apt install python3.9


If you want to install Python3.9 simultaneously with pip and virtualenv, use the command below instead:

  1. sudo apt install python3.9 python3.9-venv python3.9-dev


4. You can finally check that Python3.9.x is installed/updated:

  1. $python3 -V
  2. Python 3.6.9

0
TSSFL -- A Creative Journey Towards Infinite Possibilities!
User avatar
Eli
Senior Expert Member
Reactions: 183
Posts: 5334
Joined: 9 years ago
Location: Tanzania
Has thanked: 75 times
Been thanked: 88 times
Contact:

#7

You may also quickly install Ipython3 as:

  1. sudo apt-get update -y
  2. sudo apt-get install -y ipython3
  3. #OR
  4. pip3 install ipython

0
TSSFL -- A Creative Journey Towards Infinite Possibilities!
User avatar
Eli
Senior Expert Member
Reactions: 183
Posts: 5334
Joined: 9 years ago
Location: Tanzania
Has thanked: 75 times
Been thanked: 88 times
Contact:

#8

Upgrade to the latest version of Python 3 in 2023:

  1. #Add PPA and update repositories
  2. sudo add-apt-repository ppa:deadsnakes/ppa
  3. sudo apt update
  4.  
  5. #Check if Python 3.12 is available by running
  6. apt list | grep python3.12
  7.  
  8. #Install
  9. sudo apt install python3.12

Make python3.12 a default python by replacing the older versions in Ubuntu as follows:

$nano ~/.bashrc
alias python=python3.12 (Add this line on top of .bashrc file)
Press ctrl+o (To save the file)
Press Enter
Press ctrl+x (To exit the file)
$source ~/.bashrc
Or
. ~/.bashrc (To refresh the bashrc file)

You can then type python to start python3.12.
0
TSSFL -- A Creative Journey Towards Infinite Possibilities!
User avatar
Eli
Senior Expert Member
Reactions: 183
Posts: 5334
Joined: 9 years ago
Location: Tanzania
Has thanked: 75 times
Been thanked: 88 times
Contact:

#9

Add Python 3.10.9 & Python 3.11.3 to update-alternatives: Upgrade Python to the latest version which is 3.11.3

Add both old and new versions of Python to Update Alternatives:

  1. sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.10 9
  2. sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.11 3

Update Python 3 for point to Python 3.11

By default, Python 3 is pointed to Python 3.10. That means when we run python3 it will execute as python 3.10 but we want to execute this as python 3.11.

Type this command to configure python3:

  1. sudo update-alternatives --config python3

You should get the above output. Now type 2 and hit enter for Python 3.11.3. Remember the selected number may differ so choose the selection number which is for Python 3.11.x

Check the version of python:

0
TSSFL -- A Creative Journey Towards Infinite Possibilities!
User avatar
Eli
Senior Expert Member
Reactions: 183
Posts: 5334
Joined: 9 years ago
Location: Tanzania
Has thanked: 75 times
Been thanked: 88 times
Contact:

#10

WARNING: The script pygmentize is installed in '/home/tssfl/.local/bin' which is not on PATH. Consider adding this directory to PATH:

You need to add the following line to your ~/.bash_profile or ~/.bashrc file.

  1. export PATH="/home/tssfl/.local/bin:$PATH"

You will then need to refresh the profile, do this by either running the command:

  1. source ~/.bashrc

Or simply close your terminal and open a new session. Continue to check your PATH to make sure it includes the path:

0
TSSFL -- A Creative Journey Towards Infinite Possibilities!
Post Reply
  • Similar Topics
    Replies
    Views
    Last post

Return to “Linux and Unix Based Operating Systems”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 4 guests