Installing Python 3 with virtualenv

Izabela Borges
Feb 13, 2018 · 5 min read


Installing Python

$ python -V$ python3 -V

1. The outputs can be the same for both commands depending on your distribution.
2. If you already have Python 3.6 installed you can jump to the
pip installation.

# Ubuntu 16.10 and newer
$ sudo apt-get update
$ sudo apt-get install python3.6

# Older Ubuntu versions
$ sudo apt-get install software-properties-common
$ sudo add-apt-repository ppa:deadsnakes/ppa
$ sudo apt-get update
$ sudo apt-get install python3.6

# Fedora
$ sudo dnf install python3

# Arch Linux
$ pacman -S python
# Install GCC through xCode tools if you don't have it
$ xcode-select --install

# Install Homebrew if you don't have it
$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

# Install Python
$ brew install python3

Installing python-dev

# Ubuntu 16.10 and newer
$ sudo apt-get update
$ sudo apt-get install python-dev python3-dev python3.6-dev

# Older Ubuntu versions
$ sudo apt-get update
$ sudo apt-get install python-dev python3-dev python3.6-dev

# Fedora
# If dnf doesn't work, you can use the same commands as Ubuntu 16.10
$ sudo dnf install python-devel python3-devel

# Arch Linux
$ pacman -S base-devel

Installing pip

$ pip -V
$ python3.6 get-pip.py

Installing virtualenv and setting up safe virtual environments

Installing virtualenv

$ pip3 install -U virtualenv

# Check the installation
$ virtualenv --version

Limiting pip to virtualenvs

# Create the projects and virtualenv folders 
$ mkdir -p ~/Projects ~/Virtualenvs

# Mac OS
# Creating the pip config folder
$ mkdir ~/Library/Application\ Support/pip

# Creating the config file
$ vim ~/Library/Application\ Support/pip/pip.conf
# Linux
# Creating the pip config folder
$ mkdir ~/.config/pip
# Creating the config file
$ vim ~/.config/pip/pip.conf
[install]
require-virtualenv = true

[uninstall]
require-virtualenv = true

Creating virtualenvs

$ virtualenv environment-name
$ virtualenv -p python3 environment-name

Creating the globalpip alias

globalpip(){
PIP_REQUIRE_VIRTUALENV="" pip "$@"
}
globalpip3(){
PIP_REQUIRE_VIRTUALENV="" pip3 "$@"
}
$ globalpip3 install --upgrade pip setuptools wheel virtualenv

Activating and deactivating environments

$ source ~/Virtualenvs/environment-name/bin/activate
$ deactivate

Izabela Borges

Written by

A Data Scientist wannabe, AI enthusiast that likes to discuss it’s ethics, front-ender by fun, bookworm, yogini and painter on the free hours.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade