Tutorial

How To Install Python 3 and Set Up a Programming Environment on Ubuntu 20.04 [Quickstart]

Published on April 24, 2020
English
How To Install Python 3 and Set Up a Programming Environment on Ubuntu 20.04 [Quickstart]

Introduction

Python is a flexible and versatile programming language, with strengths in scripting, automation, data analysis, machine learning, and back-end development.

This tutorial will walk you through installing Python and setting up a programming environment on an Ubuntu 20.04 server. For a more detailed version of this tutorial, with more thorough explanations of each step, please refer to How To Install Python 3 and Set Up a Programming Environment on an Ubuntu 20.04 Server.

Step 1 — Update and Upgrade

Logged into your Ubuntu 20.04 server as a sudo non-root user, first update and upgrade your system to ensure that your shipped version of Python 3 is up-to-date.

  1. sudo apt update
  2. sudo apt -y upgrade

Confirm installation if prompted to do so.

Step 2 — Check Version of Python

Check which version of Python 3 is installed by typing:

  1. python3 -V

You’ll receive output similar to the following, depending on when you have updated your system.

Output
Python 3.8.2

Step 3 — Install pip

To manage software packages for Python, install pip, a tool that will help you manage libraries or modules to use in your projects.

  1. sudo apt install -y python3-pip

Python packages can be installed by typing:

  1. pip3 install package_name

Here, package_name can refer to any Python package or library, such as Django for web development or NumPy for scientific computing. So if you would like to install NumPy, you can do so with the command pip3 install numpy.

Step 4 — Install Additional Tools

There are a few more packages and development tools to install to ensure that we have a robust set-up for our programming environment:

  1. sudo apt install build-essential libssl-dev libffi-dev python3-dev

Step 5 — Install venv

Virtual environments enable you to have an isolated space on your server for Python projects. We’ll use venv, part of the standard Python 3 library, which we can install by typing:

  1. sudo apt install -y python3-venv

Step 6 — Create a Virtual Environment

You can create a new environment with the pyvenv command. Here, we’ll call our new environment my_env, but you should call yours something meaningful to your project.

  1. python3 -m venv my_env

Step 7 — Activate Virtual Environment

Activate the environment using the command below, where my_env is the name of your programming environment.

  1. source my_env/bin/activate

Your command prompt will now be prefixed with the name of your environment:

Step 8 — Test Virtual Environment

Open the Python interpreter:

  1. python

Note that within the Python 3 virtual environment, you can use the command python instead of python3, and pip instead of pip3.

You’ll know you’re in the interpreter when you receive the following output:

Python 3.8.2 (default, Mar 13 2020, 10:14:16) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

Now, use the print() function to create the traditional Hello, World program:

  1. print("Hello, World!")
Output
Hello, World!

Step 9 — Deactivate Virtual Environment

Quit the Python interpreter:

  1. quit()

Then exit the virtual environment:

  1. deactivate

Further Reading

From here, there is a lot you can learn about Python, here are some links related to this guide:

Get Ubuntu on a hosted virtual machine in seconds with DigitalOcean Droplets! Simple enough for any user, powerful enough for fast-growing applications or businesses.

Learn more here

About the authors

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
2 Comments


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

in step.6

you create a virtualenv with python3.6 ?? but there’s no python3.6 in ubuntu 20.04 !

The virtual environment is nice. But now I have to use Pycharm IDE for python. How to use Pycharm with this virtual environment such that if i create a new project in Pycharm it should be added to this virtual environment ??

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel