[Python][Ubuntu] Setting up a virtual environment — virtualenvwrapper
Using virtual environment, we can manage package installations for different projects that we work on, hence it will make sure that that our global packages are untouched.
In brief, I will walk you through setting up a virtual environment in ubuntu.
Check desired python version is installed
Before we start up setting up virtual environment, we need to make sure that we have following packages installed
python [any version]
pip [python-pip]
In Ubuntu 16.04, we have python2.7 version installed by default and in Ubuntu18.04, python3.6 comes default.
Do not get confused with this. If you are using python2.7 version in your machine, then install python-pip package and we can configure virtual environment with that. On the other hand if it is python3, then install python3-pip and configure the environment.
So there comes another question, if I want a python3 virtual environment, then how I will do that in Ubuntu16.04 with default python2.7 version. I will explain this while we configure things up.
Install required packages
We will discuss on installing packages in both Ubuntu16 and 18 versions here.
Ubuntu16.04:
$ sudo apt update
$ sudo apt upgrade
$ python —-version
Python 2.7.12
Before installing packages make sure that we run update and upgrade to make sure that our repository is up to date and the packages are latest.
Now it’s see if we have pip installed in our server
$ pip
The program 'pip' is currently not installed. You can install it by typing:
sudo apt install python-pip
We need to install python-pip package.
$ sudo apt install python-pip
Reading package lists... Done
Building dependency tree
Reading state information... Done
...
.
update-alternatives: using /usr/bin/file-rename to provide /usr/bin/rename (rename) in auto mode
Processing triggers for libc-bin (2.23-0ubuntu11) ...
$
We are done with installing pre-requisites in Ubuntu16.04
Ubuntu 18.04:
$ sudo apt update
$ sudo apt upgrade
We will check for python version
$ python --version
Command 'python' not found, but can be installed with:
sudo apt install python3
sudo apt install python
sudo apt install python-minimalYou also have python3 installed, you can run 'python3' instead.
Does this mean that we don’t have python? no, it is not like that. We have python3.6 installed on server. We just want to create a symbolic link to make our work easy.
$ which python3
/usr/bin/python3
$
$ sudo ln -s /usr/bin/python3 /usr/bin/python
$
$ python --version
Python 3.6.9
Now we will install python3-pip package
$ sudo apt install python3-pip
Reading package lists... Done
Building dependency tree
Reading state information... Done
...
.
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
Processing triggers for libc-bin (2.27-3ubuntu1) ...
$
$
$ which pip3
/usr/bin/pip3
$ sudo ln -s /usr/bin/pip3 /usr/bin/pip
$
$ pip --version
pip 9.0.1 from /usr/lib/python3/dist-packages (python 3.6)
$
Install and configure virtualenvwrapper
Same step follows for both flavours of Ubuntu version discussed here
$ sudo pip install virtualenvwrapper
Collecting virtualenvwrapper
Downloading https://files.pythonhosted.org/packages/c1/6b/2f05d73b2d2f2410b48b90d3783a0034c26afa534a4a95ad5f1178d61191/virtualenvwrapper-4.8.4.tar.gz (334kB)100% |████████████████████████████████| 337kB 913kB/s
...
..
.
Running setup.py install for virtualenvwrapper ... done
Successfully installed pbr-5.4.4 six-1.13.0 stevedore-1.31.0 virtualenv-clone-0.5.3 virtualenvwrapper-4.8.4
Check the presence of virtualenvwrapper.sh file
$ ls -l /usr/local/bin/virtualenvwrapper.sh
-rwxr-xr-x 1 root root 41703 Feb 9 2019 /usr/local/bin/virtualenvwrapper.sh
Update ~/.profile by including the /usr/local/bin in $PATH.
PATH="/usr/local/bin:/usr/bin:$HOME/bin:$PATH"
export WORKON_HOME=$HOME/NikhilLearning/VirtualEnv
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python
source /usr/local/bin/virtualenvwrapper.sh
Start the virtualenvwrapper
To start virtual environment in Ubuntu
source ~/.profile
$ source ~/.profilevirtualenvwrapper.user_scripts creating /home/nnarayanan/NikhilLearning/VirtualEnv/premkproject
virtualenvwrapper.user_scripts creating /home/nnarayanan/NikhilLearning/VirtualEnv/postmkproject
virtualenvwrapper.user_scripts creating /home/nnarayanan/NikhilLearning/VirtualEnv/initialize
virtualenvwrapper.user_scripts creating /home/nnarayanan/NikhilLearning/VirtualEnv/premkvirtualenv
virtualenvwrapper.user_scripts creating /home/nnarayanan/NikhilLearning/VirtualEnv/postmkvirtualenv
virtualenvwrapper.user_scripts creating /home/nnarayanan/NikhilLearning/VirtualEnv/prermvirtualenv
virtualenvwrapper.user_scripts creating /home/nnarayanan/NikhilLearning/VirtualEnv/postrmvirtualenv
virtualenvwrapper.user_scripts creating /home/nnarayanan/NikhilLearning/VirtualEnv/predeactivate
virtualenvwrapper.user_scripts creating /home/nnarayanan/NikhilLearning/VirtualEnv/postdeactivate
virtualenvwrapper.user_scripts creating /home/nnarayanan/NikhilLearning/VirtualEnv/preactivate
virtualenvwrapper.user_scripts creating /home/nnarayanan/NikhilLearning/VirtualEnv/postactivate
virtualenvwrapper.user_scripts creating /home/nnarayanan/NikhilLearning/VirtualEnv/get_env_details
Create virtual environment
We can create a virtual environment specific for a project by using mkvirtualenv command
$ mkvirtualenv project-1New python executable in /home/nnarayanan/NikhilLearning/VirtualEnv/project-1/bin/python
Installing setuptools, pip, wheel...
done.
virtualenvwrapper.user_scripts creating /home/nnarayanan/NikhilLearning/VirtualEnv/project-1/bin/predeactivate
virtualenvwrapper.user_scripts creating /home/nnarayanan/NikhilLearning/VirtualEnv/project-1/bin/postdeactivate
virtualenvwrapper.user_scripts creating /home/nnarayanan/NikhilLearning/VirtualEnv/project-1/bin/preactivate
virtualenvwrapper.user_scripts creating /home/nnarayanan/NikhilLearning/VirtualEnv/project-1/bin/postactivate
virtualenvwrapper.user_scripts creating /home/nnarayanan/NikhilLearning/VirtualEnv/project-1/bin/get_env_details
(project-1) nnarayanan@node1:~$
(project-1) nnarayanan@node1:~$
(project-1) nnarayanan@node1:~$ deactivate
nnarayanan@node1:~$
We can see that we are now inside the virtual environment named project-1. We can install any package inside this environment using pip and this won’t affect any packages outside this environment.
How can I install a python3 virtual environment inside a Ubuntu 16.04
There are two options for that.
- Remove python2 from the server and install python3 version and make it default one.
- Install python3 along with python2 on Ubuntu 16.04
I believe option [2] is good. If we follow option [2], then all the steps till configuring virtualenvwrapper is same. Which means we can configure virtualenvwrapper with python2 version itself. The change happens when we create a virtual environment.
We can create a virtual environment by specifying where my python3 binary.
$ mkvirtualenv -p $(which python3) project-2
Once we have configured the virtual environment. We can access virtual environment or choose one between two or more virtual environments by using workon command [which comes with virtualenvwrapper package]
$ workon {press the tab to list all}
django project-1 project-2
$ workon django
(django) nnarayanan@node1:~$
(django) nnarayanan@node1:~$
(django) nnarayanan@node1:~$ deactivate
nnarayanan@node1:~$
Hope this helps you all.
Thanks for reading, have a great day ahead.