How to Install Python on Ubuntu

Python is a popular and versatile high-level language that can be used for developing a huge variety of applications. In this tutorial, we are going to show you how to install Python on a Linux  Cloud VPS running Ubuntu as an operating system.

Update your system

Connect to your Linux VPS via SSH and update all the currently installed software to the latest version available using the command below:

# apt-get update
# apt-get -y upgrade

Don’t forget to regularly update your system. You can even enable automatic updates.

Now that your system has been upgraded we can proceed with the steps below.

Prerequisites for pyenv

Pyenv is a neat little tool that allows us to create a virtual environment and run different Python versions in this environment.
We are going to install pyenv as it is not advised to mess with the Python version Ubuntu uses because some core Ubuntu services might rely on this version.
Install headers needed to build CPython:

# apt-get install -y build-essential libbz2-dev libssl-dev libreadline-dev libsqlite3-dev tk-dev

Install curl as we need it to download and execute the pyenv install script:

# apt-get install -y curl

If you want to learn more about curl, see our 5 basic curl command examples.

Installing pyenv

We are going to use curl to download and execute the pyenv install script:

# curl -L https://raw.githubusercontent.com/pyenv/pyenv-installer/master/bin/pyenv-installer | bash

After the successful installation of pyenv, we are going to update pyenv:

# pyenv update

When pyenv is done updating we need to add the pyenv init lines to ~/.bash_profile or ~/.bashrc , we can do that by opening the file with nano:

# nano ~/.bash_profile

Next, add these lines in the file:

export PATH="~/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

Then press Ctrl+O and enter to save the changes to the file and Ctrl+X to exit nano.
Type in the following command to reload the bash shell with the current profile settings:

# exec bash

Making our own environment

Now we will make our own environment with pyenv and we will install Python 3.6.1 which is the latest Python version at the time of writing this tutorial.
To do that we need to run the following command:

# pyenv install 3.6.1

We make our own virtual environment with this version as this will allow us to install other versions later if we want to:

# pyenv virtualenv 3.6.1 main

If we prefer we can make this version our global version, meaning it will be called whenever we call python regardless of the folder we are in:

# pyenv global main

Or we can make this version our local version, meaning it will be only called from the folder we type the following command in:

# pyenv local main

This is very useful if you have separate projects that require different Python versions.
If for some reason you need to use the system version of Python you can tell pyenv to switch back to the system environment using the following commands:
Use the system environment globally:

# pyenv global system

Use the system environment locally:

# pyenv local system

That’s it, you now have the latest Python version installed.

Of course, you don’t have to do any of this if you use one of our Linux Cloud VPS hosting services, in which case you can simply ask our expert Linux admins to install Python and set up virtual environments for you. They are available 24×7 and will take care of your request immediately.

PS. If you liked this post please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks.

Leave a Comment