14

It's my first time using ubuntu so I don't know how to use it very well yet... How can i install python 3.5.2 ? It has already the version 2.7.12 . My Ubuntu version is Ubuntu 16.04 LTS (desktop).

  • 3
    just fyi, you probably already have python 3.5.1 (though not 3.5.2). Try opening a terminal & typing python3... – Zanna Jul 13 '16 at 11:42
  • @Zanna it shows 3.5.1-3 on my system – grooveplex Jul 13 '16 at 12:11
  • you're right , i typed python 3 and showed this "Python 3.5.1+ "

    however how can I update to the 3.5.2 version? i will start learning with a book that has that version...

    – Rui Miranda Jul 13 '16 at 12:45
  • 5
    the differences between python 2* and python 3* are big, you would notice a lot of difference in behaviour. But python 3.5.1+ and 3.5.2 are very similar and will behave almost the same. It's possible to compile the newer version from source, but using repo versions is generally recommended - that way package management will look after its dependencies and keep the app updated. My advice would be just dive in with the book using python3 (and IDLE 3 (to get it type in a terminal sudo apt install idle3) if the book recommends) – Zanna Jul 13 '16 at 13:46
  • 4
    @RuiMiranda If you are new to Ubuntu/Linux and Python, don't bother trying to upgrade your Python 3 from 3.5.1 to 3.5.2 by compiling from source; you will only end up coming back to this site with even more questions that distract you from learning Python. – edwinksl Jul 13 '16 at 16:47
  • 4
    See Is it possible to install Python 3.x in 14.04 LTS, or upgrade from Python 2.7.6 to 2.7.8?. But I join Zanna and edwinksl in advising that you use the version of Python 3 provided by your system for learning Python 3 programming unless you have a specific and compelling reason to use a different version. – Eliah Kagan Jan 02 '17 at 17:44

2 Answers2

8

Basic process:

sudo apt-get update
sudo apt-get install python3

If you want a very specific version:

sudo apt-cache show python3
sudo apt-get install python3=3.5.1*
  • 2
    I get E: Unable to locate package python3= when I run your suggested command. Are you show that is right? – Charlie Parker Dec 16 '16 at 07:09
  • 1
    It's a single equals sign to denote a specific version. – Thomas Ward Jan 02 '17 at 17:29
  • @ThomasWard who is that comment addressed at? – Charlie Parker Feb 08 '17 at 15:56
  • 5
    this still doesn't work for me I get E: Version '3.5.1*' for 'python3' was not found – Charlie Parker Feb 08 '17 at 16:00
  • 1
    @CharlieParker, originally I'd written "python3==3.5.1*", which is why you got that "unable to locate" error. Note the two equals signs. – 300D7309EF17 Feb 08 '17 at 16:00
  • 1
    @tedder42 ah ok. I've ran it with one equal sign and the star too and it still doesn't work. Does it work for you? Not sure if the fact I'm inside a docker container makes any difference but its unable to find the python version. What am I suppose to see with apt-cache show python3? – Charlie Parker Feb 08 '17 at 16:03
  • 1
    This doesn't work. Version '3.5.1*' for 'python3' was not found – Jibu James Jun 29 '19 at 11:41
  • @JibuJames what's available has changed in two years. What version of Ubuntu are you using? Right now it looks like two 3.6.* versions are installable from a generic 18.04 install, and 3.5.1-3 is available from a generic 16.04 install. – 300D7309EF17 Jun 29 '19 at 16:56
  • this does not work for Ubuntu 16.04, since the python version in the repos is 3.5.1 – Him Aug 14 '19 at 16:22
6

As you must have already noticed that Ubuntu 16.04 has 'python 2.7.12' by default. To check the default python version, run below line

   $ python -V

it should return 'Python 2.7.12'

It is recommended that we don't try to modify/uninstall this default package of python because there could be many other system files/applications depending on it, and it might create some unexpected errors or issues if we uninstall this default python package.

So, to use the latest version of python, it would be better to go for creating a virtual environment ('virtualenv'). This is a module inside python which facilitates us to use multiple python versions on the same system.

Step-1: First install python3 -

$ sudo apt-get update
$ sudo apt-get install python3

Step-2: Now install 'virtualenv'-

$ pip install virtualenv

Step-3: Now set the path of virtualenv to your desired directory. lets first create a desired directory-

$ mkdir MYENV

it will create a folder in the current directory with the name 'MYENV'

Step-4: set the path of virtualenv to the created(desired) directory-

$ sudo virtualenv -p python3 MYENV

Step-5: Activate the virtualenv

$ source MYENV/bin/activate

done.. you should be able to get (MYENV) as prefix in the terminal command line. Now run below command

$ python -V

it should return 'python 3.5.2'

Ajay Singh
  • 201
  • 2
  • 3
  • This is awesome ans amongst of all the ans given SE sites. – CKM Mar 14 '17 at 06:22
  • 1
    Few remarks : 1/ No need to install python3, it's already available in Ubuntu 16.04. 2/ No need to install virtualenv. You can spin up a virtualenv using the recommanded python3 -m venv MYVENV. So basically all you have to type is python3 -m venv MYVENV && source MYVENV/bin/activate – Antwan Aug 18 '17 at 19:16