Do it your self hobby code

How to install Python 3 and Pip3 in Ubuntu 16

This is the easiest way to install Python 3 on Ubuntu 16 server and making it the default program to run Python 3.

Update first your system

sudo apt-get update

Install prerequisite


sudo apt-get install build-essential libpq-dev libssl-dev openssl libffi-dev zlib1g-dev

sudo apt-get install python-pip python3-pip python3-dev -y

Making Python to execute Python3

A simple safe way would be to use an alias:

sudo nano ~/.bashrc

and add this code on the top:

alias python=python3

To apply the changes, run this:

source ~/.bashrc

Lets check the changes:

python --version

The return should be something like this:
Python 3.4.3

Making Pip3 as the default pip

sudo nano ~/.bashrc

Paste this code on the top:

alias pip='pip3'

Now let’s apply the changes:

source ~/.bashrc

Check the changes if applied:

pip --version

You should see something like this:
pip 1.5.4 from /usr/lib/python3/dist-packages (python 3.4)

Use Python version system wide

We can use update-alternatives. But first let’s see what versions do we have.

<pre><code>/usr/bin/python --version
 #Python 2.7.16

ls /usr/bin/python*
 #/usr/bin/python2.7
 #/usr/bin/python3.4

</code></pre>

I got two versions of Python. Let’s check if we have python alternatives:

<pre><code>update-alternatives --list python
 #update-alternatives: error: no alternatives for python

</code></pre>

I don’t have alternatives for python. Let’s add Python2.7 and Python3.4

<pre><code>sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
 #update-alternatives: using /usr/bin/python2.7 to provide /usr/bin/python (python) in auto mode


sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.4 2
 #update-alternatives: using /usr/bin/python3.4 to provide /usr/bin/python (python) in auto mode

</code></pre>

List all python alternatives:

<pre><code>update-alternatives --list python
/usr/bin/python2.7
/usr/bin/python3.4

</code></pre>

Check the default python version:

<pre><code>python --version
 #Python 3.4.3


/usr/bin/python --version
 #Python 3.4.3

</code></pre>


You can switch to different version by using this command:

<pre><code>update-alternatives --config python
There are 2 choices for the alternative python (providing /usr/bin/python).

  Selection    Path                Priority   Status
------------------------------------------------------------
* 0            /usr/bin/python3.4   2         auto mode
  1            /usr/bin/python2.7   1         manual mode
  2            /usr/bin/python3.4   2         manual mode

Press <enter> to keep the current choice[*], or type selection number:
</code></pre>

Leave a Comment

Your email address will not be published.