How to Install Python 3 and Create Virtual Environment In Ubuntu
- By : Mydatahack
- Category : Infrastructure, Installation and Configuration
- Tags: installation, Linux, Python 3, Ubuntu, Virtual Environment
If you need to use the latest Python 3 version in Linux as a non-root user, you can download the latest python in your space and install it there. In this way, you will have the separate Python development environment from the one that OS relies on. Once you installed Python, it is a good idea to know how to set up virtual environments for development. In this example, I used Ubuntu 16.04 LTS.
If you are interested in doing the same thing in Centos, Redhat and Amazon Linux, check out the blog post here.
(1) Install necessary packages before running Python installation
If you miss some of the packages, pip may fail to download packages after installation.
sudo apt-get install zlib1g-dev
sudo apt-get install libreadline-gplv2-dev libncursesw5-dev libssl-dev \
libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev
(2) Download Python and configure for installation
The prefix parameter will create the folder where Python 3 lives. If you already have the folder with the same name, it will give you error. Make sure to upgrade pip in the call. Make sure to get the download link for the latest Python version. It is 3.6.4 for now.
cd python3
wget https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tar.xz
tar xf Python-3.*
cd Python-3*
./configure --prefix=/home/user/python3 --with-ensurepip=upgrade
(3) Build and install
make altinstall
(4) Create environment variables for new Python and Pip
export pip3=/home/user/python3/bin/pip3.6
(5) Install virtualenv and create a virtual environment
$python3 -m venv lambda
source /home/user/python3/lambda/bin/activate
(6) See if it works.
pip
It works! Yay!